Reputation: 688
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".
Upvotes: 3
Views: 5924
Reputation: 8519
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.
Upvotes: 1
Reputation: 688
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')),
),
));
Upvotes: 5
Reputation: 1903
Editted
This should do it:
...
$name = $this->getElement('name')->addErrorMessage('Please enter your name');
...
Upvotes: 0