Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Form Validation

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2578 Views
  • thejackel
    Avatar
    Community Member
    33 Posts

    Form Validation Link to this post

    Hi,

    I'm tearing my hair out over this!

    I've built a custom form and have used the RequiredFields() class to specify what fields are mandatory.

    If the fields arn't filled in on submission the form doesn't fully submit (which is correct).

    The problem is, no error messages are displayed to the user?

    Does anyone know why this would be?

    I'm not using a custom form template - its just the standard one what the Form class uses.

    Thanks

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Form Validation Link to this post

    can you paste the instanciation of your custom form here, including the requiredfields definition? (using pastie.caboo.se/)

    so the javascript validation is not triggered, or are you referring to messages which should be generated after the php-validation failed?

  • thejackel
    Avatar
    Community Member
    33 Posts

    Re: Form Validation Link to this post

    hi Ingo,

    custom form code below:

    class BookingForm extends Form {

       function __construct($controller, $name, $eventdata) {
          // Build up event data for emailing
          if($eventdata) {
             foreach($eventdata as $key => $value)
             {
                $this->Event = $value->Event;
                $this->EventDate = $value->EventDate;
                $this->Contact = $value->Contact;
                $this->ContactEmail = $value->Email;
             }
          }
          
          $fields = new FieldSet(
             new TextField('Firstname'),
             new TextField('Surname'),
             new EmailField('Email'),
             new TextField('Telephone'),
             new HiddenField('Event', '',$this->Event),
             new HiddenField('EventDate', '',$this->EventDate),
             new HiddenField('Contact', '',$this->Contact),
             new HiddenField('ContactEmail', '',$this->ContactEmail)
          );
          // A hidden form field has been added to ss to stop CRSF attacks. If you want to disable this feature, use the method below
          $this->disableSecurityToken();
          // Create validator
          $validator = new RequiredFields('Firstname', 'Surname', 'Email');

          // Create actions for form (individual buttons what are passed to specific functions)
          $actions = new FieldSet(
             new FormAction('submit', '')
             //new FormAction('doPublicationForm', 'Reset')
             );

          parent::__construct($controller, $name, $fields, $actions, $validator);
       }

       function submit($data, $form) {
          $email = new Email_Template();
          $email->ss_template = "EventBookingEmail";
          $templateData = array(
             'Contact' => $data['Contact'],
             'Event' => $data['Event'],
             'EventDate' => $data['EventDate'],
             'UserName' => $data['Firstname']. ' '. $data['Surname'],
             'Email' => $data['Email'],
             'Telephone' => $data['Telephone']
          );
          $email->populateTemplate($templateData);
          $email->subject = 'Event Booking: '.$data['Event'];
          $email->from = $email->getAdminEmail();
          $email->to = $data['ContactEmail'];
          $email->send();
          Director::redirect('event-booking-thankyou/');
       }
    }

    I use $BookingForm to insert my custom form into the page.

    The form validation half works in that it doesn't submit if any required fields are missing. The problem is though, it doesn't display any error messages.

    any ideas?

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Form Validation Link to this post

    the code looks alright... do you have any css-rules that could hide the error messages? can you see if they are inserted in the sourcecode (e.g. via firebug?) do you get any javascript errors?

  • thejackel
    Avatar
    Community Member
    33 Posts

    Re: Form Validation Link to this post

    i've looked into the form structure with firebug and theirs no errors being displayed.

    i have another form built in much the same way and the validation on that works fine.

    Their is a js error though:

    Behaviour is not defined.

    It's pointing to the Behaviour.register object what is inserted at in the page header.

    I don't think this is causing it though because this error also appears on the page where the form actually works.

  • thejackel
    Avatar
    Community Member
    33 Posts

    Re: Form Validation Link to this post

    Maybe its because my custom form template is wrong. Heres what I have:

    <form $FormAttributes>
    <ul>
    <% if Message %>
    <p id="{$FormName}_error" class="message $MessageType">$Message</p>
    <% else %>
    <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
    <% end_if %>
    <li>
    <label :="" for="publication-title">Publication title:</label>
    $dataFieldByName(PublicationTitle)
    </li>
    <li class="submit-comments">Please use the space provided below to provide us with an outline of your publication (i.e. Background & Aims, Design & Scope, Findings & Conclusions.)</li>
    <li>
    <label :="" for="publication-abstract">Publication abstract:</label>
    $dataFieldByName(PublicationAbstract)
    </li>
    <li>
    <label :="" for="publication-category">Category:</label>
    $dataFieldByName(Category)
    </li>
    <li>
    <label :="" for="publication-web-site-url">Web site url:</label>
    $dataFieldByName(WebSiteURL)
    </li>
    <li>
    <label :="" for="publication-funders">Funders:</label>
    $dataFieldByName(Funders)
    </li>
    <li class="submit-comments">Please include both internal and external funders.</li>
    <li id="project-status">
    <strong>Project status:</strong>
    $dataFieldByName(ProjectStatus)
    </li>
    <li>
    <label :="" for="publication-report-url">Report url:</label>
    $dataFieldByName(ReportURL)

    </li>
    <li>
    <label :="" for="publication-contact-name">Contact name:</label>
    $dataFieldByName(ContactName)
    </li>
    <li class="submit-comments">Please provide the name of 1 key contact involved in the project.</li>
    <li>
    <label :="" for="publication-contact-address">Contact address:</label>
    $dataFieldByName(ContactAddress)
    </li>
    <li>
    <label :="" for="publication-contact-tel">Contact tel:</label>
    $dataFieldByName(ContactTel)
    </li>
    <li>
    <label :="" for="publication-contact-email">Contact email:</label>
    $dataFieldByName(ContactEmail)
    </li>
    <li>
    <% if Actions %>
    <% control Actions %>$Field<% end_control %>
    <% end_if %>
    </li>
    </ul>
    </form>

    WHen I removed the custom form (used the default form), the validation worked correctly.

    So, it must be my custom form.

    Have I missed a 'merge variable' or controller?

    Or is my 'Message ' control missing something?

    Thanks

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Form Validation Link to this post

    form template looks alright... behaviour.js would be responsible for clientside validation errors - just to be double sure, can you submit a form with javascript turned off? also, can you show us the shortened controller in which you instanciate the form (and have your custom renderWith() call)?

  • thejackel
    Avatar
    Community Member
    33 Posts

    Re: Form Validation Link to this post

    Thanks for getting back to me.

    Heres my shortened code:

    // Create page from Page object
    class PublicationFormAdd extends Page {
       // ...
    }

    // Page controller, creates new instance of PublicationFormAdd_Form
    class PublicationFormAdd_Controller extends Page_Controller {

       function PublicationForm() {
          return new PublicationFormAdd_Form($this, 'PublicationForm');
       }
    }

    class PublicationFormAdd_Form extends Form {

       function __construct($controller, $name) {
          // ... create the form
       }
       
       // Build form
       function forTemplate() {
          return $this->renderWith(array(
             $this->class,
             'Form'
             ));
       }
       
       // Form submit action
       function submit($data, $form) {
          // ...
       }
    }

    If i comment out the forTemplate() method (use silverstripes default method), the form displays relevant error messages.

    The form doesn't get submitted (it emails me when it does) but I don't receive any errors (i've checked the DOM with firebug and nothings being hidden).

    Any other ideas?

    Thanks

    2578 Views
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.