Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Form: How to set the length of a text input or textarea element?

Tags:

zend-form

By default Zend Form Text elements don't have a width specified. Textarea elements have a default of rows="24" and cols="80". But when I set a different value...

$body = new Zend_Form_Element_Textarea('body');
$body->setLabel('Body:')
    ->setRequired(true)
    ->setAttrib('COLS', '40')
    ->setAttrib('ROWS', '4');
$this->addElement($body);

...the attributes are only added, not changed:

<textarea name="body" id="body" COLS="40" ROWS="4" rows="24" cols="80">

What is the correct way to specify a width and height of a textarea element, and the column width of a text element?

Solution:

Apparently, you can't specify html attributes in capital letters or else it will add duplicate attributes.

To change the height and width of a text area element:

$textarea = new Zend_Form_Element_Textarea('body');
$textarea
    ->setAttrib('cols', '40')
    ->setAttrib('rows', '4');

To change the width of a text element:

$text = new Zend_Form_Element_Text('subject');
$text->setAttrib('size', '40');
like image 945
Andrew Avatar asked Dec 22 '09 19:12

Andrew


4 Answers

It'll work if you take those attribute names and lowercase'em.

like image 152
Derek Illchuk Avatar answered Nov 07 '22 10:11

Derek Illchuk


Try this:

$text = new Zend_Form_Element_Text('subject');

$text ->setAttrib('maxlength','100');

like image 41
Ram Ch. Bachkheti Avatar answered Nov 07 '22 11:11

Ram Ch. Bachkheti


Using the setAttrib will not affect the stringlength as that attribute is only recognised by text inputs. Try using a validator to control the string length. Note you can also set custom error messages.

$text = new Zend_Form_Element_Textarea( 'body' );
        $text->      ->setLabel('Body:')
                     ->setAttrib('cols', 50)
                     ->setAttrib('rows', 4)
                     ->addValidator('StringLength', false, array(40, 250))
                     ->setRequired( true )
                     ->setErrorMessages(array('Text must be between 40 and 250 characters'));
like image 20
user466764 Avatar answered Nov 07 '22 10:11

user466764


I'm not an expert, but have you tried using lowercase attribute names? It's pretty tacky but if it works it suggests the language is broken in this respect.

like image 28
peter.murray.rust Avatar answered Nov 07 '22 11:11

peter.murray.rust