<?php
class FML_PageType_Demo_Form extends FML_PageType
{
public function init() {
if (in_array($this->action, array(
'addProcess',
'editProcess',
))) {
$this->cfg->showOutput = false; // no output when processing forms
}
}
protected function actionDefault() {
$this->setTitle('Form demo');
?><div class="main">
<h1>Form demo</h1>
<p><a href="<?php echo $this->escape($this->link(false, array('action' => 'add'))) ?>">add</a></p>
<p>Demonstrates a form for adding data.</p>
<p><a href="<?php echo $this->escape($this->link(false, array('action' => 'edit'))) ?>">edit</a></p>
<p>Demonstrates the same form in "edit mode", simulating previously stored data.</p>
<p>All the code that is necessary to write this entire page (/demo/form) can be found <a href="http://fangorn.thijma.nl/pastebin?id=2" title="complete code of this page(type)">here</a>.</p>
</div><?php
}
// Display "Add" form.
protected function actionAdd() {
$this
->setTitle('Form demo - adding new data');
?><div class="main"><?php
// Load form, initialize, display.
$form = $this->form(array(
'name' => 'formName',
'action' => $this->link(false, array('action' => 'addProcess'))
));
$form->initialize()->printHtml();
?></div><?php
}
// Process "Add" form.
protected function actionAddProcess() {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$form = $this->form(array('name' => 'formName'));
// Load form, initialize, validate.
$data = $form->initialize()->validate();
// If validation failed, $data is false.
if ($data === false) {
// Store data in session and redirect back to the "Add" form page, indicating that there are errors.
$form->store();
$this->redirect($this->link(false, array(
'action' => 'add',
'errors' => true,
)));
}
// Validation passed - process form. Normally this would consist of storing stuff in a database.
FML_Debug::dump($data);
}
}
// Simulate "Edit" form. Same as before, but with initialisation with dummy data.
protected function actionEdit() {
$this
->setTitle('Form demo - editing existing data');
?><div class="main"><?php
$form = $this->form(array(
'name' => 'formName',
'action' => $this->link(false, array('action' => 'editProcess'))
));
// Normally, this data comes from a database.
$form->initialize(array(
'text' => '<b>hello</b>',
'number' => 41,
'email' => 'dummy@gmail.com',
'select' => 2,
'moveselect' => array('a', 'c'),
'moveselect2' => array('d'),
'checkbox' => true,
'counter' => 12,
'textarea' => "<h1>Hello</h1>\n<script>alert('hello')</script>",
'rights' => '3,!:1,2,|:2,1,&:2',
'rights2' => '4,!:1,!:1,!:1',
'radiotest' => 2,
))->printHtml();
?></div><?php
}
// Process "Edit" form. Same as before.
protected function actionEditProcess() {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$form = $this->form(array('name' => 'formName'));
$data = $form->initialize()->validate();
if ($data === false) {
$form->store();
$this->redirect($this->link(false, array(
'action' => 'edit',
'errors' => true,
)));
}
// Validation passed - process form data $data in some way.
FML_Debug::dump($data);
}
}
// Form definition. We only have to define this form ONCE.
protected function form($options, $properties=array()) {
$form = new FML_Form($this, $options, $properties);
$form
->addField('token', false, array('haltOnInvalidToken' => false))
->addField('label', 'label_basic', array('html' => '<h2>Basic elements</h2>'))
->addField('text', 'text', array(
'label' => 'field (required)',
'rules' => array('required'),
'properties' => array('maxlength' => 12, 'size' => 12),
))
->addField('text', 'number', array(
'label' => 'numeric field (required)',
'rules' => array('required', 'number'),
))
->addField('text', 'email', array(
'label' => 'e-mail address',
'rules' => array('email'),
'properties' => array('class' => 'text-long'),
))
->addField('select', 'select', array(
'label' => 'dropdown (required)',
'options' => array(1 => 'een', 2 => 'twee', 3 => 'drie'),
'rules' => array('required'),
'firstOptionLabel' => '- select -',
))
->addField('checkbox', 'checkbox', array(
'label' => 'checkbox',
))
->addField('textarea', 'textarea', array(
'label' => 'textarea',
))
->addField('radio', 'radiotest', array(
'class' => 'radio',
'label' => 'radio field',
'options' => array('0' => 'nul', '1' => 'een', '2' => 'twee'),
))
->addField('label', 'label_composite', array('html' => '<h2>Composite elements</h2>'))
->addField('counter', 'counter', array(
'label' => 'counter', 'properties' => array('size' => 3),
))
->addField('moveselect', 'moveselect', array(
'label' => 'moveselect',
'options' => array('a' => 'een', 'b' => 'twee', 'c' => 'drie'),
))
->addField('moveselect', 'moveselect2', array(
'label' => 'moveselect 2',
'options' => array('d' => 'vijf', 'e' => 'zes', 'f' => 'zeven'),
'customOrder' => true,
))
->addField('label', 'label_custom', array('html' => '<h2>Custom elements</h2>'))
->addField('rights', 'rights', array(
'label' => 'rights field',
'validRights' => array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'funny')
))
->addField('rights', 'rights2', array(
'label' => 'rights field 2',
'validRights' => array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'funny')
));
return $form;
}
}
?>