Zend Framework 2 - Form Element Decorators

后端 未结 4 693
情话喂你
情话喂你 2020-12-13 22:32

I want to force the Zend form into Twitter Bootstrap style. I currently iterate through the form fields and write the form info into my bootstrap div construction.

I

4条回答
  •  一整个雨季
    2020-12-13 22:49

    I tried Ron's Partial method, the result would like this which not Bootstrap 3 intended.

    ...
    ...

    We know, in order to use bootstrap 3 predefined Form style, we need to define style to input element: form-control, rather than its wrapping element.

    My Partial way is as following.

    echo $this->form()->openTag($form);
    foreach ($form as $element) :?>
        
    getOption('required')) { $req = 'required'; } $type = $element->getAttribute('type'); $name = $element->getAttribute('name'); $label = $element->getLabel(); ?>
    form()->closeTag();

    Well, we could get the result.

    
        ...
        
    ...

    How to attach custom styles into zf2 forms has mentioned : to add class attribute to the Form element.

    class TeaForm extends Form
    {
        public function __construct($name = null)
        {
            // we want to ignore the name passed
            parent::__construct('tea');
    
            $this->add(array(
                'name' => 'id',
                'type' => 'Hidden',
            ));
            $this->add(array(
                'name' => 'brand',
                'type' => 'Text',
                'options' => array(
                    'label' => 'Brand',
                ),
                /** **define class attribute** **/
            'attributes' => array(
                'class' => 'form-control',
            ),
            ));
    ....
    

    It looks quite simple, but, the problem is the input element would be wrapped into the label element, which still not what Bootstrap 3 intended.

    
        
        
    ...
    

    In my opinion, the Partial method is still one flexible and light choice. Tea Box is one ZF2 practice, you could find all above mentioned code and description from Gibhub

提交回复
热议问题