Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Ajax form issue

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1364 Views
  • dbenton
    Community Member
    22 Posts

    Ajax form issue Link to this post

    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!


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

    class ContactPage_Controller extends Page_Controller
       function init() {
             (function($) {
                $(document).ready(function() {
                      return false;

       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);
          $success = $email->send();
          if (Director::is_ajax()) {
             if ($success) {
                return 'Success';
             } else {
                return 'Error';
          } else {
             /*non-ajax code*/


  • dbenton
    Community Member
    22 Posts

    Re: Ajax form issue Link to this post

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

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.