@@ -12,6 +12,98 @@ Library can be installed using Composer like so:
1212$ composer require dipcode/php-form
1313```
1414
15+ ## Simple Example
16+
17+ Start by defining your form class like so:
18+ ``` php
19+ <?php
20+ namespace MyForms;
21+
22+ use PHPForm\Exceptions\ValidationError;
23+ use PHPForm\Fields\CharField;
24+ use PHPForm\Fields\EmailField;
25+ use PHPForm\Forms\Form;
26+ use PHPForm\Widgets\Textarea;
27+
28+ class ContactForm extends Form
29+ {
30+ protected static function setFields()
31+ {
32+ return array(
33+ 'name' => new CharField(['required' => true]),
34+ 'email' => new EmailField(['required' => true]),
35+ 'subject' => new CharField(),
36+ 'body' => new CharField(["widget" => Textarea::class])
37+ );
38+ }
39+
40+ protected function clean()
41+ {
42+ // Use this function to crossfields validation
43+ //
44+ // You can use $this->addError($message, $field) to add error messages to specific fields.
45+ // Or just throw a ValidationError Exception.
46+ }
47+ }
48+ ```
49+
50+ Define the template to render form fields:
51+ ``` php
52+ <form action =" /contact-form/" method =" POST" novalidate >
53+ <div class =" form-row" >
54+ <div class =" col" >
55+ <?php echo $form['name']->label_tag; ?>
56+ <?php echo $form['name']; ?>
57+ <?php echo $form['name']->errors; ?>
58+ </div >
59+ <div class =" col" >
60+ <?php echo $form['email']->label_tag; ?>
61+ <?php echo $form['email']; ?>
62+ <?php echo $form['email']->errors; ?>
63+ </div >
64+ </div >
65+ <div class =" form-row" >
66+ <div class =" col" >
67+ <?php echo $form['subject']->label_tag; ?>
68+ <?php echo $form['subject']; ?>
69+ <?php echo $form['subject']->errors; ?>
70+ </div >
71+ </div >
72+ <div class =" form-row" >
73+ <div class =" col" >
74+ <?php echo $form['body']->label_tag; ?>
75+ <?php echo $form['body']; ?>
76+ <?php echo $form['body']->errors; ?>
77+ </div >
78+ </div >
79+
80+ <input type =" submit" value =" Submit" >
81+ </form >
82+ ```
83+
84+ Now process the form and force validation when ` $_POST ` data is sended:
85+ ``` php
86+ namespace MyViews;
87+
88+ use MyForms\ContactForm;
89+
90+ public function handleForm()
91+ {
92+ if (!empty($_POST)) {
93+ $form = new ContactForm(["data" => $_POST]);
94+
95+ if ($form->isValid()) {
96+ // Form is valid, do your logic here
97+ // Use can use $form->getCleanedData() to access cleaned and validated data.
98+ }
99+
100+ return $form;
101+ }
102+
103+ return new ContactForm();
104+ }
105+ ```
106+
15107## Starting development
16108Start by cloning the repo:
17109
0 commit comments