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

Validation for Custom Form

Go to End

11 Posts   6668 Views


5 January 2009 at 10:12am Community Member, 42 Posts

Hi Guys,

I have created my first Silverstripe site...and everything worked out very well except contact form..
I want to use custom form...where I can put asterisk(*) for required field after form field to mark those field as a required one, and also would like to use custom form validation.
I have gone through the tutorial for creating custom form, read all threads regarding custom form creation & validation..but I am still confused about using Dreamweaver's form validation code (which is created in Javascript)? Can I use that code? Where shall I put that code?
I want to send the form data through an email, so dont want to store in database...Normally use PHP mail() function to send form data..But dont have an idea about where to put that code???
Any help is very much appreciated. Thanks in advance for your help & time..

Kind Regards,


8 January 2009 at 10:57am Community Member, 470 Posts

Why do you want to use DW's JS validation when SS has its own built in?

I know you've read up on it, but shows how to include certain form fields to be required.


8 January 2009 at 11:09am (Last edited: 8 January 2009 11:21am), Forum Moderator, 921 Posts

It would be far easier to just use the SilverStripe Form API to create the form. Validation is also supplied.

Here's a code example of a "ContactPage" page type that has a basic contact form on it. This code would sit inside the ContactPage.php file, which should reside in the mysite/code directory for your project.

class ContactPage extends Page {
class ContactPage_Controller extends Page_Controller {
   function Form() {
      $fields = new FieldSet(
         new TextField('Name', 'Your name'),
         new EmailField('Email', 'Your email address'),
         new TextareaField('Comments', 'Any comments?')
      $actions = new FieldSet(
         new FormAction('doSubmit', 'Submit')
      $validator = new RequiredFields(
      return new Form($this, 'Form', $fields, $actions, $validator);

   function doSubmit($data, $form) {
      $email = new Email();
      $email->setSubject('Someone submitted the contact form');

      $form->sessionMessage('Thanks for contacting us. We'll endeavour to get in touch with you as soon as possible!');

$Form is where your contact form will sit in the template when the user browses to the contact page. This is where the Form() function returns the form, with the appropriate fields, submit buttons and validation etc. If you're using the blackcandy theme (or a copy of it), you shouldn't have to do anything special, apart from just ensure that the "ContactPage" page type is set in the CMS for your contact page, using the dropdown in the "Behaviour" tab.

the doSubmit() function is where the email gets sent when the user submits the form. In this case, we set up an email to send to the site admin so that they know someone needs to be contacted.

You'll just need to create inside the mysite/templates/email directory for your SilverStripe project. This email template can contain the variables $Name, $Email, $Comments etc which are substituted with what the user submitted in the contact form. The line $email->populateTemplate($data) facilitates this by pushing through the submitted form data into that email template.

Hope this helps!



9 January 2009 at 12:38am Community Member, 42 Posts

Thanks Sean,

For your reply..Thats very helpful information.
Just a small question, how can I put asterisk(*) after required fields in Form?
Do i need to create an image & put that image as a background image in CSS??
Thanks once again for your reply..



9 January 2009 at 8:00am Community Member, 470 Posts

Edit your template ( in the example above) and just type the * in yourself when setting up the form layout.

When you make a field required, it is given a CSS class so you could style it via CSS to make all required field text bold or a different colour for example.


15 January 2009 at 2:09am Community Member, 42 Posts

Thanks for your reply Leeum,

Hi Sean,

I have tried your code, but unfortunately that code does not work in my case.
Error - Page not found error after adding doSubmit() code...

Even tried to put the code you had mentioned in one of the threads -
if(!empty($data['Email'])) {
$email = new Email_Template(
'Form was submitted - subject line (change me)'
$email->ss_template = 'MyEmailTemplate';
'Name' => $data['Name'],
'Email' => $data['Email']

But this one also does not work
Error msg -
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log

Am I doing anything wrong???Any help!
Thanks in advance for your time...



16 January 2009 at 12:17am Community Member, 42 Posts

hey guys,

use following code in doContactSubmit() & it atleast sends an email

function doContactSubmit($data, $form)
$email = new Email('from address,'to address','Contact Form','Form Test');
$form->sessionMessage('Thanks for contacting us. We will endeavour to get in touch with you as soon as possible!');

so dont know why it does not work when I use code given by Sean...
Any thoughts???



16 January 2009 at 2:44am Community Member, 42 Posts

Hi Guys,

Just you to know I finally magagend to get my contact form working...
While going through API docs, I realise that populateTemplate() is not direct method of email() but of class email_template().
so instead of creating instance of Email() I have created an instance of Email_Template() & assign populateTemplate() method to it.
And my contact form starts working..
Thanks Sean anyways for providing the code..And correct me if anything above written is wrong..


Go to Top