<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pi &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/pi/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>How to Setup Different Error Messages for Each Zend Form Element Validator</title>
		<link>/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/</link>
		<comments>/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 08:25:07 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[0]]></category>
		<category><![CDATA[Elementary arithmetic]]></category>
		<category><![CDATA[Error message]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[measurement]]></category>
		<category><![CDATA[Nothing]]></category>
		<category><![CDATA[Numbers]]></category>
		<category><![CDATA[Pi]]></category>

		<guid isPermaLink="false">/?p=2469</guid>
		<description><![CDATA[Anyone who has worked with that has come across this problem. I&#8217;d like to show different error message on each validator attached to a Zend_Form_Element. Let&#8217;s say we validate an text input field. We want it to contain only digits, but also we&#8217;d like to display different messages when the field is empty and when &#8230; <a href="/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/" class="more-link">Continue reading <span class="screen-reader-text">How to Setup Different Error Messages for Each Zend Form Element Validator</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/06/04/one-form-multiple-db-records/" rel="bookmark" title="One Form &#8211; Multiple DB Records">One Form &#8211; Multiple DB Records </a></li>
<li><a href="/2010/04/09/secure-forms-with-zend-framework/" rel="bookmark" title="Secure Forms with Zend Framework">Secure Forms with Zend Framework </a></li>
<li><a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" rel="bookmark" title="Zend_Validate_Db_RecordExists in Zend Framework 1.10+">Zend_Validate_Db_RecordExists in Zend Framework 1.10+ </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Anyone who has worked with that has come across this problem. I&#8217;d like to show different error message on each validator attached to a <a href="http://framework.zend.com/manual/1.11/en/zend.form.standardElements.html" title="Zend Framework: Standard Form Elements" target="_blank">Zend_Form_Element</a>. Let&#8217;s say we validate an text input field. We want it to contain only digits, but also we&#8217;d like to display different messages when the field is empty and when the user has entered something that is different from digits. </p>
<p>It can be done by attaching to the form element two validators: <a href="http://framework.zend.com/manual/en/zend.validate.set.html#zend.validate.set.digits" title="Zend Framework: Standard Validation Classes, Zend_Validate_Digits" target="_blank">Zend_Validate_Digits</a> and <a href="http://framework.zend.com/manual/en/zend.validate.set.html#zend.validate.set.notempty" title="Zend Framework: Standard Validation Classes, Zend_Validate_NotEmpty" target="_blank">Zend_Validate_NotEmpty</a>, but first let&#8217;s see how to change the default &#8220;Value is required and can&#8217;t be empty&#8221; error message of a form field.</p>
<pre lang="PHP">
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits');
$form->addElement($element);
</pre>
<p>Here we validate the field with Zend_Validate_Digits and we have set it to be required. Thus everything containing characters, i.e. &#8220;my123name&#8221; or &#8220;007bond&#8221;, will be false, while &#8220;1234&#8221; will be true.</p>
<p><figure id="attachment_2485" style="width: 620px" class="wp-caption alignnone"><a href="/category/zend-framework-on-stoimen-com/"><img src="/wp-content/uploads/2011/11/zend-framework-logo.png" alt="Zend Framework" title="zend-framework-logo" width="620" height="169" class="size-full wp-image-2485" /></a><figcaption class="wp-caption-text">To show different error messages you&#039;ve to attach them per validator and not per form element!</figcaption></figure><br />
 <span id="more-2469"></span><br />
First of all this field is set to be required with the line ->setRequired(true), so we cannot submit the form if the input is empty and we&#8217;ll receive the default error message &#8220;Value is required and can&#8217;t be empty&#8221;. The question is how to change this default message, because as you know sometimes you&#8217;d like to say something different to your users or you&#8217;d like to display error messages on a language different from English. Here&#8217;s how.</p>
<pre lang="PHP">
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits')
	->addErrorMessage('Please, type your phone here!');
$form->addElement($element);
</pre>
<p>Now the error message is changed from &#8220;Value is required and can&#8217;t be empty&#8221; to &#8220;Please, type your phone here!&#8221;. </p>
<p>The problem is that when you add more than one validator to a form field you can still show one message regardless of the validator that has failed.</p>
<pre lang="PHP">
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('NotEmpty', true)
	->addValidator('Digits', true)
	->addErrorMessage('Please, type your phone here!');
$form->addElement($element);
</pre>
<p>In this case whenever the field is empty or it contains something different from digits, the message shown to the user will be &#8220;Please, type your phone here!&#8221;. The question is can we show different error messages on every validator. This should look something like &#8220;The field cannot be empty!&#8221; when the field is empty and &#8220;Please, enter only digits!&#8221; when the user has entered something into the field, but it doesn&#8217;t contain only digits.</p>
<h2>The Solution</h2>
<p>Actually we have to attach error messages <em><strong>per validator</strong></em>, and not on a form element. Here&#8217;s how it can be done.</p>
<pre lang="PHP">
$notEmpty = new Zend_Validate_NotEmpty();
$notEmpty->setMessage('The field cannot be empty!');

$digits = new Zend_Validate_Digits();
$digits->setMessage('Please, enter only digits');

$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone:')
	->setRequired(true)
	->addValidator($notEmpty, true)
	->addValidator($digits, true);
$form->addElement($element);
</pre>
<p>Note that we set to &#8220;true&#8221; the second parameter of addValidator. This is important because this way we break the validator&#8217;s chain and when the validation fails on NotEmpty the framework stops the validation of that field against the other validators.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/06/04/one-form-multiple-db-records/" rel="bookmark" title="One Form &#8211; Multiple DB Records">One Form &#8211; Multiple DB Records </a></li>
<li><a href="/2010/04/09/secure-forms-with-zend-framework/" rel="bookmark" title="Secure Forms with Zend Framework">Secure Forms with Zend Framework </a></li>
<li><a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" rel="bookmark" title="Zend_Validate_Db_RecordExists in Zend Framework 1.10+">Zend_Validate_Db_RecordExists in Zend Framework 1.10+ </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
