jcropp
jcropp

Reputation: 1246

How to set the attributes of the zf2 formTextarea helper in a view

I am developing an edit form for a zf2 project. I have an entity for the data and a fieldset for the entity. An element in the fieldset is defined as follows:

    $this->add(array(
        'name' => 'headlineText',
        'type' => 'Zend\Form\Element\Textarea',
        'attributes' => array(
            'type'  => 'text',
        ),
        'options' => array(
            'label' => 'text',
        ),
    ));

In the view, the element is rendered as follows:

$hfs=$form->get('headline-fieldset');
$headlineText = $hfs->get('headlineText');

      ...

      $this->formTextarea($headlineText),

If I want to change the attributes of the element, I can do so in the fieldset:

    $this->add(array(
        'name' => 'headlineText',
        'type' => 'Zend\Form\Element\Textarea',
        'attributes' => array(
            'type'  => 'text',
            'rows'  => 10,    // THIS CHANGES THE NUMBER OF ROWS
        ),
        'options' => array(
            'label' => 'text',
        ),
    ));

If I want to change the attributes of the element, but make those changes in the view, I would assume that that it would go like this:

$hfs=$form->get('headline-fieldset');
$headlineText = $hfs->get('headlineText');
$headlineText->setAttrib('rows', 15); // TO CHANGE THE NUMBER OF ROWS

      ...

      $this->formTextarea($headlineText),

However, this returns:

Fatal error: Call to undefined method Zend\Form\Element\Textarea::setAttrib()

How do I set the element attributes the view?

Upvotes: 0

Views: 1199

Answers (1)

lku
lku

Reputation: 1742

I think the error message is pretty clear, there is no setAttrib method in ZF2. You probably mistaken it with ZF1 element package. In ZF2 it is setAttribute method.

Example:

$headlineText->setAttrib('rows', 15); // Wrong
$headlineText->setAttribute('rows', 15); // Correct

Upvotes: 1

Related Questions