Skip to content

Commit d44598f

Browse files
committed
update
1 parent ccfb451 commit d44598f

File tree

2 files changed

+191
-192
lines changed

2 files changed

+191
-192
lines changed

README.md

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,191 @@
1-
# forms
1+
# MintyPHP Forms
2+
3+
MintyPHP Forms is a powerful PHP form builder that enables you to create and validate forms without having to write a lot of boiler plate code.
4+
5+
## Features
6+
7+
- Bulma renderer, easily creating good looking forms
8+
- Validation rules, such as validating email, string length and numeric comparisons
9+
- Support checkbox arrays and multi selects
10+
- Best practices: follows best practices, well-tested, object oriented code
11+
- Extensible: create support for your favorite framework or form rendering style
12+
13+
## Installation
14+
15+
Run the following command to install MintyPHP Forms with Composer.
16+
17+
```bash
18+
composer require mintyphp/forms
19+
```
20+
21+
The package has no dependencies on other packages.
22+
23+
## Quick Start
24+
25+
Then add the following to alias the most used classes in your PHP file:
26+
27+
```php
28+
use MintyPHP\Form\Elements as E;
29+
use MintyPHP\Form\Validator\Validators as V;
30+
```
31+
32+
Create a simple login form using:
33+
34+
```php
35+
$form = E::form([
36+
E::field(E::text('username'),E::label('Username'),[V::required('Username is required')]),
37+
E::field(E::password('password'),E::label('Password')),
38+
E::field(E::submit('Login')),
39+
]);
40+
$form->render(true);
41+
```
42+
43+
Now render the form using:
44+
45+
```html
46+
<form method="post">
47+
<div>
48+
<label for="username">Username</label>
49+
<input id="username" type="text" name="username" value=""/>
50+
</div>
51+
<div>
52+
<label for="password">Password</label>
53+
<input id="password" type="password" name="password" value=""/>
54+
</div>
55+
<div>
56+
<input type="submit" value="Login"/>
57+
</div>
58+
</form>
59+
```
60+
61+
Easy as that.
62+
63+
## Frontend frameworks
64+
65+
MintyPHP Forms has support for the Bulma framework right out of the box.
66+
Just tell MintyPHP Forms that you want to use Bulmas style forms using:
67+
68+
```php
69+
E::$style = 'bulma';
70+
// now render the same form
71+
$form = E::form([
72+
E::field(E::text('username'),E::label('Username'),[V::required('Username is required')]),
73+
E::field(E::password('password'),E::label('Password')),
74+
E::field(E::submit('Login')),
75+
]);
76+
$form->render(true);
77+
```
78+
79+
And the output will be form in the familiar Bulma style:
80+
81+
```html
82+
<form method="post">',
83+
<div class="field">',
84+
<label class="label" for="username">Username</label>',
85+
<div class="control">',
86+
<input id="username" class="input" type="text" name="username" value=""/>',
87+
</div>',
88+
</div>',
89+
<div class="field">',
90+
<label class="label" for="password">Password</label>',
91+
<div class="control">',
92+
<input id="password" class="input" type="password" name="password" value=""/>',
93+
</div>',
94+
</div>',
95+
<div class="field">',
96+
<div class="control">',
97+
<input class="button is-primary" type="submit" value="Login"/>',
98+
</div>',
99+
</div>',
100+
</form>',
101+
```
102+
103+
In the future we will add support for other frameworks, such as bootstrap 5.
104+
105+
## Backend frameworks
106+
107+
This package has been tested with the MintyPHP backend framework. It can also be used with other frameworks as this package has no dependencies at all.
108+
109+
Although we don't recommend you to use MintyPHP Forms without a framework, it is certainly possible, see the full example.
110+
111+
## Full example
112+
113+
The Form object has the following data methods:
114+
115+
- **fill**: Fill the form with an array of data (e.g. $_POST)
116+
- **validate**: Validate the form and add errors where needed
117+
- **addErrors**: Add custom errors (after validation)
118+
- **extract**: Extract the filled in form values
119+
- **render**: Output the form with or without root element
120+
121+
These data methods are typically used on GET:
122+
123+
- GET:
124+
- fill (with default values or from database)
125+
- render
126+
- POST:
127+
- fill (from POST data)
128+
- validate
129+
- on success: extract
130+
- on errors: render
131+
132+
You can see how these are used in the following full example:
133+
134+
```php
135+
<?php
136+
137+
use MintyPHP\Form\Elements as E;
138+
use MintyPHP\Form\Validator\Validators as V;
139+
140+
// include MintyPHP Forms
141+
require_once 'vendor/autoload.php';
142+
143+
// set style to Bulma
144+
E::$style = 'bulma';
145+
146+
// create a form object
147+
$form = E::form([
148+
E::field(E::text('username'), E::label('Username'), [V::required('Username is required')]),
149+
E::field(E::password('password'), E::label('Password')),
150+
E::field(E::submit('Login')),
151+
]);
152+
153+
// check if the form has been submitted
154+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
155+
// fill the form with the submitted data
156+
$form->fill($_POST);
157+
// if the form is valid, process the data
158+
if ($form->validate()) {
159+
// extract the data
160+
$data = $form->extract();
161+
// process the data (e.g., login, save to database, etc.)
162+
if ($data['username'] == 'user' && $data['password'] == 'pass') {
163+
// redirect to the dashboard page
164+
die('logged in, redirecting to dashboard...');
165+
} else {
166+
// invalidate credentials
167+
$form->addErrors([
168+
'username' => 'Invalid username/password combination',
169+
'password' => 'Invalid username/password combination',
170+
]);
171+
}
172+
}
173+
} else {
174+
// if the form is not submitted, fill it with empty values
175+
$form->fill(['username' => '', 'password' => '']);
176+
}
177+
?>
178+
<!DOCTYPE html>
179+
<html lang="en">
180+
181+
<head>
182+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/1.0.3/css/bulma.min.css">
183+
</head>
184+
185+
<body class="container">
186+
<h1 class="title">Login</h1>
187+
<?php $form->render(); ?>
188+
</body>
189+
190+
</html>
191+
```

docs/index.md

Lines changed: 0 additions & 191 deletions
This file was deleted.

0 commit comments

Comments
 (0)