Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » submit is not working (template form)

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

Page: 1
Go to End
Author Topic: 1392 Views
  • leafchild
    Avatar
    Community Member
    41 Posts

    submit is not working (template form) Link to this post

    When I click submit button, just refreshing page. (nothing happened. Validation is not working neither)

    What do I need to fix?

    I'm using template form (http://doc.silverstripe.org/sapphire/en/topics/forms#using_a_custom_template)

    Landing.php

    <?php
    class Landing_Controller extends Page_Controller {
       static $allowed_actions = array(
          //'MyForm'
       );

    public function MyForm(){
    return new MyForm($this, 'MyForm');
    }
    }
    ?>

    MyForm.php

    <?php
    class MyForm extends Form {
    function __construct($controller, $name) {
    $fields = new FieldSet(
    new TextField('name', 'name', 'first','20')
    new EmailField('Email', 'Email address'),
    );

    $actions = new FieldSet(
    new FormAction('sendRequest', 'send request')
    );
       
    $validator = new RequiredFields('name', 'Email'');

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

    function forTemplate() {
    return $this->renderWith(array(
    $this->class,
    'Form'
    ));
    }

    public function sendRequest($data, $form) {
    //submit code
    }   
    }
    ?>

    MyForm.ss

    <form $FormAttributes>

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

    <fieldset>

    <table>
    <tr><td>name</td><td>$dataFieldByName(name)</td></tr>
    <tr><tdemail></td><td>$dataFieldByName(Email)</td></tr>
    </table>

    $dataFieldByName(SecurityID)
    </fieldset>

    <% if Actions %>
    <div class="Actions">
    <% control Actions %>$Field<% end_control %>
    </div>
    <% end_if %>

    </form>

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: submit is not working (template form) Link to this post

    If you want to refresh the page, I think you must add something like this in your MyForm.php:

    public function sendRequest($data, $form) {
    header( 'location:http://www.blabla.biz/blabla/' )
    }

    In any case the sendRequest-function is called, if you press the submit-button.

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: submit is not working (template form) Link to this post

    For your validation to work, put the fields into array as follow:

    $validator = new RequiredFields(array('name', 'Email'));

    you will also need a submission class to construct your database table.

    class ContactUsSubmission extends DataObject {
       static $db = array(
          'name' => 'Varchar(255)',
          'Email' => 'Varchar(255)'
       );   
    }

    You then use this class in your process function so that your script knows where to store the data captured by your form. in your case the function is sendRequest()

    public function sendRequest($data, $form){
    $submission = new ContactUsSubmission();
       $form->saveInto($submission);
       $submission->write();

    //then you need to redirect user to thank you page or any page you want.
    Director::redirect('/thank-you/');
    }

    Here is a nice tutorial on a basic contact us form, however be aware it does not extend the Form class as you did. I recommend you to start with this easy tutorial.
    http://www.ssbits.com/newbies/2010/creating-a-simple-contact-form/

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