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

后端 未结 5 1830
悲&欢浪女
悲&欢浪女 2021-02-05 05:20

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

相关标签:
5条回答
  • 2021-02-05 05:49

    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.

    0 讨论(0)
  • 2021-02-05 05:54

    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'));
    
    0 讨论(0)
  • 2021-02-05 05:56

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

    0 讨论(0)
  • 2021-02-05 05:57

    Try this:

    $text = new Zend_Form_Element_Text('subject');

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

    0 讨论(0)
  • 2021-02-05 05:57

    Generally it is good practice to add your form attributes in your fieldset class (or form class depending on how you have set it up).

    Here is an example:

    class SomeFieldSet extends Fieldset
    {
        /**
         * @var \Doctrine\Common\Persistence\ObjectManager
         * @access protected
         */
        protected $objectManager;
    
        /**
         * @param ObjectManager $objectManager
         * @param SomeEntity $claimPrototype
         * @param null $name
         * @param array $options
         */
        public function __construct(
            ObjectManager $objectManager,
            SomeEntity $somePrototype,
            $name = null,
            $options = array()
        ) {
            parent::__construct($name, $options);
    
            $this->objectManager = $objectManager;
    
            $this->setHydrator(new DoctrineObject($objectManager));
            $this->setObject($somePrototype);
    
        }
    
        public function init()
        {
    
            $this->add(
                [
                    'name'       => 'description',
                    'type'       => 'textarea',
                    'options'    => [
                        'label' => 'Some Label',
                        'instructions' => 'Some instruction',
                    ],
                    'attributes' => [
                        'class' => 'form-control',
                        'placeholder' => 'Some placeholder',
                        'required' => 'required',
                        'rows' => 10
                    ],
                ]
            );
    
    }
    
    0 讨论(0)
提交回复
热议问题