Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions

Ajax form issue


Reply

2 Posts   1375 Views

Avatar
dbenton

22 December 2010 at 6:02pm (Last edited: 22 December 2010 6:05pm), Community Member, 22 Posts

I am trying to create an ajax contact form. The form is working fine (ajax and regular POST) as long as the form data validates, but when there are validation errors, I am getting unexpected output when submitting by ajax. The following seems to be returned by my form action method, the output of which, I intend to use as a confirmation message.

validationError('Email', '\"Email*\" is required..', 'required'); validationError('Comments', '\"Comments*\" is required..', 'required'); statusMessage('Validation failed', 'bad'); $(document).ready(function() { jQuery('#nav').superfish({ speed: 'fast', autoArrows: false }); }); (function($) { $(document).ready(function() { $("#Form_Form").submit(function(){ $.post( '/contact-us/Form', $(this).serializeArray(), function(data){ $("#Form_Form").replaceWith(data); } ); return false; }); }); })(jQuery);

Any idea what's happening here? If I disable javascript validation -- $form->getValidator()->setJavascriptValidationHandler('none'); -- the error goes away, but other issues are introduced. Ideally, I'd like to keep the javascript validation.

Thanks in advance for any help you can give!

-David

Here is my controller and form code (somewhat truncated):

class ContactPage_Controller extends Page_Controller
{
   
   function init() {
      parent::init();
      
      Requirements::customScript(<<<JS
         (function($) {
            $(document).ready(function() {
               $("#Form_Form").submit(function(){
                  $.post(
                     '/contact-us/Form',
                     $(this).serializeArray(),
                     function(data){
                        $("#Form_Form").replaceWith(data);
                     }
                  );
                  return false;
                  });
               });
            })(jQuery);
JS
      );
   }

   function Form() {
   
      $Params = Director::urlParams();
      
      $fields = new FieldSet(
         new TextField('Name', 'Name*'),
         new EmailField('Email', 'Email*'),
         new TextareaField('Comments','Comments*')
      );
       
      $actions = new FieldSet(
         new FormAction('SendContactForm', 'Send')
      );
      
      $validator = new RequiredFields('Name', 'Email', 'Comments');
      
      $form = new Form($this, 'Form', $fields, $actions, $validator);
      
      return $form;
   }
   
   function SendContactForm($data) {

       $From = $data['Email'];
      $To = 'email@address';
      $Subject = "You've got mail";
      $email = new Email($From, $To, $Subject);
      $email->setTemplate('ContactEmail');
      $email->populateTemplate($data);
      
      $success = $email->send();
      
      if (Director::is_ajax()) {
         if ($success) {
            return 'Success';
         } else {
            return 'Error';
         }
      } else {
         /*non-ajax code*/
      }

   }
}

Avatar
dbenton

5 January 2011 at 4:39am Community Member, 22 Posts

Anyone have any thoughts on this? I am stuck. (Bump.)