File: 202_form_sensible_example.t

package info (click to toggle)
libbread-board-perl 0.29-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 620 kB
  • sloc: perl: 5,280; makefile: 13
file content (146 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Moose;

use Test::Requires { 'Form::Sensible' => '0.11220' };

use Bread::Board;

{
    package My::Model;
    use Moose;

    sub get_all_access_levels {
        return (
            { id => 'standard', name => 'Standard User' },
            { id => 'admin',    name => 'Administrator' },
            { id => 'super',    name => 'Super User'    },
        )
    }
}

my $FormBuilder = container 'FormBuilder' => [ 'Fields' ] => as {
    service 'Form' => (
        class => 'Form::Sensible',
        block => sub {
            my $s      = shift;
            my $c      = $s->parent;
            my $fields = $c->get_sub_container('Fields');
            my $form   = Form::Sensible::Form->new( name => $s->param('name') );
            foreach my $name ( $fields->get_service_list ) {
                $form->add_field(
                    $fields->get_service( $name )->get
                );
            }

            if ( my $state = $s->param('state') ) {
                $form->set_values( $state );
            }

            $form;
        },
        parameters => {
            name  => { isa => 'Str'                    },
            state => { isa => 'HashRef', optional => 1 },
        }
    );
};

my $Fields = container 'Fields' => [ 'Model' ] => as {

    service 'Username' => (
        class => 'Form::Sensible::Field::Text',
        block => sub {
            Form::Sensible::Field::Text->new(
                name       => 'username',
                validation => { regex => qr/^[0-9a-z]*$/ }
            );
        }
    );

    service 'Password' => (
        class => 'Form::Sensible::Field::Text',
        block => sub {
            Form::Sensible::Field::Text->new(
                name         => 'password',
                render_hints => {
                    'HTML' => {
                        field_type => 'password'
                    }
                }
            );
        }
    );

    service 'Submit' => (
        class => 'Form::Sensible::Field::Trigger',
        block => sub {
            Form::Sensible::Field::Trigger->new(
                name => 'submit'
            );
        }
    );

    service 'AccessLevel' => (
        class => 'Form::Sensible::Field::Select',
        block => sub {
            my $s = shift;
            my $select = Form::Sensible::Field::Select->new(
                 name => 'access_level',
            );
            foreach my $access_level ( $s->param('schema')->get_all_access_levels ) {
                $select->add_option(
                    $access_level->{id},
                    $access_level->{name}
                );
            }
            $select;
        },
        dependencies => {
            schema => depends_on('Model/schema') ,
        },
    );

};

# this would actually wrap the
# $c->model('DBIC') or something
# in order to get the DBIC schema
# object
my $Model = container 'Model' => as { service 'schema' => My::Model->new };

# perhaps create this in a early part
# of a catalyst dispatch chain
my $Form = $FormBuilder->create(
    Fields => $Fields->create(
        Model => $Model
    )
);

# then in the actual action code
# you would create the form instance
# and pass the state (which is
# basically $c->req->parameters)
my $f = $Form->resolve(
    service    => 'Form',
    parameters => {
        name  => 'test',
        state => {
            username     => 'stevan',
            password     => '****',
            access_level => [ 'admin' ]
        }
    }
);
isa_ok($f, 'Form::Sensible::Form');

my $result = $f->validate;

ok( $result->is_valid, '... our form validated' );


done_testing;