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 /

Problem With Creating Custom Form


Reply


4 Posts   595 Views

Avatar
cachobong

Community Member, 9 Posts

25 April 2013 at 6:25pm

I created this custom form and put it in mysite/code and named it NewRegistrationForm.php

class NewRegistrationForm extends Form {

public function __construct($controller, $name) {
$fields = new FieldList(
EmailField::create("Email"),
PasswordField::create("Password")
);
$actions = new FieldList(FormAction::create("login")->setTitle("Log in"));

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

public function login(array $data, Form $form) {
// Do something with $data
Controller::curr()->redirectBack();
}

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

When i did a ?flush=1, it did not flush properly and displayed this PHP error:
Fatal error: Class 'FieldList' not found in <omitted full path here>mysite/code/NewRegistrationForm.php on line 6

Am i missing something?

Avatar
Willr

Forum Moderator, 5513 Posts

3 May 2013 at 9:25pm

Are you on 3.0? FieldList was introduced in 3.0.

Avatar
cachobong

Community Member, 9 Posts

3 May 2013 at 9:30pm

I'm using 2.4.5

Avatar
Willr

Forum Moderator, 5513 Posts

3 May 2013 at 10:48pm

Then make sure you read documentation for 2.4 and use 2.4 supported modules. Your NewRegistrationForm class has mostly 3.0 related code so that will need to be changed to something like

public function __construct($controller, $name) {
$fields = new FieldSet(
new EmailField("Email"),
new PasswordField("Password")
);
$actions = new FieldSet(new FormAction('login', 'Login'));

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

Though I wonder why you're creating a custom login form? You should use MemberAuthenicator and MemberLoginForm for handling security authentication as this will do all the work for you. Both of those classes can be altered via Object::useCustomClass() if you need to customize.