11# PHP Form
22[ ![ Build Status] ( https://travis-ci.org/dipcode-software/php-form.svg?branch=master )] ( https://travis-ci.org/dipcode-software/php-form )
33[ ![ Coverage Status] ( https://coveralls.io/repos/github/dipcode-software/php-form/badge.svg?branch=master )] ( https://coveralls.io/github/dipcode-software/php-form?branch=master )
4+ [ ![ Latest Stable Version] ( https://poser.pugx.org/dipcode/php-form/v/stable )] ( https://packagist.org/packages/dipcode/php-form )
45[ ![ License] ( http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat )] ( http://www.opensource.org/licenses/MIT )
56
6- PHP class for form handling abstraction inspired in Django Framework forms .
7+ Full featured form engine library inspired in Django Framework.
78
89## Instalation
910Library can be installed using Composer like so:
@@ -12,129 +13,6 @@ Library can be installed using Composer like so:
1213$ composer require dipcode/php-form
1314```
1415
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_name) 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-
107- ## Available Fields
108-
109- ``` php
110- // All fields has the following common arguments:
111- $args = [
112- PHPForm\Widgets\Widget $widget,
113- string $label = null,
114- string $help_text = '',
115- bool $required = false,
116- bool $disabled = false,
117- mixed $initial = null,
118- array $validators = array(),
119- array $widget_attrs = array(),
120- array $error_messages = array()
121- ]
122-
123- // Fields available
124- // '...' represents common fields
125-
126- new BooleanField([...]);
127- new CharField([int $max_length, int $min_length, ...]);
128- new ChoiceField([array $choices, ...]);
129- new DateField([string $format, ...]);
130- new DateTimeField([string $format, ...]);
131- new EmailField([...]);
132- new FileField([int $max_size, array $valid_filetypes, ...]);
133- new IntegerField([int $max_value, int $min_value, ...]);
134- new URLField([...]);
135-
136- ```
137-
13816## Starting development
13917Start by cloning the repo:
14018
0 commit comments