File: 202_form_sensible_example.t

package info (click to toggle)
libbread-board-perl 0.37-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 716 kB
  • sloc: perl: 5,494; xml: 394; makefile: 2; sh: 1
file content (148 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl

use strict;
use warnings;

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

BEGIN { plan skip_all => 'Form::Sensible is broken at the moment' }

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;