Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Form submit method isn't called

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

Page: 1
Go to End
Author Topic: 2318 Views
  • mauzer_tim
    Avatar
    Community Member
    11 Posts

    Form submit method isn't called Link to this post

    Hi everyone!

    I'm creating a "contact us" form using subclassing method describing here: http://doc.silverstripe.org/doku.php?id=form

    Unfortunately, method responsible for performing submission declared in my form subclass isn't called. Take a look at the code below:

    class SupportForm extends Form {

    function __construct($controller, $name) {
    $fields = new FieldSet(
    new TextField('FirstName', 'First name'),
    new EmailField('Email', 'Email address'),
    new TextareaField('Message')
    );

    $actions = new FieldSet(
    new FormAction('submit', 'Submit')
    );

    $validator = new RequiredFields('Email', 'Message');

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

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

    public function submit($data, $form) {
    die("This code is never called");
    }
    }

    class ContactUs extends Page {

    }

    class ContactUs_Controller extends Page_Controller {

    public function Form() {
    return new SupportForm($this, "SupportForm");
    }
    }


    Or see more this code in more convenient form: http://pastie.org/834032

    Just for information: I've also created includes/SupportForm.ss template to display this form data.

    What I'm doing wrong?

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: Form submit method isn't called Link to this post

    If you have problems debugging your code, why don´t you just the the Userforms module? It´s just drag and drop configuration.

  • mauzer_tim
    Avatar
    Community Member
    11 Posts

    Re: Form submit method isn't called Link to this post

    As I wrote before I needed to create "contact us" form with captcha and custom validation on the server side. Userforms module does not allow to do this . And also my goal is to figure out how to make custom forms.

    P.S.
    At the moment I found the solve to my problem: it was needed to add SupportForm method to the ContactUs_Controller with the code that duplicates Form method:

    public function SupportForm() {
    return new SupportForm($this, "SupportForm");
    }

    Only in this case submit method of the form will be called. A bit strange behavior... Is it a bug of the framework?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Form submit method isn't called Link to this post

    It works now because the method name passed to the form is the same as the method name that generates it.

    In your first example the method SupportForm didn't exist, so sapphire would not be able to find the form action 'submit'. Now that you have renamed the method, sapphire directs the request successfully. You could also have fixes this by passing "Form" instead of "SimpleForm" to your form constructor.

  • mauzer_tim
    Avatar
    Community Member
    11 Posts

    Re: Form submit method isn't called Link to this post

    Hamish,
    thank you for your input.

    If I name my Form just "Form" I'll loose an ability to take away view of the form to the separated file. If I name my form "SupportForm" I have an ability to create "includes/SeparateForm.ss" that will be used for rendering of my form. Of course I can name method that returns Form "SupportForm" and remove "Form" but in this case I have to add "SupportForm" to the layout template or to create different layout for "Contact Us" page, which are undesirable actions. Moreover this way of creation form is a standard way supposed by SilverStripe developers and it's described here: http://doc.silverstripe.org/doku.php?id=form#using_a_custom_template

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Form submit method isn't called Link to this post

    "If I name my Form just "Form" I'll loose an ability to take away view of the form to the separated file."

    I didn't say you should change the class name of the form, only the method that creates it in your page controller. This doesn't change which template your form will use. Sapphire will still search for a template with the same class name as your form ("SupportForm"), not the method name that called it ("Form", or "SupportForm", or whatever).

    The only difference between this:

    function Form() {
       return new SupportForm($this, "Form");
    }

    and

    function SupportForm() {
       return new SupportForm($this, "SupportForm");
    }

    is the tag you use in your page template to display the form (ie, $Form or $SupportForm). However, the form will still render with SupportForm.ss in both cases.

  • sanzios
    Avatar
    Community Member
    1 Post

    Re: Form submit method isn't called Link to this post

    Hi I have similar problem (form submit generate in response form, action is never called) but I don't see if there is a problem with name of function with generate the form.

    see code below

    BookVisitForm

    <?php
    class BookVisitForm extends Page {
       static $db = array();
       static $has_one = array();
    }

    class BookVisitForm_Controller extends Page_Controller {

    public function init() {
          parent::init();
          
    }

    static $allowed_actions = array(
    );

    public function BookVisitForm() {
       $bookVisitForm = new BookVisitFormTemplate($this, "BookVisitForm");
       $bookVisitForm ->getValidator()->setJavascriptValidationHandler('none');
       return $bookVisitForm;
    }

    function BookVisitAction($data, $form) {
    /* NEVER CALL*/
          $form->addErrorMessage("Message","Send msg","good");
          Director::redirectBack();
    }

    }

    BookVisitFormTemplate

    class BookVisitFormTemplate extends Form {

    function __construct($controller, $name) {
          
        .....

        $reservation= new FormAction('BookVisitAction', 'RESERVATION');
        $reservation->AddExtraClass('button');
    $actions = new FieldSet(
    $reservation
    );

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

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

    }

    this is generated html code (looks fine)

    <form id="BookVisitFormTemplate_BookVisitForm" action="........../BookVisitForm" method="post" enctype="application/x-www-form-urlencoded" class="jqtransformdone">
    ........
    <input class="action button" id="BookVisitFormTemplate_BookVisitForm_action_BookVisitAction" type="submit" name="action_BookVisitAction" value="RESERVATION" title="RESERVATION" />
    ..........
    </form>

    but after submit form in response I get the same form (when I'm submitting by ajax I see exactly html code generated in response).

    does anyone see the problem???
    Thanks in advance for your help

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