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.

Form Questions /

submit is not working (template form)


Reply


3 Posts   1405 Views

Avatar
leafchild

Community Member, 41 Posts

9 April 2011 at 12:05pm

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>

Avatar
Bereusei

Community Member, 94 Posts

7 May 2011 at 7:19pm

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.

Avatar
Ben_W

Community Member, 80 Posts

9 May 2011 at 6:16pm

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/