3

I have the following code snippets in my forms/video.php. But i do not know where to add the validation message for required.

$this->addElement('text','name', array(
        'label'=>'Name',
        'maxlength'=>20,
        'class'=>'name',
        'required'=>true,
        'filters'=>array('StringTrim'),
        'decorators'=>array(
            'ViewHelper',
            'Errors',
            array(array('control'=>'HtmlTag'), array('tag'=>'div', 'class'=>'fieldcontrol')),
            array('Label', array('tag'=>'div', 'class'=>'name')),
            array(array('row'=>'HtmlTag'), array('tag' => 'div', 'class'=>'row')),
        )
    ));

Instead of "Value is required and can't be empty", I would like to set it to something else like "Please enter your name".

zen.c
  • 678
  • 8
  • 17
  • 1
    There's a very similar question here: http://stackoverflow.com/questions/459921/zend-form-nicely-change-setrequired-validate-message . You should find some interesting answers there. – dinopmi May 10 '12 at 09:59
  • I understand that it's similar, but in my case, i am adding the entire element, with validation, as arguments into $this->addElement. Is it not possible using the addElement approach? Sorry, as i am new to ZF. – zen.c May 10 '12 at 10:03

3 Answers3

5

In the end, i got this to work:

$this->addElement('text', 'age', array(
            'label'=>'Age',
            'maxlength'=>2,
            'class'=>'age',
            'required'=>true,
            'filters'=>array('StringTrim'),
            'validators'=>array(
                array(
                    'validator'=>'NotEmpty',
                    'options'=>array(
                        'messages'=>'Please enter your age.'
                    ),
                    'breakChainOnFailure'=>true
                ),
                array(
                    'validator'=>'Int',
                    'options'=>array(
                        'messages'=>'Age must be a number.'
                    ),
                    'breakChainOnFailure'=>true
                ),
                array(
                    'validator'=>'between',
                    'options'=>array(
                        'min'=>8,
                        'max'=>10,
                        'messages'=>array(
                            Zend_Validate_Between::NOT_BETWEEN => 'This is for %min% to %max% years old.'
                        )
                    )
                ),

            ),
            'decorators'=>array(
                'ViewHelper',
                'Errors',
                array(array('control'=>'HtmlTag'), array('tag'=>'div', 'class'=>'fieldcontrol')),
                array('Label', array('tag'=>'div', 'class'=>'age')),
                array(array('row'=>'HtmlTag'), array('tag' => 'div', 'class'=>'row')),
            ),
        ));
zen.c
  • 678
  • 8
  • 17
1

I would likely leave the required set to default and use the NotEmpty validator instead.

$this->addElement('text', 'age', array(
            'label'=>'Age',
            'maxlength'=>2,
            'class'=>'age',
            'filters'=>array('StringTrim'),
            'validators'=>array(
                array(
                    'validator'=>'Int',
                    'options'=>array(
                        'messages'=>'Age must be a number.'
                    )
                ),
                array(
                    'validator'=>'NotEmpty',
                    'options'=>array(
                        'messages'=>'Please enter your age.'
                    )
                ),
                array(
                    'validator'=>'between',
                    'options'=>array(
                        'min'=>8,
                        'max'=>10,
                        'messages'=>array(
                            Zend_Validate_Between::NOT_BETWEEN => 'Your age must be between %min% to %max%.'
                        )
                    )
                )
            ),
            'decorators'=>array(
                'ViewHelper',
                'Errors',
                array(array('control'=>'HtmlTag'), array('tag'=>'div', 'class'=>'fieldcontrol')),
                array('Label', array('tag'=>'div', 'class'=>'age')),
                array(array('row'=>'HtmlTag'), array('tag' => 'div', 'class'=>'row')),
            ),
        ));

NotEmpty() does essentially the same thing as isRequired() but is an actual validator where isRequired() is just a flag set in Zend_Form_Element. Also it should'nt mess up your messages.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
0

Editted

This should do it:

...
$name = $this->getElement('name')->addErrorMessage('Please enter your name');
...
amburnside
  • 1,943
  • 5
  • 24
  • 43
  • If you are refering to $this->addElement(...)->addErrorMessage('Error message'), that wont work as $this->addElement return the form itself and not the element. – zen.c May 10 '12 at 10:14