How can i create a new zend Form attribute - forms

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'
]);

Related

Using Laravel to Add Dynamic Attributes to Form Fields

I am trying to create a dynamic value for a form attribute that is auto-populated based on a previous setting stored in the database. It works fine in HTML with a little Laravel and looks like:
<input type="text" class="class" id="firstName" placeholder="First Name" value="{{ $user->firstName }}">
But I want to fully generate the entire form in Laravel. I'm unsure how to pass the value into the array. I can't seem to get the form to pull the information. Here is how it is currently looking:
{{ Form::text('first_name', '', [
'class' => 'class',
'id' => 'firstName',
'placeholder' => 'First Name',
'value' => $user->firstName
])}}
Try this:
{{ Form::text('first_name', $user->firstName, [
'class' => 'class',
'id' => 'firstName',
'placeholder' => 'First Name'
])}}
For more information regarding this topic, visit this
See, if that works.
To specify a default value in laravel's form generator, the second value you pass in is made for you:
{{ Form::text('first_name', $user->firstName,
[
'class' => 'class',
'id' => 'firstName',
'placeholder' => 'First Name',
]
) }}
Please note that from laravel 5, being published next week, the form helpers are removed (and for the actual state the installable replacement packages don't work very well/without bugs). So if you are planning on upgrading to laravel 5 better don't use this, instead go with html form elements.

How to add some text in textarea in Zend_Form?

How put information in the textarea tag in Zend_Form? For example:
<textarea name="descNews" id="descNews" rows="5" class="txt_meta" cols="80">This is text!</textarea>
Zend form:
$description = new Zend_Form_Element_Textarea('descNews');
$description->addFilter('StripTags')
->setAttribs(array(
'rows' => '5',
'class' => 'txt_meta'
));
you can use setValue(mixed $value)
$description = new Zend_Form_Element_Textarea('descNews');
$description->addFilter('StripTags')
->setAttribs(array(
'rows' => '5',
'class' => 'txt_meta'
));
$description->setValue('set your default vallue');
Or you can see on zend doc
hope it will work for you.

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
)
));
?>

populate values from data base in select box in 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)

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