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
Code for contact form is
class Form_Contactus extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->setAction('contactus/index');
$frontController = Zend_Controller_Front::getInstance();
$pageName = $this->createElement('hidden','pageName');
$pageName->setValue( $frontController->getRequest()->getControllerName() );
$FullName = $this->createElement('text','FullName');
$FullName->setLabel('Full Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$Email = $this->createElement('text','Email');
$Email->setLabel('Email')
->setRequired(true)
->addFilter('StringTrim')
->addValidator('EmailAddress')
->addValidator('NotEmpty');
$Message = $this->createElement('textarea','Message');
$Message->setLabel('Message')
->setAttribs( array('rows' => 3, 'cols' => 20 ))
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = $this->createElement('submit','submit');
$submit->setLabel('Submit')
->setIgnore(true);
$this->addElements(array( $pageName,
$FullName,
$Email,
$Message,
$submit, )
);
}
}
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.
It is included in the different pages like index, about etc by code
$conForm = new Form_Contactus();
echo $conForm;
And the controller code looks like as
class ContactusController extends Zend_Controller_Action
{
protected $_redirector = null;
public function init()
{
$registry = Zend_Registry::getInstance();
$this->msgObj = $registry['MessageHandler'];
}
public function indexAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$form = new Form_Contactus();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->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->setSubject('Contact Enquiry.');
$mail->setFrom($Email, $FullName);
$mail->addTo(CONTACT_ADMIN_EMAIL, CONTACT_ADMIN_NAME);
$mail->setBodyText($mailBody);
if( $mail->send() ){
$this->msgObj->addMessage('Thank you!');
$this->msgObj->addMessage('Your message has been received and will be reviewed within 72 hours.');
}
else{
$this->msgObj->addError('Unable to sent mail! Please try later.');
}
}
}
else {
$this->msgObj->addError('Please correct the following:!');
$form->populate($formData);
$pageName = 'index';
}
}
$this->view->form = $form;
$this->_helper->redirector('index', $pageName);
}
}
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
another problem is unable to display messages like ‘'Thank you’ .
this may because of $this->_helper->redirector method I used. The form is redirected and hence lost the values. If I use $this->_helper->forwarded
or $this_forward()
it also doesn’t work.
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