$fields = new FieldList( TextField::create('name', 'Name'), EmailField::create('email', 'Email') ); $array = $fields->toArray(); print_r($array);
Array ( [name] => Name [email] => Email )
// create the form $form = new Form( $this, 'ContactForm', new FieldList( TextField::create('Name', 'Name'), EmailField::create('Email', 'Email') ), new FieldList( FormAction::create('submit', 'Submit') ) ); // handle the form submission public function submit($data, $form) { // convert the FieldList to an array $fieldData = $form->Fields()->toArray(); // validate the form data $validator = RequiredFields::create(array('Name', 'Email')); if (!$validator->validate($data)) { $form->setSessionData($data); $form->addErrorMessage('Name', 'Please enter your name.', 'bad'); $form->addErrorMessage('Email', 'Please enter a valid email address.', 'bad'); $this->redirectBack(); return; } // process the form submission // ... }In this code example, we use the FieldList toArray method to convert the FieldList to an array, which we then use for form validation. The package library for this example is the SilverStripe framework.