<p>When using a <code>Zend_Form</code>, the only way to validate that an input is not left blank is to do</p>

<pre><code>$element-&gt;setRequired(true);
</code></pre>

<p>If this is not set and the element is blank, it appears to me that validation is not run on the element.</p>

<p>If I do use <code>setRequired()</code>, the element is automatically given the standard NotEmpty validator. The thing is that the error message with this validator sucks, "Value is empty, but a non-empty value is required". I want to change this message. At the moment I have done this by changing the <code>Zend_Validate_NotEmpty</code> class, but this is a bit hacky.</p>

<p>I would ideally like to be able to use my own class (derived from <code>Zend_Validate_NotEmpty</code>) to perform the not empty check.</p>

[{'answer_id': 34862, 'author': 'mk.', 'author_id': 1797, 'author_profile': 'https://Stackoverflow.com/users/1797', 'pm_score': 1, 'selected': False, 'text': '<p>Change the <a href="http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.validation.errors" rel="nofollow noreferrer">error message</a>.</p>\n'}, {'answer_id': 34921, 'author': 'Robin Barnes', 'author_id': 1349865, 'author_profile': 'https://Stackoverflow.com/users/1349865', 'pm_score': 0, 'selected': False, 'text': '<p>As far as I can see <a href="http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.validation.errors/" rel="nofollow noreferrer">Changing the error message</a> has no way of changing the message of a specific error. Plus the manual makes it look like like this is a function belonging to Zend_Form, but I get method not found when using it on an instance of Zend_Form.</p>\n\n<p>And example of the useage would be really great.</p>\n'}, {'answer_id': 35894, 'author': 'crono', 'author_id': 1462, 'author_profile': 'https://Stackoverflow.com/users/1462', 'pm_score': 3, 'selected': True, 'text': '<p>I did it this way (ZF 1.5):</p>\n\n<pre><code>$name = new Zend_Form_Element_Text(\'name\');\n$name-&gt;setLabel(\'Full Name: \')\n     -&gt;setRequired(true)\n     -&gt;addFilter(\'StripTags\')\n     -&gt;addFilter(\'StringTrim\')\n     -&gt;addValidator($MyNotEmpty);\n</code></pre>\n\n<p>so, the addValidator() is the interesting part. The Message is set in an "Errormessage File" (to bundle all custom messages in one file):</p>\n\n<pre><code>$MyNotEmpty = new Zend_Validate_NotEmpty();\n$MyNotEmpty-&gt;setMessage($trans-&gt;translate(\'err.IS_EMPTY\'),Zend_Validate_NotEmpty::IS_EMPTY);\n</code></pre>\n\n<p>hope this helps...</p>\n'}, {'answer_id': 191889, 'author': 'Toxygene', 'author_id': 8428, 'author_profile': 'https://Stackoverflow.com/users/8428', 'pm_score': 2, 'selected': False, 'text': "<p>By default, setRequired(true) tells isValid() to add a NonEmpty validation <em>if one doesn't already exist</em>. Since this validation doesn't exist until isValid() is called, you can't set the message.</p>\n\n<p>The easiest solution is to simply manually add a NonEmpty validation before isValid() is called and set it's message accordingly.</p>\n\n<pre><code>$username = new Zend_Form_Element_Text('username');\n$username-&gt;setRequired(true)\n         -&gt;addValidator('NotEmpty', true, array('messages' =&gt; array('isEmpty' =&gt; 'Empty!')));\n</code></pre>\n"}, {'answer_id': 318213, 'author': 'duma', 'author_id': 245644, 'author_profile': 'https://Stackoverflow.com/users/245644', 'pm_score': 2, 'selected': False, 'text': "<p>Add a NotEmpty validator, and add your own message:</p>\n\n<pre><code>// In the form class:\n$username = $this-&gt;createElement('text', 'username');\n$username-&gt;setRequired();  // Note that this seems to be required!\n$username-&gt;addValidator('NotEmpty', true, array(\n    'messages' =&gt; array(\n        'isEmpty' =&gt; 'my localized err msg')));\n</code></pre>\n\n<p><strong>Note</strong> that the NotEmpty validator doesn't seem to be triggered unless you've also called setRequired() on the element.</p>\n\n<p>In the controller (or wherever), call $form->setTranslator($yourTranslator) to localize the error message when it's printed to the page.</p>\n"}]
['https://Stackoverflow.com/questions/34848', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1349865/']