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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Form submit method isn't called


Go to End


7 Posts   6781 Views

Avatar
mauzer_tim

Community Member, 11 Posts

20 February 2010 at 11:40pm

Edited: 20/02/2010 11:43pm

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?

Avatar
baba-papa

Community Member, 279 Posts

21 February 2010 at 2:56am

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

Avatar
mauzer_tim

Community Member, 11 Posts

21 February 2010 at 5:03am

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?

Avatar
Hamish

Community Member, 712 Posts

21 February 2010 at 2:41pm

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.

Avatar
mauzer_tim

Community Member, 11 Posts

21 February 2010 at 6:21pm

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

Avatar
Hamish

Community Member, 712 Posts

22 February 2010 at 9:09am

Edited: 22/02/2010 9:09am

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

Avatar
sanzios

Community Member, 1 Post

19 January 2012 at 1:02pm

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