views:

420

answers:

2

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

+1  A: 

If the form doesn't validate (and send the contact information), then don't redirect. Simply move your redirect into your "if valid" block.

You can still add a field to store the page to return to once the form is completed successfully. You probably need to go about populating it a different way though, otherwise the contact page will be used as the "go to" page when the form is created in the contactus/index action (ie. when the form doesn't validate the first time)

Also, is there a reason you're not using Zend_Mail to actually send the mail? Since you're using the Zend Framework anyway?

Brenton Alker
A: 

Yes, the problem may be is because of my approach was wrong. I used one controller/page. For eg IndexController for index page, Aboutus Controller for about us page etc. the contactus is a small form that included in all these pages and thus the problem. Anyone can please suggest a better method?

riyas kp