Standalone Form for PHP

Yesterday I wrote about Standalone Forms and Validation. It may need a little bit knowledge on how the Aura.Filter works. But that is a good start if you want to know how you can integrate the pieces of Aura.

Today I think I need to show you the very minimal approach you can take to build the form and validate your form. If you want to use a powerful validation and filtering use something like Aura.Filter or bind your favourite components which does the validation Respect, Symfony2 Validator, valitron and filtering with something like DMS-Filter.

You can see how you can integrate with different components as I did with Aura.Filter in the example in master branch.

I assume you know a bit about composer and how to install.

The composer.json file

1
2
3
4
5
6
7
{
    "minimum-stability": "dev",
    "require": {
        "aura/input": "1.0.0-beta1",
        "aura/view": "1.1.2"
    }
}

The page which you need to bring the form.

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
<?php

use Aura\Input\Builder;
use Aura\Input\Filter;
use Aura\Input\Form;

$loader = require __DIR__ . '/vendor/autoload.php';

class ContactForm extends Form
{
    public function init()
    {
        $this->setField('name')
            ->setAttribs([
                'id' => 'name',
                'size' => 20,
                'maxlength' => 20,
            ]);
        $this->setField('email')
            ->setAttribs([
                'size' => 20,
                'maxlength' => 20,
            ]);
        $this->setField('url')
            ->setAttribs([
                'size' => 20,
                'maxlength' => 20,
            ]);
        $this->setField('message', 'textarea')
            ->setAttribs([
                'cols' => 40,
                'rows' => 5,
            ]);
        $this->setField('submit', 'submit')
            ->setAttribs(['value' => 'send']);

        $filter = $this->getFilter();

        $filter->setRule(
            'name',
            'Name must be alphabetic only.',
            function ($value) {
                return ctype_alpha($value);
            }
        );

        $filter->setRule('email', 'Enter a valid email address', function ($value) {
            return filter_var($value, FILTER_VALIDATE_EMAIL);
        });

        $filter->setRule('url', 'Enter a valid url', function ($value) {
            return filter_var($value, FILTER_VALIDATE_URL);
        });

        $filter->setRule('message', 'Message should be more than 7 characters', function ($value) {
            if (strlen($value) > 7) {
                return true;
            }
            return false;
        });
    }
}

$form = new ContactForm(new Builder(), new Filter());

if ($_POST && $_POST['submit'] == 'send') {
    $data = $_POST;
    $form->fill($data);
    if ($form->filter()) {
        //
        echo "Yes successfully validated and filtered";
        var_dump($data);
        exit;
    }
}

$helper = new Aura\View\HelperLocator([
    'field'         => function () {
        return new Aura\View\Helper\Form\Field(
            require dirname(__DIR__) . '/vendor/aura/view/scripts/field_registry.php'
        );
    },
    'input'         => function () { return new Aura\View\Helper\Form\Input(
            require dirname(__DIR__) . '/vendor/aura/view/scripts/input_registry.php'
        );
    },
    'radios'        => function () { return new Aura\View\Helper\Form\Radios(new Aura\View\Helper\Form\Input\Checked); },
    'repeat'         => function () { return new Aura\View\Helper\Form\Repeat(
            require dirname(__DIR__) . '/vendor/aura/view/scripts/repeat_registry.php'
        );
    },
    'select'        => function () { return new Aura\View\Helper\Form\Select; },
    'textarea'      => function () { return new Aura\View\Helper\Form\Textarea; },
]);

$field = $helper->get('field');
function showFieldErrors($form, $name) {
    $errors = $form->getMessages($name);
    $str = '';
    if ($errors) {
        $str .= '<ul>';
        foreach ($errors as $error) {
            $str .= '<li>' . $error . '</li>';
        }
        $str .= '</ul>';
    }
    return $str;
}
?>
<html>
<head>
    <title>Aura Form, to make it standalone</title>
</head>
<body>
    <form method="post" action="" enctype="multipart/form-data" >
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td>Name : </td>
                <td>
                <?php
                    echo $field($form->get('name'));
                    echo showFieldErrors($form, 'name');
                ?>
                </td>
            </tr>
            <tr>
                <td>Email : </td>
                <td>
                <?php
                    echo $field($form->get('email'));
                    echo showFieldErrors($form, 'email');
                ?>
                </td>
            </tr>
            <tr>
                <td>Url : </td>
                <td>
                <?php
                    echo $field($form->get('url'));
                    echo showFieldErrors($form, 'url');
                ?>
                </td>
            </tr>
            <tr>
                <td>Message : </td>
                <td>
                <?php
                    echo $field($form->get('message'));
                    echo showFieldErrors($form, 'message');
                ?>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                <?php
                echo $field($form->get('submit'));
                ?>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

That’s it. Pretty clear I guess. Still having trouble? The whole code lies in the minimalview branch of the phpform.

auraphp, form, input, validation

Comments