Im using zend framework 2. I want to call a function in model from zend form.
The situation is Im having a combo box & I need to bind data from database to fill its options and value.
This is my select tag in zend form
$this->add(array(
'name' => 'ddlcountry',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Country',
'value_options' => (here I've to call function),
),
));
For this value option I want to call a function which is in model below is my function in model:
public function fetchcountry()
{
$this->adapter = $this->getServiceLocator()->get('db');
$dbAdapterConfig = $this->adapter;
$dbAdapter = $dbAdapterConfig;
$driver = $dbAdapter->getDriver();
$connection = $driver->getConnection();
$result = $connection->execute("CALL sp_showcountry()");
$statement = $result->getResource();
$resultdata = $statement->fetchAll(\PDO::FETCH_OBJ);
return $resultdata;
}
before you write such a Question, please check at least the first 10 Questions on this Page, as your Question has been asked SEVERAL Times lately ;)
Please refer to my answer provided here:
How to get data from different model for select?
Or refer to my Blogpost, which covers your problem in detail
Zend\Form\Element\Select and Database-Values
Related
I am trying to make a chat sandboxing api, aka creating the user, channel and adding the user to the channel. The problem is that every time i call the restApi to create a new user, it doesn't take the attributes or the friendlyName. The code is the following:
$user->chat->services($serviceSid)->users->create(
array(
'identity' => $identity,
'friendlyName' => $friendlyName,
'attributes' => $attributes,
'roleSid' => $roleSid
)
);
Thanks.
Twilio developer evangelist here.
Your code to create the user isn't quite right there. create takes the identity as the first argument and then the optional arguments in an array as the second. Try this instead:
$user->chat->services($serviceSid)->users->create(
$identity,
array(
'friendlyName' => $friendlyName,
'attributes' => $attributes,
'roleSid' => $roleSid
)
);
Let me know if that helps at all.
I again ran into a problem I just canĀ“t seem to comprehend.
I have a form element like so:
$this->add(array(
'type' => 'datetime',
'name' => 'modifiedTime',
'options' => array(
'label' => 'Modified Time',
),
'attributes' => array(
'disabled' => 'disable',
),
));
This one does get filled correctly trough my entity(I am using doctrine) like so:
/**
* #ORM\Column(type="datetime", nullable=true, name="modified_time")
*
* #Form\Exclude()
*/
protected $modifiedTime;
public function getModifiedTime(){
return $this->modifiedTime;
}
public function populate($data)
{
$this->modifiedTime = date_create($data['modifiedTime']);
}
This works completly fine as long as the "disabled" attribute is not set. But as soon as it is I get a validation error claiming "Value is required and can't be empty" eventhou the value is set in the input.
any Ideas?
The disabled attribute does exactly what it states. It disables the html-form-element. This means, that when you sent the form back to your service/controller, that form field will not be posted.
Now my assumption is that you don't want users to edit some data that's displayed in an EditForm? In this case, don't use disabled, better choose readonly for displaying purpose.
On Server-Side, simply ignore what the user is posting. As when it's readonly, the data will still be sent and can be modified by the user using browser-dev-tools ;)
Iam new guy to Zend framework and currently Iam working on Zend2...I want to ask about Translator usage in Zend forms....If i want to use translator i directly using for labels in form view i.e.form_view.php like
$this->formLabel()->setTranslator($translator, 'date_of_birth');
But I want to add the translator at the form only i.e.in src/my_module/Form/UserForm.php
like
$this->add(array(
'name' => 'date_of_birth',
'attributes' => array(
'type' => 'text',
'id' => 'date_of_birth',
),
'options' => array(
'label' => 'DateOfBirth',
), //Here there is any option to put translator
));
Please help me...any answer would be help for me like I asked
Thanks in advance
You don't really need to do that. Since the the Translator that is set up using the factory-key translator will automatically be injected into the Form.
The best approach (in my opinion) is to make extensive use of the translator text_domain:
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
'text_domain' => 'MyModuleTextDomain'
),
),
),
With this setup, the Files of your Module will automatically be inserted into the default TranslatorService which every Zend\Form knows of.
So ultimately all you have to do is make the ViewHelpers know of the TextDomain that you are using. And this is done in the following manner:
$this->formLabel()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formButton()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formElementErrors()->setTranslatorTextDomain('MyModuleTextDomain');
You need to do this once inside your respective view.phtml before(!) using the ViewHelpers like $this->formElement($element) or $this->formCollection($form)
And that's really all there is to it. I recall having seen a discussion somewhere about making it easier to pass along Text-Domain-Data, but i can't find it right now. So things may get a little easier in the future ;) For now, 3 lines are all that's needed though!
above answer is quite unnecessary ... as your translator was added automatically to zend form for rendering form labels and ....
only use this code in your module config :
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
),
),
),
if u use the correct view helpers for rendering form elements (or whole form) it will automatically translated
This is not a recommended approach because forms are translated automatically if you have a translator configured (which you do if you are using the Skeleton Application). However, since you asked how to use the translator directly within your form, I will show you how you can do it. Please carefully consider if you really want to do this, as I cannot imagine a use case where it would be necessary.
To do exactly what you were asking, you can inject the translator into your form. You can do this either in your controller or in a factory. I will be using a factory in this example because it is more DRY.
// In your module's config file
'service_manager' => array(
'factories' => array(
'YourModule\Form\YourForm' => function($sm) {
$translator = $sm->get('Translator');
return new \YourModule\Form\YourForm($translator);
},
),
),
Then in your form class, you can do like this:
namespace YourModule\Form;
class RegisterForm extends \Zend\Form\Form {
public function __construct($translator) {
// Do something
$translated_string = $translator->translate('string to translate');
}
}
Then in your controller, you can do like this:
$your_form = $this->servicelocator->get('YourModule\Form\YourForm');
Or if you don't want to use the factory, you can choose to not add it and do like this instead:
$your_form = new \YourModule\Form\YourForm($this->servicelocator->get('Translator'));
I would recommend going with the factory, though.
Sorry for the many questions but I'm new to Symfony. I made a form and all it working well. I a few fields for pictures (Picture1, Picture2, Picture3, etc). I used te sfWidgetFormInputFile and all is working well with that as well. It uploads the file where it needs to and it gives it a unique name.
The problem I'm having is that those fields are set in mySQL to have a default value of "no-pic.png" so that if the user doesn't upload a picture, it will still have something to show in the post. But every time someone uploads a picture, it deletes my no-pic.png file and the posts without a picture show with a blank square. I'm thinking it's because it's set to "delete previous image" before uploading the new one to reduce excess unused pictures on the server.
I'm wondering if there's a way to have it check to see if the value is "no-pic.png" before deleting it from the folder. In case it helps, this is the code I'm using in the form.class file.
$this->widgetSchema['picture1'] = new sfWidgetFormInputFile(array('label' => 'Pictures', ));
$this->validatorSchema['picture1'] = new sfValidatorFile(array('required' => false, 'path' => sfConfig::get('sf_upload_dir').'/car', 'mime_types' => 'web_images', ));
$this->widgetSchema['picture1'] = new sfWidgetFormInputFileEditable(array(
'label' => 'Pictures',
'file_src' => '/uploads/car/'.$this->getObject()->getPicture1(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div>%file%<br />%input%<br />%delete%%delete_label%</div>',
));
$this->validatorSchema['picture_delete'] = new sfValidatorPass();
Just for reference here is the code I used.
$this->setWidget('logo_image', new sfWidgetFormInputFileEditable(array(
'file_src' => 'uploads/logos/'.$this->getObject()->getFilename(),
'edit_mode' => !$this->isNew()
)));
$validatorOptions = array(
'max_size' => 1024*1024*10,
'path' => sfConfig::get('sf_upload_dir').'/logos',
'mime_type_guessers' => array(),
'required' => false
);
if ($this->isNew())
{
$validatorOptions['required'] = true;
}
$this->setValidator('logo_image', new sfValidatorFile($validatorOptions));
$this->setValidator('logo_image_delete', new sfValidatorString(array('required' => false)));
I found a workaround. I had the page check to see if it was set to no-pic.png and then it displays a no-pic.png in another location. That way when it goes to upload a picture, it won't delete it. :) Thanks for your help though.
I'm using ahDoctrineEasyEmbeddedRelationsPlugin to add dynamic i18n translations to my object, so I wrote this in my object Form class
$this->embedRelations(array(
'Translation' => array(
'considerNewFormEmptyFields' => array('content', 'lang')
));
The result I got is only one input per record, "content".
I've tried this in the FormTranslation class, but no luck:
$this->useFields(array('content', 'lang'));
So what I did was to create a manual doctrine relation with a foreignAlias called "translations", and then:
$this->embedRelations(array(
'translations' => array(
'considerNewFormEmptyFields' => array('content', 'lang')
));
this almost worked, I get the lang field now, but only in the list of existing tranlations, not in the new translation form
Any ideas if I can archieve this? Thanks!
Hm , I always use for example for 'en' and 'uk' culture :
considerNewFormEmptyFields' => array('en','uk')