0

I am creating a custom contact form (so that my site will have two separate contact forms). I used the tutorial here

However my issue is I need to set some of the form inputs to require a value.

I added code that I thought would make it work (adding class="required-entry input-text validation-failed" to each of the form inputs) however my form still allows me to submit it without entering any values in my required fields. I am not sure what I need to change/add to my below code

Here is my form.phtml code

     <div class="box simple_contact">
        <div id="messages_product_view">
           <?php echo $this->getChildHtml('global_messages') ?>
        </div>
        <form id="simple_contact_form" name="simple_contact_form" action="<?php echo $this->getUrl('inchoo-simplecontact/') ?>index/sendemail" method="post">

       <fieldset class="group-select">
         <ul>
           <li>
            <div class="input-box">
                <label for="name">Name<span class="required">*</span></label><br />

                <input name="name" id="name" title="Name" value="" class="required-entry input-text validation-failed" type="text" />
                <div class="validation-advice" id="advice-required-entry-name">This is a required field.</div>
            </div>

            <div class="input-box">
                <label for="email">Email<span class="required">*</span></label><br />
                <input name="email" id="email" title="Email" value="" class="required-entry input-text validate-email validation-failed" type="text" />
                <div class="validation-advice" id="advice-required-entry-email">This is a required field.</div>
            </div>
            <div class="input-box">
                <label for="phone">Phone<span class="required">*</span></label><br />
                <input name="phone" id="phone" title="Phone" value="" class="required-entry input-text validation-failed" type="text" />
                <div class="validation-advice" id="advice-required-entry-phone">This is a required field.</div>
            </div>

            <div class="input-box">
                <label for="businessname">Business Name<span class="required">*</span></label><br />

                <input name="company" id="company" title="Company" value="" class="required-entry input-text validation-failed" type="text" />
                 <div class="validation-advice" id="advice-required-entry-company">This is a required field.</div>
            </div>

            <div class="clear"></div>

            <div class="input-box">
               <label for="comment">Question/Comments</label><br />

                <textarea name="comment" id="comment" title="Comment" class="required-entry input-text" style="height:100px;" cols="50" rows="3"></textarea>
            </div>
            </li>
            </ul>
</fieldset>
<div class="button-set">
    <p class="required">* Required Fields</p>
    <button class="button" type="submit"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>

</div>
<br /><br />
 </form>

 </div>

Then my IndexController.php file

    class Inchoo_SimpleContact_IndexController extends Mage_Core_Controller_Front_Action
     {
      public function indexAction()
       {
       //Get current layout state
        $this->loadLayout();   

       $block = $this->getLayout()->createBlock(
         'Mage_Core_Block_Template',
         'inchoo.simple_contact',
         array(
             'template' => 'marketingform/simple_contact.phtml'
          )
       );

    $this->getLayout()->getBlock('content')->append($block);
    //$this->getLayout()->getBlock('right')->insert($block, 'catalog.compare.sidebar', true);

    $this->_initLayoutMessages('core/session');

    $this->renderLayout();
}

public function sendemailAction()
{
    //Fetch submited params
    $params = $this->getRequest()->getParams();

    $mail = new Zend_Mail();
    $mail->setBodyHtml("Name: " .$params['name']. "<br /> Email: " .$params['email']. "<br /> Phone: " .$params['phone']. "<br /> Business Name: " .$params['company']. "<br /><br /> Questions/Comments: <br />" .$params['comment']);
    $mail->setFrom($params['email'], $params['name']);
    $mail->addTo('me@foobar.com', 'Marketing Dept');
    $mail->setSubject('A Marketing Request');
    try {
        $mail->send();
    }        
    catch(Exception $ex) {
          Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to send your message. Please, try again later'));

    }

    Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your message has been sent to our marketing department.'));
    //Redirect back to index action of (this) inchoo-simplecontact controller
    $this->_redirect('marketing/');
  }
}

?>
1

Please see the original Magento contact form controller, i.e. app/code/core/Mage/Contacts/controllers/IndexController.php -> public function postAction(). For example you have to add this in your sendemailAction function below $params = $this->getRequest()->getParams(); (don't forget to surround it with try/catch block):

        $error = false;

        if (!Zend_Validate::is(trim($params['name']) , 'NotEmpty')) {
            $error = true;
        }

        if (!Zend_Validate::is(trim($params['email']), 'EmailAddress')) {
            $error = true;
        }

        if ($error) {
            throw new Exception();
        }

This is PHP validation. If you like JavaScript validation - you can see again Magento original contact form for reference, i.e. app/design/frontend/base/default/template/contacts/form.phtml. So you have to add this at the bottom of your form:

<script type="text/javascript">
//<![CDATA[
    var simpleContactForm = new VarienForm('simple_contact_form', true);
//]]>
</script>
  • I am not sure in my IndexController.php where to put the php validation code. – Jayreis Apr 16 '15 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.