zf2 create select/drop down box and populate options in controller? - zend-framework2

To Create a text input box I used folling code in zend framework2
use Zend\Form\Form;
class Loginform extends Form
{
public function __construct()
{
$this->add(array(
'name' => 'usernames',
'attributes' => array(
'id' => 'usernames',
'type' => 'text',
),
'options' => array(
'label' => 'User Name',
),
));
}
}
and I can populate the values in controller action using
$form = new Loginform();
$form->get('usernames')->setAttribute('value', 'user 1');
Any idea how can I do the same for Selection/drop down box in zf2?
Ref: zend framework 2 documentation

Check the API (the docs are terrible, so check the code).
Use the Zend\Form\Element\Select class and set the options attribute like so:
$element->setAttribute('options', array(
'key' => 'val',
...
));
Output the element using the FormRow or FormSelect view helper.
This site is also a good source for examples and information: http://zf2.readthedocs.org/en/latest/modules/zend.form.quick-start.html
Example:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'usernames',
'attributes' => array(
'id' => 'usernames',
'options' => array(
'test' => 'Hi, Im a test!',
'Foo' => 'Bar',
),
),
'options' => array(
'label' => 'User Name',
),
));
You can also assign the options in the controller if you need to, as shown above.

$form = new Loginform();
$form->get('usernames')->setValueOptions($usernames );
$usernames is an array
Ref Click Here

Zend Framework 2.2 , select options have been moved into 'options' instead of 'attributes' so above code will be changed too
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'usernames',
'attributes' => array(
'id' => 'usernames'
),
'options' => array(
'label' => 'User Name',
'options' => array(
'test' => 'Hi, Im a test!',
'Foo' => 'Bar',
),
),
));

If you want to do it in controller then do it like this way
$form->get('ELEMENT_NAME')->setAttribute('options' ,array('KEY' => 'VALUE'));

Related

ZF2 add styles to elements

Hi is there a way to add style to a form element, besides adding a class?
$this->add(array(
'type' => 'Zend\Form\Element\Textarea',
'name' => 'notas',
'options' => array(
'label' => 'Notas',
'label_attributes' => array(
'class' => 'label-wrapped'
),
),
));
thanks!
This is the way, But I still don't know how to add several styles
$this->add(array(
'type' => 'Zend\Form\Element\Textarea',
'name' => 'notas',
'attributes' => array(
'style'=>'width:100px',
),
'options' => array(
'label' => 'Notas',
'label_attributes' => array(
'class' => 'label-wrapped'
),
),
));

ZendFramework 2: No database adapter present

I have this class:
<?php
class RegisterFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'email1',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'email2',
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'table' => 'user',
'field' => 'email',
'messages' => array(
'recordFound' => "Email already exist ... ! <br>",
),
),
),
),
));
}
}
?>
I get this error: No database adapter present. Any ideas why this happens?
It would be good if you'd read the documentation about Zend\Validator\Db\Record*. The given error message means exactly what it said. You don't provide a DB-Adapter inside the Validator.
From the DOCs:
$validator = new Zend\Validator\Db\RecordExists(
array(
'table' => 'users',
'field' => 'emailaddress',
'adapter' => $dbAdapter
)
);
If you want to find out how to get the DB-Adapter into your Form, i have written a Blog Article about said topic.

In Zend Framework 2, how do you change the message in the DateStep validator

In \Zend\Validator\DateStep, I want to override the error message shown below:
protected $messageTemplates = array(
self::NOT_STEP => "The input is not a valid step"
);
I have an input filter connected to a form containing an element of the type 'Zend\Form\Element\Date' which automatically calls the DateStep validator.
Here is the relevant part of my form:
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
Here is my input filter:
$inputFilter->add($factory->createInput(array(
'name' => 'appointment-date',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'DateStep',
'options' => array(
//'step' => new DateInterval("P2D"),
//'baseValue' => new DateTime(),
'messages' => array(
\Zend\Validator\DateStep::NOT_STEP => 'Must be a day in the future',
),
),
),
),
)));
The inputFilter seems to be ignored. I tried setting up the step and baseValue in the inputFilter, but that seems to not work at all. Working app can be found here: https://github.com/bickerstoff/zf2_datetest if more details are needed.
Looks like you're trying to filter a "Date" input with a "DateStep" validator.
$this->add(array(
'type' => 'Zend\Form\Element\Date',
'name' => 'appointment-date',
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d'
),
'attributes' => array(
'min' => date('Y-m-d'), // today's date
'max' => '2020-01-01',
'step' => '2', // days; default step interval is 1 day
)
));
This may cause your problem.

ZF2 Dynamic Form Filters/Validators

I'm setting up a controller that will create a form.
I can't use an extended to Form class, so i need to build up my form on my controller.
$form = new Form('example');
$fieldset = new Fieldset('default');
$fieldset->add(array('name' => 'example_field', 'attributes' => array('type' => 'text', 'id' => 'example_field'), 'options' => array('label' => 'Example Field',),));
$form->add($fieldset);
The main question here is, how do I define the filters and validators for each element/fieldset without required to create a class implementing InputFilterAwareInterface, so i can do everything from my controller?
Thanks in advance!
You could add/remove form Validators by handle InputFilter of form, here is my example:
$form = new \Zend\Form\Form();
$name = array(
'name' => 'username',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
);
$form->add($name);
$filter = $form->getInputFilter();
$filter->remove('username');
$filter->add(array(
'name' => 'username',
'required' => true,
'validators' => array (
'stringLength' => array (
'name' => 'StringLength',
'options' => array (
'max' => '3',
),
),
),
));
$form->setInputFilter($filter);
$form->setData(array(
'username' => 'longtext',
));
$form->prepare();
echo $form->isValid(); //false
print_r($form->getMessages()); //stringLengthTooLong error will show
Add validator dynamically:
$form->getInputFilter()->get('element_name')->getValidatorChain()->attach(new ValidatorClassName());
Add filter dynamically:
$form->getInputFilter()->get('element_name')->getFilterChain()->attach(new FilterClassName());

How can I insert an id or class attribute into an option of a select statement in the form of ZF2?

Currently, I have this select code in ZF2. Works great, have no issues, BUT! I want to put an id or class attribute into one of the options. Is that possible currently in ZF2?
$this->add(
array(
'name' => 'number_of_rooms',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Number of Rooms',
'options' => array(
array(
'value' => '',
'label' => '- number of rooms -',
'disabled' => true,
),
array(
'value' => 'one bedrooms,
'label' => '1 Bedrooms',
),
array(
'value' => 'two bedrooms',
'label' => '2 Bedrooms',
),
array(
'value' => 'three bedrooms',
'label' => '3 Bedrooms',
),
),
),
'attributes' => array(
'id' => 'number_of_rooms',
),
)
);
You would need to do two things. First would be to write your own SelectElement extending Zend\Form\Element\Select and then you'd need to also write your own ViewHelper rendering the Select, easiest extending Zend\Form\View\Helper\FormSelect
With the inbuilt Select the only valid options are:
//FormSelect.php #43-48
protected $validOptionAttributes = array(
'disabled' => true,
'selected' => true,
'label' => true,
'value' => true,
);
You can check this from the classes linked above.

Resources