Pastebin


demo/form code

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?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 &quot;edit mode&quot;, 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'        => '&:2,1,|:2,2,!:1,3',
                'rights2'       => '!:1,!:1,!:1,4',
                '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(=> 'een'=> 'twee'=> '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(=> 'one'=> 'two'=> 'three'=> 'funny')
            ))
            ->addField('rights''rights2', array(
                'label' => 'rights field 2',
                'validRights' => array(=> 'one'=> 'two'=> 'three'=> 'funny')
            ));
        return $form;
    }
}
?>