Aura Turns 2

Looking over the commits on Aura.Router, Aura.Signal etc you will notice, aura project has turned 2.

And today, I would like to introduce you, the new born baby still under active development and refactoring based on user feedback, the form library for php, Aura.Input.

The Aura.Input, doesn’t have a rendering functionality. But you can always use Aura.View or create your own helpers.

A basic filtering based on closure exists in Aura.Input. But you are not limited, you can use your own filtering components or integrate Aura.Filter.

Let us look at some code.

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
<?php
// use composer or require '/path/to/Aura.Input/src.php';

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

$filter = new Aura\Input\Filter();

// validate
$filter->setRule('name', 'Name should be alpha 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;
});

class ContactForm extends Form
{
    public function init()
    {
        $name    = $this->setField('name');
        $email   = $this->setField('email');
        $url     = $this->setField('url');
        $message = $this->setField('message', 'textarea');
    }
}

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

$values = [
    'name' => 'Hari K T',
    'email' => 'oh will it works!',
    'url' => 'google.com',
    'message' => 'Aweso'
];

$form->setValues($values);

$passed = $form->filter();

// 'foo' is invalid
if (! $passed) {
    // get all messages
    $actual = $form->getMessages();
    var_dump($actual);
}

Try it out, we have some more documentation for the Aura.Input. I warn the api is still not stable for Aura.Input and is not yet released a Beta.

But it is a good start!

auraphp, php

Comments