Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zf2 annotation for select element

I'm getting a bit confused with zf2 annotations, I created a few elements based on this tutorial:

/**
 * @Annotation\Attributes({"type":"text" })
 * @Annotation\Required(false)
 * @Annotation\Options({"label":"Cardholder's Name: *:"})
 */
protected $cardholder;

For simple text all is working fine but I'm stuck when try to create a select element.

If you know any tutorial or github repo please let me know.

like image 727
Nikolai Senkevich Avatar asked Dec 26 '22 21:12

Nikolai Senkevich


2 Answers

Problem was in view so to get select you need
added example for validation and filtering

/**
* @Annotation\Attributes({"type":"text" })
* @Annotation\Options({"label":"Cardholder's Name: *:"})
* @Annotation\Required(false)
* @Annotation\Filters({"name":"StripTags"},{"name":"StringTrim"}}) 
* @Annotation\Validator({"name":"StringLength","options":{"min":"1", "max":"20"}})
*/

protected $cardholder;

/**
 * @Annotation\Type("Zend\Form\Element\Select")
 * @Annotation\Options({"label":"Description"})
 * @Annotation\Attributes({"options":{"1":"Visa","2":"Maestro"}})
 */
protected $cardType;

and in view

<dt><?php echo $this->formLabel($form->get('cardholder')); ?></dt>
<dd><?php 
echo $this->formInput($form->get('cardholder'));
echo $this->formElementErrors($form->get('cardholder'));
?></dd>

<dt><?php echo $this->formLabel($form->get('cardType')); ?></dt>
<dd><?php 
echo $this->formSelect($form->get('cardType'));
echo $this->formElementErrors($form->get('cardType'));
?></dd>
like image 68
Nikolai Senkevich Avatar answered Jan 09 '23 13:01

Nikolai Senkevich


Try this:

/**
 * @Annotation\Type("Zend\Form\Element\Select")
 * @Annotation\Required(false)
 * @Annotation\Options({"label":"Cardholder's Name: *:", "value_options":{"1":"VISA", "2":"MASTER CARD", "3":"AMERICAN EXPRESS"}})
 */
protected $cardholder;
like image 39
Andrew Boyko Avatar answered Jan 09 '23 15:01

Andrew Boyko