Note that there are some explanatory texts on larger screens.

plurals
  1. POzend_Form:display validation and error messages
    primarykey
    data
    text
    <p>I have a common ‘Contact Form’ in my project which is called in most of pages like index, about us etc. this page accept user inputs and sent an email to admin, then return back to the page, from which it is called</p> <p>Code for contact form is </p> <pre><code>class Form_Contactus extends Zend_Form { public function init() { $this-&gt;setMethod('post'); $this-&gt;setAction('contactus/index'); $frontController = Zend_Controller_Front::getInstance(); $pageName = $this-&gt;createElement('hidden','pageName'); $pageName-&gt;setValue( $frontController-&gt;getRequest()-&gt;getControllerName() ); $FullName = $this-&gt;createElement('text','FullName'); $FullName-&gt;setLabel('Full Name') -&gt;setRequired(true) -&gt;addFilter('StripTags') -&gt;addFilter('StringTrim') -&gt;addValidator('NotEmpty'); $Email = $this-&gt;createElement('text','Email'); $Email-&gt;setLabel('Email') -&gt;setRequired(true) -&gt;addFilter('StringTrim') -&gt;addValidator('EmailAddress') -&gt;addValidator('NotEmpty'); $Message = $this-&gt;createElement('textarea','Message'); $Message-&gt;setLabel('Message') -&gt;setAttribs( array('rows' =&gt; 3, 'cols' =&gt; 20 )) -&gt;setRequired(true) -&gt;addFilter('StripTags') -&gt;addFilter('StringTrim') -&gt;addValidator('NotEmpty'); $submit = $this-&gt;createElement('submit','submit'); $submit-&gt;setLabel('Submit') -&gt;setIgnore(true); $this-&gt;addElements(array( $pageName, $FullName, $Email, $Message, $submit, ) ); } } </code></pre> <p>Please note that, the line $this->setAction('contactus/index');. My idea is, if I fill this form (note it is a common form) from index page, it pass through 'contactus controller’ index action. Sent a mail from there and return back to index page. If the page is filled from about us page, it return back to about us page.</p> <p>It is included in the different pages like index, about etc by code</p> <pre><code>$conForm = new Form_Contactus(); echo $conForm; </code></pre> <p>And the controller code looks like as</p> <pre><code>class ContactusController extends Zend_Controller_Action { protected $_redirector = null; public function init() { $registry = Zend_Registry::getInstance(); $this-&gt;msgObj = $registry['MessageHandler']; } public function indexAction() { $this-&gt;_helper-&gt;layout()-&gt;disableLayout(); $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(); $form = new Form_Contactus(); if ($this-&gt;_request-&gt;isPost()) { $formData = $this-&gt;_request-&gt;getPost(); if ($form-&gt;isValid($formData)) { $pageName = $formData['pageName']; $FullName = $formData['FullName']; $Email = $formData['Email']; $Message = $formData['Message']; if( strlen(trim( $FullName) ) ){ $mailBody .= "Name:\r\n\t".$FullName ."\r\n\r\n"; $mailBody .= "Email:\r\n\t".$Email ."\r\n\r\n"; $mailBody .= "Message:\r\n\t".$Message ."\r\n\r\n"; $mail = new Zend_Mail(); $transport = new Zend_Mail_Transport_Smtp('localhost'); Zend_Mail::setDefaultTransport($transport); $mail-&gt;setSubject('Contact Enquiry.'); $mail-&gt;setFrom($Email, $FullName); $mail-&gt;addTo(CONTACT_ADMIN_EMAIL, CONTACT_ADMIN_NAME); $mail-&gt;setBodyText($mailBody); if( $mail-&gt;send() ){ $this-&gt;msgObj-&gt;addMessage('Thank you!'); $this-&gt;msgObj-&gt;addMessage('Your message has been received and will be reviewed within 72 hours.'); } else{ $this-&gt;msgObj-&gt;addError('Unable to sent mail! Please try later.'); } } } else { $this-&gt;msgObj-&gt;addError('Please correct the following:!'); $form-&gt;populate($formData); $pageName = 'index'; } } $this-&gt;view-&gt;form = $form; $this-&gt;_helper-&gt;redirector('index', $pageName); } } </code></pre> <blockquote> <p>everything works fine and mail is gone if I fill this form except that the form is not validated. For example the mail may sent without ‘FullName’, which is a required field </p> </blockquote> <p>another problem is unable to display messages like ‘'Thank you’ . </p> <p>this may because of $this->_helper->redirector method I used. The form is redirected and hence lost the values. If I use <code>$this-&gt;_helper-&gt;forwarded</code> or <code>$this_forward()</code> it also doesn’t work. </p> <p>Any one can please suggest a method for me to dipsply validation message and other messages properly? Sorry for my poor English and thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload