Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Validation for Custom Form

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

Page: 1 2
Go to End
Author Topic: 6574 Views
  • Anaya
    Avatar
    Community Member
    42 Posts

    Validation for Custom Form Link to this post

    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,
    Anaya..

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Validation for Custom Form Link to this post

    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 http://doc.silverstripe.com/doku.php?id=validator shows how to include certain form fields to be required.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Validation for Custom Form Link to this post

    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.

    <?php
    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(
             'Name',
             'Email'
          );
          return new Form($this, 'Form', $fields, $actions, $validator);
       }

       function doSubmit($data, $form) {
          $email = new Email();
          $email->setTemplate('ContactPage_Email');
          $email->setSubject('Someone submitted the contact form');
          $email->setFrom('admin@somesite.com');
          $email->setTo('admin@somesite.com')
          $email->populateTemplate($data);
          $email->send();

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

    $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 ContactPage_Email.ss 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!

    Cheers,
    Sean

  • Anaya
    Avatar
    Community Member
    42 Posts

    Re: Validation for Custom Form Link to this post

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

    Regards
    Anaya...

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Validation for Custom Form Link to this post

    Edit your template (ContactPage_Email.ss 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.

  • Anaya
    Avatar
    Community Member
    42 Posts

    Re: Validation for Custom Form Link to this post

    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(
    'fromemail@somewhere.com',
    $data['Email'],
    'Form was submitted - subject line (change me)'
    );
    $email->ss_template = 'MyEmailTemplate';
    $email->populateTemplate(array(
    'Name' => $data['Name'],
    'Email' => $data['Email']
    ));
    $email->send();
    }

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

    Regards
    Anaya

  • Anaya
    Avatar
    Community Member
    42 Posts

    Re: Validation for Custom Form Link to this post

    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');
    $email->send();
    $form->sessionMessage('Thanks for contacting us. We will endeavour to get in touch with you as soon as possible!');
    Director::redirectBack();

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

    Regards
    Anaya

  • Anaya
    Avatar
    Community Member
    42 Posts

    Re: Validation for Custom Form Link to this post

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

    Regards
    Anaya

    6574 Views
Page: 1 2
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.