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

Validating form


Go to End


3 Posts   2301 Views

Avatar
anujkryadav

Community Member, 30 Posts

19 December 2009 at 2:10am

I have created a form using the below code which is working fine.
I need to validate the fields.Please help me to validate the form

RegistrationForm.php

<?php
class RegistrationForm extends Page {
}

class RegistrationForm_Controller extends Page_Controller {

// Make sure you set this to the right group.
// See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
private $defaultGroupID = 2;

/**
* This function lets you put a form on your page, using $Form.
*/
function Form() {

return new Form($this, "Form", new FieldSet(

// List your fields here
new EmailField("Email", "Email address"),
new PasswordField("Password", "Create a Password"),
new TextField("FirstName", "First name"),
new TextField("LastName", "Last name"),
new TextField("Phone", "Phone"),
new TextField("Company", "Company"),
new TextField("Address1", "Address 1"),
new TextField("Address2", "Address 2"),
new TextField("City", "City"),
new TextField("State", "State"),
new TextField("Zip", "Zip"),
new TextField("Country", "Country"),
new ReadonlyField("IPAddress","IPAddress",$_SERVER['REMOTE_ADDR'])

), new FieldSet(

// List the action buttons here
new FormAction("SignupAction", "Submit")

), new RequiredFields(

// List the required fields here: "Email", "FirstName"
"Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"

));
$Form->setTemplate('RegistrationForm');
}

/**
* This function is called when the user submits the form.
*/
function SignupAction($data, $form) {

// Create a new Member object and load the form data into it
$member = new Member();
$form->saveInto($member);

// Write it to the database. This needs to happen before we add it to a group
$member->write();

// Add the member to group. (Check if it exists first)
if($group = DataObject::get_one('Group', "ID = 2")) {

$member->Groups()->add($group);
// Redirect to a page thanking people for registering

// COMMENTING BY ANUJ
//Set data
/* $From = $data['Email'];
$To = "email address of who gets the email, 2nd email addres";
$Subject = "Registration Request";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('RegistrationFormEmail');
//populate template
$email->populateTemplate($data);
//send mail
$email->send(); */

// COMMENTING ENDS HERE BY ANUJ

//$e = new Email();
//$e->setTemplate("RegistrationForm");
//$e->To = "terry@thestoreinsider.com";
//$e->Subject = "Registration Request";
// $e->Body = populateTemplate($data);

//$e->Body = $member;
//$e->send();
Director::redirect('thanks-for-registering/');

}else{

// Redirect to a failure page
Director::redirect('registration-failed/');

}

}
}

?>

Please help me in validating the form fields.
Note.my form is working fine and its working the way i want.it is entering user under a particular group

Avatar
anujkryadav

Community Member, 30 Posts

19 December 2009 at 2:12am

I am giving the code of php page again

<?php
class RegistrationForm extends Page {
}

class RegistrationForm_Controller extends Page_Controller {

// Make sure you set this to the right group.
// See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
private $defaultGroupID = 2;

/**
* This function lets you put a form on your page, using $Form.
*/
function Form() {

return new Form($this, "Form", new FieldSet(

// List your fields here
new EmailField("Email", "Email address"),
new PasswordField("Password", "Create a Password"),
new TextField("FirstName", "First name"),
new TextField("LastName", "Last name"),
new TextField("Phone", "Phone"),
new TextField("Company", "Company"),
new TextField("Address1", "Address 1"),
new TextField("Address2", "Address 2"),
new TextField("City", "City"),
new TextField("State", "State"),
new TextField("Zip", "Zip"),
new TextField("Country", "Country"),
new ReadonlyField("IPAddress","IPAddress",$_SERVER['REMOTE_ADDR'])

), new FieldSet(

// List the action buttons here
new FormAction("SignupAction", "Submit")

), new RequiredFields(

// List the required fields here: "Email", "FirstName"
"Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"

));
$Form->setTemplate('RegistrationForm');
}

/**
* This function is called when the user submits the form.
*/
function SignupAction($data, $form) {

// Create a new Member object and load the form data into it
$member = new Member();
$form->saveInto($member);

// Write it to the database. This needs to happen before we add it to a group
$member->write();

// Add the member to group. (Check if it exists first)
if($group = DataObject::get_one('Group', "ID = 2")) {

$member->Groups()->add($group);
// Redirect to a page thanking people for registering

Director::redirect('thanks-for-registering/');

}else{

// Redirect to a failure page
Director::redirect('registration-failed/');

}

}
}

?>

Avatar
Willr

Forum Moderator, 5523 Posts

19 December 2009 at 2:07pm

RequiredFields needs to be passed an array to iterate over, not just strings. Change your RequiredFields code to

new RequiredFields(arrray("Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"))