1+ <?php
2+
3+ // alias the most used classes
4+ use MintyPHP \Form \Elements as E ;
5+ use MintyPHP \Form \Validator \Validators as V ;
6+
7+ // ensure all classes are (auto)loaded:
8+ require_once __DIR__ . '/../../vendor/autoload.php ' ;
9+
10+ // set style to Bulma
11+ E::$ style = 'bulma ' ;
12+
13+ // create a form object
14+ $ form = E::form ([
15+ E::field (E::selectOrType ('room ' , ['Bathroom ' , 'Kitchen ' ], 'Type a room name ... ' )->required (), E::label ('Select a room ' )),
16+ E::field (E::submit ('Save ' )),
17+ ]);
18+
19+ // check if the form has been submitted
20+ if ($ _SERVER ['REQUEST_METHOD ' ] == 'POST ' ) {
21+ // fill the form with the submitted data
22+ $ form ->fill ($ _POST );
23+ // if the form is valid, process the data
24+ if ($ form ->validate ()) {
25+ // extract the data
26+ $ data = $ form ->extract ();
27+ // process the data (e.g., login, save to database, etc.)
28+ if (strtolower ($ data ['room ' ]) == 'living ' ) {
29+ // redirect to the dashboard page
30+ die ('OK ' );
31+ } else {
32+ // invalidate credentials
33+ $ form ->addErrors ([
34+ 'room ' => 'Invalid room name, try "Living" ' ,
35+ ]);
36+ }
37+ }
38+ } else {
39+ // if the form is not submitted, fill it with empty values
40+ $ form ->fill ([]);
41+ }
42+ ?>
43+ <!DOCTYPE html>
44+ <html lang="en">
45+
46+ <head>
47+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/1.0.3/css/bulma.min.css">
48+ </head>
49+
50+ <body class="container p-5">
51+ <?php $ form ->render (); ?>
52+ </body>
53+
54+ </html>
0 commit comments