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 action


Go to End


4 Posts   3391 Views

Avatar
Roelfsche

25 Posts

23 January 2010 at 3:28am

Edited: 23/01/2010 3:29am

Hi,
i try to create my own form. Everything works fine except my action is never reached. I derived the form class from Form because I want my own template to be used.
First I created a new page type called ContactFormPage:

class ContactFormPage extends Page {
}

class ContactFormPage_Controller extends Page_Controller {
	
	public function init() {
		parent::init ();
		
		//setze die locale, damit das Suchform auf deutsch switcht
		if ($this->dataRecord->hasExtension ( 'Translatable' )) {
			i18n::set_locale ( $this->dataRecord->Locale );
		}
	}
	public function ContactForm() {
		return new ContactForm ( $this, 'Kontakt' );
	}
	
	function submit($data, $form) {
		Debug::message("am i here?");	

		$this->redirect('EmailDanke');
/*	$email = new Email('kontakt@schloss-open-air.de', 'rolf.staege@lumturo.net', 'Webseitenkontakt');
		$email->setTemplate('EmailForm');
		$email->populateEmail($data);
//		$email->send();
*/		
	}
}

Then I created my form class as follows:
DEFINE ( 'TEXTFIELD_MAXLENGTH', 20 );
class ContactForm extends Form {
	function __construct($controller, $name) {

		if ($controller->getRequest ()->getVar ( 'recv_chef' ))
			$rcc = new CheckboxField ( 'recv_chef', _t ( 'ContactForm.RECV_CHEF', 'geschaeftsleitung' ), 1 );
		else
			$rcc = new CheckboxField ( 'recv_chef', _t ( 'ContactForm.RECV_CHEF', 'geschaeftsleitung' ) );
		
		if ($controller->getRequest ()->getVar ( 'recv_presse' ))
			$rcp = new CheckboxField ( 'recv_presse', _t ( 'ContactForm.RECV_PRESSE', 'presse' ), 1 );
		else
			$rcp = new CheckboxField ( 'recv_presse', _t ( 'ContactForm.RECV_PRESSE', 'presse' ) );
		
		if ($controller->getRequest ()->getVar ( 'recv_booking' ))
			$rcb = new CheckboxField ( 'recv_booking', _t ( 'ContactForm.RECV_BOOKING', 'booking' ), 1 );
		else
			$rcb = new CheckboxField ( 'recv_booking', _t ( 'ContactForm.RECV_BOOKING', 'booking' ) );
		
		if ($controller->getRequest ()->getVar ( 'recv_produktleitung' ))
			$rcpr = new CheckboxField ( 'recv_produktleitung', _t ( 'ContactForm.RECV_PRODUKTLEITUNG', 'produktleitung' ), 1 );
		else
			$rcpr = new CheckboxField ( 'recv_produktleitung', _t ( 'ContactForm.RECV_PRODUKTLEITUNG', 'produktleitung' ) );
		
		$fields = new FieldSet ( new OptionsetField ( 'geschlecht', 'geschlecht_t', array ('1' => _t ( 'Contactform.MALE', 'Herr' ), '2' => _t ( 'Contactform.FEMALE', 'Frau' ) ) ), new TextField ( 'name', 'Name', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'vorname', 'Vorname', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'strasse', 'Strasse', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'plz', 'PLZ', '', 5 ), new TextField ( 'ort', 'Ort', '', TEXTFIELD_MAXLENGTH ), new EmailField ( 'email', 'E-Mail Adresse', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'telefon', 'Telefon', '', TEXTFIELD_MAXLENGTH ), $rcc, $rcp, $rcb, $rcpr, new TextareaField ( 'message', 'titel', 10, 180, '' ) );
		
		$actions = new FieldSet ( new FormAction ( 'submit', 'Abschicken' ) );
		
//		$requiredFields = new RequiredFields ( 'geschlecht', 'name', 'vorname', 'strasse', 'plz', 'ort', 'email', 'telefon', 'message' );
		// 	$requiredFields = new RequiredFields('name', 'plz');
		
//    parent::__construct ( $controller, $name, $fields, $actions, $requiredFields );
    parent::__construct ( $controller, $name, $fields, $actions );
    
	}
	
	function forTemplate() {
		return $this->renderWith ( array ($this->class, 'Form' ) );
	}
	
	function submit()
	{
		echo "or here...";
	}
}

The HTML-form-Code, that is created looks like this:
<form id="ContactForm_Kontakt" action="/kontakt/Kontakt" method="post" enctype="application/x-www-form-urlencoded">
...
  <input class="action" id="ContactForm_Kontakt_action_submit" type="submit" name="action_submit" value="Abschicken" title="Abschicken" />
</form>

As I understand, the method "submit" of my form class should be called, if the form will be transmitted. But this method is never reached. Does anybody know, what's the reason for that?
Thank you,
roelfsche

Avatar
zenmonkey

Community Member, 545 Posts

23 January 2010 at 7:10am

I haven't checked if your method is sound but I think the submit action needs to be defined on your form class

Avatar
Roelfsche

25 Posts

25 January 2010 at 12:48am

I defined it in my Form class. It's called 'submit' and I define my Action as follows:

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

I also define it in my Form-Class-Page-Controller, because I red it somewhere (but I think it's only necessary, if I don't derive my own Form).
But neither of them is called...
Roelfsche

Avatar
Roelfsche

25 Posts

25 January 2010 at 9:39pm

I started again from scratch and now it works.
One thing I changed is, that the form template now resides in "includes" and not in "templates". Maybe, that's the reason...
Roelfsche