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.

Archive /

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

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

Form Validation


Reply


8 Posts   2743 Views

Avatar
thejackel

Community Member, 33 Posts

13 June 2008 at 1:13am

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

Avatar
Ingo

Forum Moderator, 801 Posts

21 June 2008 at 6:37pm

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?

Avatar
thejackel

Community Member, 33 Posts

23 June 2008 at 9:24pm

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?

Avatar
Ingo

Forum Moderator, 801 Posts

23 June 2008 at 9:48pm

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?

Avatar
thejackel

Community Member, 33 Posts

24 June 2008 at 3:07am

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.

Avatar
thejackel

Community Member, 33 Posts

17 July 2008 at 12:20am

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

Avatar
Ingo

Forum Moderator, 801 Posts

17 July 2008 at 8:58am

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)?

Avatar
thejackel

Community Member, 33 Posts

17 July 2008 at 11:41pm

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