populate values from data base in select box in zend framework - zend-framework

I am new in zend framework. I want to populate company name and company id from database to select box. in zend
example as we do it PHP
<option value="<?php echo id ?>" > <?php echo $comapnyName ?>
this is my form
$this->addElement('select', 'companyName', array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:103px'),
'decorators'=> array(
'ViewHelper','Errors'
)
));
help me

The parameter name is multiOptions and requires an array with the value=>companyName pairs for your option elements.
So either add it to your statement above when you create the element or later with $element->setMultiOptions($options)

Related

How can i create a new zend Form attribute

How can i add a new personalized attribute with zend From, exemple <input type='text' **ref='name**' name='id_commande' id='commande'/>
Use setAttrib on element for single, or setAttribs for multiple attributes you want to add
This should make a job:
$id_commande = new Zend_Form_Element_Text('id_commande');
$id_commande->setAttribs([
'ref' => 'name',
'class' => 'your_class',
'style' => 'width: 150px'
]);

how to add form id in cakephp when creating a form

i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>

Cakephp form creation gives me missing database table

According to the manual, I should be able to do this which is to leave Model as null
<?php
echo $this->Form->create(null, array('url' => '/recipes/add'));
// or
echo $this->Form->create(null, array(
'url' => array('controller' => 'recipes', 'action' => 'add')
));
But in reality, I got error saying missing database table? Why?
I have my Model which is not directly mapping to any table. Why can't I leave it as null? Something like this:
<?php echo $this->Form->create(null,array('url' => array('my_account','action' => 'change_avatar'),'type' => 'file'));?>
Try passing false for the model instead of null:
<?php
echo $this->Form->create(false, array('url' => '/recipes/add'));
// or
echo $this->Form->create(false, array(
'url' => array('controller' => 'recipes', 'action' => 'add')
));
From the manual:
You can also pass false for $model. This will place your form data into the array: $this->request->data (instead of in the sub-array: $this->request->data['Model']). This can be handy for short forms that may not represent anything in your database.
make a table with name recipes in your database. The submitted date from the form will be saved at recipes table.

cakephp link outside form which access form input values

I have this form with a date input.
echo $this->Form->create('Nodata');
echo $this->Form->input('date1', array('type' => 'date', 'label' => 'From:'));
echo $this->Form->input('date2', array('type' => 'date', 'label' => 'To:'));
echo $this->Form->end('Get Hours');
When the form is submitted, I'm showing the results in the same view, below the form.
My problem is I have a link that is not part of the form and need to read the value (in the view) from the date field on the form to use it as a param
on this link.
// date1 is the param I need to take the value from date input
<th> <?php echo $this->Html->link(__('Agents Detail'), array('controller' => 'qcas', 'action' => 'hours', 'paramProject' => $hour['Qca']['dir_id'], 'date1' => $this->data)); ?> </th>
Note this link is outside the form and I need a way to read a input on the form to use as param in my link.
Instead of just using $this->data for your date1 element, you need to refer the field in the $this->data object.
CakePHP < 2.0
'date1' => $this->data['Nodata']['date1']
CakePHP 2.0+
'date1' => $this->request->data['Nodata']['date1']
I'm not sure what you're trying to do at the destination link, but you may need to format the date as well:
'date1' => date('Y-m-d', $this->request->data['Nodata']['date1']) // you may need strtotime

How to validate and render dynamically extensible zend form

i need help with my dynamically extensible zend form.
I have form with subform, which contains two elements:
<form>
<fieldset class="itemGroup">
<label>
Question
<input type="text" name="items[questions][]" value="">
</label>
<label>
Answer
<input type="text" name="items[answers][]" value="">
</label>
</fieldset>
</form>
I obtained it with following procedure:
$itemsSubform = new Zend_Form_SubForm();
$form->addSubForm($itemsSubform, 'items');
$itemsSubform->setElementsBelongTo('items');
$itemQuestion = new Zend_Form_Element_Text('questions', array(
'label' => 'Question',
'isArray' => true,
'filters' => array(
'stringTrim',
),
'validators' => array(
array('stringLength', array('max' => 255)),
),
));
$itemAnswer = new Zend_Form_Element_Text('answers', array(
'label' => 'Answer',
'isArray' => true,
'filters' => array(
'stringTrim',
),
'validators' => array(
array('stringLength', array('max' => 255)),
),
));
$itemsSubform->addDisplayGroup(array($itemQuestion, $itemAnswer), 'itemGroup');
If is needed, i just copy all fieldset for extend form by javascript.
All is working correct, until i submit form. During validation is no element validated, and during rendering form, populated by such data, i get error message from class Zend_View_Abstract that escaping value is array instead of string (this method is called during rendering element for escape its value).
For compltion, if i call $form->getValues(); after validation, (by javascript another fieldset is added) i get this:
Array
(
[items] => Array
(
[questions] => Array
(
[0] => lorem
[1] => dolor
)
[answers] => Array
(
[0] => ipsum
[1] => sit
)
)
)
Could someone advise me how to behave to form? The ideal solution would be when form themselves validate each value separately and find how many times should he render fieldset (displayGroup).
First your elements fail to validate because you set isArray => TRUE and the validator you chose evaluates strings.
Next you issue with populating the form I believe is likely due to the fact that you need to supply the data to populate in the same multidimensional array as the form produces (the arrays should map one to one).
The link below is to an example of how to build a form dynamically, you should be able to use it as a template to produce one to fill your needs.
Example of generating form elements with loop