| 1 | package HTML::Shakan; | ||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 22 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 22 | ▶ | use Mouse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 22 | our $VERSION = '2.00'; | |||||||||||||||||||
| 6 | 22 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 22 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 22 | ▶ | use FormValidator::Lite 'Email', 'URL', 'Date', 'File'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 22 | ▶ | use Hash::MultiValue; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 22 | ▶ | use HTML::Shakan::Renderer::HTML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 22 | ▶ | use HTML::Shakan::Filters; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 22 | ▶ | use HTML::Shakan::Widgets::Simple; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 22 | ▶ | use HTML::Shakan::Fields; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 22 | ▶ | use HTML::Shakan::Field::Input; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 22 | ▶ | use HTML::Shakan::Field::Date; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 22 | ▶ | use HTML::Shakan::Field::Choice; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 22 | ▶ | use HTML::Shakan::Field::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 22 | ▶ | use List::MoreUtils 'uniq'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub import { | ||||||||||||||||||||
| 23 | 20 | ▶ | HTML::Shakan::Fields->export_to_level(1); | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | has '_fvl' => ( | ||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||
| 28 | isa => 'FormValidator::Lite', | ||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||
| 30 | handles => [qw/has_error load_function_message get_error_messages is_error is_valid set_error set_message/], | ||||||||||||||||||||
| 31 | default => sub { | ||||||||||||||||||||
| 32 | 53 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 53 | $self->params(); # build laziness data | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 53 | FormValidator::Lite->new($self); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 22 | ); | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub BUILD { | ||||||||||||||||||||
| 40 | 53 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 53 | my $fvl = $self->_fvl; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # simple check | ||||||||||||||||||||
| 45 | 53 | $fvl->check(do { | |||||||||||||||||||
| 46 | 53 | my @c; | |||||||||||||||||||
| 47 | 53 | for my $field (@{ $self->fields }) { | |||||||||||||||||||
| 48 | 62 | push @c, $field->get_constraints(); | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 53 | @c; | |||||||||||||||||||
| 51 | }); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # run custom validation | ||||||||||||||||||||
| 54 | 53 | ▶ | if (my $cv = $self->custom_validation) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 2 | $cv->( $self ); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 53 | for my $field ($self->fields) { | |||||||||||||||||||
| 58 | 62 | ▶ | if (my $cv = $field->custom_validation) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 2 | $cv->($self, $field); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 53 | ▶ | if ($fvl->is_valid) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 33 | $self->_inflate_values(); | |||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||
| 66 | 20 | $fvl->set_param_message( | |||||||||||||||||||
| 67 | $self->_set_error_messages() | ||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 22 | has custom_validation => ( | |||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||
| 74 | isa => 'CodeRef', | ||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | sub _set_error_messages { | ||||||||||||||||||||
| 78 | 20 | ▶ | my ($self, ) = @_; | ||||||||||||||||||
|
Subroutine
_set_error_messages: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 20 | my %x; | |||||||||||||||||||
| 81 | 20 | for my $field ($self->fields) { | |||||||||||||||||||
| 82 | 27 | ▶ | $x{$field->name} = $field->label || $field->name; | ||||||||||||||||||
|
Condition33%1/3
$field->label || $field->name
MC/DC0%0/2
$field->label || $field->name
$field->label
$field->name
Truth table33%1/3
A || B
A= $field->labelB= $field->name
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 20 | %x; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | sub _inflate_values { | ||||||||||||||||||||
| 88 | 33 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_inflate_values: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # inflate values | ||||||||||||||||||||
| 91 | 33 | my $params = $self->params; | |||||||||||||||||||
| 92 | 33 | for my $field (@{ $self->fields }) { | |||||||||||||||||||
| 93 | 35 | ▶ | if (my $inf = $field->inflator) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 1 | my $v = $params->{$field->name}; | |||||||||||||||||||
| 95 | 1 | ▶ | if (defined $v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 1 | $params->{$field->name} = $inf->inflate($v); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 22 | has 'submitted' => ( | |||||||||||||||||||
| 103 | is => 'ro', | ||||||||||||||||||||
| 104 | isa => 'Bool', | ||||||||||||||||||||
| 105 | lazy => 1, | ||||||||||||||||||||
| 106 | builder => '_build_submitted', | ||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||
| 108 | sub _build_submitted { | ||||||||||||||||||||
| 109 | 5 | ▶ | my ($self, ) = @_; | ||||||||||||||||||
|
Subroutine
_build_submitted: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 5 | my $r = $self->request; | |||||||||||||||||||
| 112 | my $submitted_field = ( | ||||||||||||||||||||
| 113 | scalar | ||||||||||||||||||||
| 114 | 7 | ▶ | grep { defined $r->param($_) || defined $r->upload($_) } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | uniq | ||||||||||||||||||||
| 116 | 5 | map { $_->name } | |||||||||||||||||||
| 117 | $self->fields | ||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||
| 119 | 5 | ▶ | return $submitted_field > 0 ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | sub submitted_and_valid { | ||||||||||||||||||||
| 123 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
submitted_and_valid: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 124 | 2 | ▶ | $self->submitted && $self->is_valid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | has model => ( | ||||||||||||||||||||
| 128 | is => 'rw', | ||||||||||||||||||||
| 129 | isa => 'Object', | ||||||||||||||||||||
| 130 | trigger => sub { | ||||||||||||||||||||
| 131 | 0 | ▶ | my ($self, $model) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 132 | 0 | $model->form($self); | |||||||||||||||||||
| 133 | 0 | $model; | |||||||||||||||||||
| 134 | }, | ||||||||||||||||||||
| 135 | 22 | ); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 22 | has renderer => ( | |||||||||||||||||||
| 138 | is => 'rw', | ||||||||||||||||||||
| 139 | isa => 'Object', | ||||||||||||||||||||
| 140 | builder => '_build_renderer', | ||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||
| 142 | sub _build_renderer { | ||||||||||||||||||||
| 143 | 53 | ▶ | HTML::Shakan::Renderer::HTML->new(); | ||||||||||||||||||
|
Subroutine
_build_renderer: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | sub render { | ||||||||||||||||||||
| 146 | 9 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 147 | 9 | $self->renderer()->render($self); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | sub render_field { | ||||||||||||||||||||
| 151 | 1 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||
|
Subroutine
render_field: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 152 | 1 | my ( $field, ) = grep { $_->name eq $name } $self->fields; | |||||||||||||||||||
| 153 | 1 | ▶ | return unless $field; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 1 | return $self->widgets->render( $self, $field ); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | sub fillin_param { | ||||||||||||||||||||
| 158 | 35 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||
|
Subroutine
fillin_param: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 159 | 35 | $self->fillin_params->{$key}; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | has fillin_params => ( | ||||||||||||||||||||
| 162 | is => 'ro', | ||||||||||||||||||||
| 163 | isa => 'HashRef', | ||||||||||||||||||||
| 164 | lazy => 1, | ||||||||||||||||||||
| 165 | default => sub { | ||||||||||||||||||||
| 166 | 13 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||
| 167 | 13 | my $fp = {}; | |||||||||||||||||||
| 168 | 13 | for my $name ($self->request->param) { | |||||||||||||||||||
| 169 | 11 | my @v = $self->request->param($name); | |||||||||||||||||||
| 170 | 11 | ▶ | if (@v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 11 | ▶ | $fp->{$name} = @v==1 ? $v[0] : \@v; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | 13 | $fp; | |||||||||||||||||||
| 175 | }, | ||||||||||||||||||||
| 176 | 22 | ); | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 22 | has fields => ( | |||||||||||||||||||
| 179 | is => 'ro', | ||||||||||||||||||||
| 180 | isa => 'ArrayRef', | ||||||||||||||||||||
| 181 | required => 1, | ||||||||||||||||||||
| 182 | auto_deref => 1, | ||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 22 | has request => ( | |||||||||||||||||||
| 186 | is => 'ro', | ||||||||||||||||||||
| 187 | isa => 'Object', | ||||||||||||||||||||
| 188 | required => 1, | ||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 22 | has 'widgets' => ( | |||||||||||||||||||
| 192 | is => 'ro', | ||||||||||||||||||||
| 193 | isa => 'Str', | ||||||||||||||||||||
| 194 | default => 'HTML::Shakan::Widgets::Simple', | ||||||||||||||||||||
| 195 | ); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 22 | has 'params' => ( | |||||||||||||||||||
| 198 | is => 'rw', | ||||||||||||||||||||
| 199 | isa => 'Hash::MultiValue', | ||||||||||||||||||||
| 200 | lazy => 1, | ||||||||||||||||||||
| 201 | builder => '_build_params', | ||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | has 'uploads' => ( | ||||||||||||||||||||
| 205 | is => 'rw', | ||||||||||||||||||||
| 206 | isa => 'HashRef', | ||||||||||||||||||||
| 207 | 53 | ▶ | default => sub { +{} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 208 | 22 | ); | |||||||||||||||||||
| 209 | sub upload { | ||||||||||||||||||||
| 210 | 2 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||
|
Subroutine
upload: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 211 | 2 | $self->uploads->{$name}; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | # code taken from MooseX::Param and changed a bit | ||||||||||||||||||||
| 215 | sub param { | ||||||||||||||||||||
| 216 | 127 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
param: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 127 | my $params = $self->params; | |||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | # if they want the list of keys ... | ||||||||||||||||||||
| 221 | 127 | ▶ | return $params->keys if scalar @_ == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | # if they want to fetch a particular key ... | ||||||||||||||||||||
| 224 | 127 | ▶ | if (scalar @_ == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | 126 | ▶ | return wantarray ? $params->get_all($_[0]) : $params->get($_[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 1 | ▶ | ( ( scalar @_ % 2 ) == 0 ) || confess "parameter assignment must be an even numbered list"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 1 | my %new = @_; | |||||||||||||||||||
| 231 | 1 | ▶ | while ( my ( $key, $value ) = each %new ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 232 | 1 | ▶ | my @values = ref $value eq 'ARRAY' ? @$value : ($value); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | 1 | $self->params->set($key, @values); | |||||||||||||||||||
| 234 | } | ||||||||||||||||||||
| 235 | 1 | return; | |||||||||||||||||||
| 236 | } | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | sub _build_params { | ||||||||||||||||||||
| 239 | 53 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_params: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 240 | 53 | my $params = {}; | |||||||||||||||||||
| 241 | 53 | for my $field (@{$self->fields}) { | |||||||||||||||||||
| 242 | 62 | ▶ | if ($self->widgets->can('field_filter')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 243 | # e.g. DateField | ||||||||||||||||||||
| 244 | 62 | $self->widgets->field_filter($self, $field, $params); | |||||||||||||||||||
| 245 | } | ||||||||||||||||||||
| 246 | 62 | ▶ | if ($field->can('field_filter')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 247 | # e.g. FileField | ||||||||||||||||||||
| 248 | 6 | $field->field_filter($self, $params); | |||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | 62 | my $name = $field->name; | |||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | 62 | my @val = $self->request->param($name); | |||||||||||||||||||
| 254 | 62 | ▶ | if (@val != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 255 | 41 | ▶ | if ( my $filters = $field->{filters} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 256 | 41 | @val = map { HTML::Shakan::Filters->filter( $filters, $_ ) } @val; | |||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | 41 | ▶ | $params->{$name} = @val==1 ? $val[0] : \@val; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 259 | } | ||||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | 53 | Hash::MultiValue->from_mixed($params); | |||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 22 | ▶ | no Mouse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 266 | 22 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 267 | __END__ | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | =encoding utf-8 | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | =for stopwords shakan edokko login sakan | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | =head1 NAME | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | HTML::Shakan - Form HTML generator/validator | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | =head1 SYNOPSIS | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | use HTML::Shakan; | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | sub form { | ||||||||||||||||||||
| 282 | my $req = shift; | ||||||||||||||||||||
| 283 | HTML::Shakan->new( | ||||||||||||||||||||
| 284 | fields => [ @_ ], | ||||||||||||||||||||
| 285 | request => $req, | ||||||||||||||||||||
| 286 | model => 'DataModel', | ||||||||||||||||||||
| 287 | ); | ||||||||||||||||||||
| 288 | } | ||||||||||||||||||||
| 289 | sub edit { | ||||||||||||||||||||
| 290 | my $req = shift; | ||||||||||||||||||||
| 291 | my $row = $model->get('user' => $req->param('id')); | ||||||||||||||||||||
| 292 | my $form = form( | ||||||||||||||||||||
| 293 | $req => ( | ||||||||||||||||||||
| 294 | TextField(name => 'name', label => 'Your name', filter => [qw/WhiteSpace/]), | ||||||||||||||||||||
| 295 | EmailField(name => 'email', label => 'Your email'), | ||||||||||||||||||||
| 296 | ), | ||||||||||||||||||||
| 297 | ); | ||||||||||||||||||||
| 298 | if ($req->submitted_and_valid) { | ||||||||||||||||||||
| 299 | $form->model->update($row); | ||||||||||||||||||||
| 300 | redirect('edit_thanks'); | ||||||||||||||||||||
| 301 | } else { | ||||||||||||||||||||
| 302 | $form->model->fill($row); | ||||||||||||||||||||
| 303 | render(form => $form); | ||||||||||||||||||||
| 304 | } | ||||||||||||||||||||
| 305 | } | ||||||||||||||||||||
| 306 | sub add { | ||||||||||||||||||||
| 307 | my $req = shift; | ||||||||||||||||||||
| 308 | my $form = form( | ||||||||||||||||||||
| 309 | $req => ( | ||||||||||||||||||||
| 310 | TextField(name => 'name', label => 'Your name'), | ||||||||||||||||||||
| 311 | EmailField(name => 'email', label => 'Your email'), | ||||||||||||||||||||
| 312 | ) | ||||||||||||||||||||
| 313 | ); | ||||||||||||||||||||
| 314 | if ($req->submitted_and_valid) { | ||||||||||||||||||||
| 315 | $form->model->insert($model => 'user'); | ||||||||||||||||||||
| 316 | redirect('edit_thanks'); | ||||||||||||||||||||
| 317 | } | ||||||||||||||||||||
| 318 | render(form => $form); | ||||||||||||||||||||
| 319 | } | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | # in your template | ||||||||||||||||||||
| 322 | <? if ($form->has_error) { ?><div class="error"><?= $form->error_message() ?></div><? } ?> | ||||||||||||||||||||
| 323 | <form method="post" action="add"> | ||||||||||||||||||||
| 324 | <?= $form->render() ?> | ||||||||||||||||||||
| 325 | <p><input type="submit" value="add" /></p> | ||||||||||||||||||||
| 326 | </form> | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | =head1 DESCRIPTION | ||||||||||||||||||||
| 329 | |||||||||||||||||||||
| 330 | HTML::Shakan is yet another form generator. | ||||||||||||||||||||
| 331 | |||||||||||||||||||||
| 332 | THIS IS BETA.API WILL CHANGE. | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 335 | |||||||||||||||||||||
| 336 | =over 4 | ||||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | =item C<custom_validation> | ||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | form 'login' => ( | ||||||||||||||||||||
| 341 | fields => [ | ||||||||||||||||||||
| 342 | TextField(name => 'login_id'), | ||||||||||||||||||||
| 343 | PasswordField(name => 'login_pw'), | ||||||||||||||||||||
| 344 | ], | ||||||||||||||||||||
| 345 | custom_validation => sub { | ||||||||||||||||||||
| 346 | my $form = shift; | ||||||||||||||||||||
| 347 | if ($form->is_valid && !MyDB->retrieve($form->param('login_id'), $form->param('login_pw'))) { | ||||||||||||||||||||
| 348 | $form->set_error('login' => 'failed'); | ||||||||||||||||||||
| 349 | } | ||||||||||||||||||||
| 350 | } | ||||||||||||||||||||
| 351 | ); | ||||||||||||||||||||
| 352 | |||||||||||||||||||||
| 353 | You can set custom validation callback, validates the field set in the form. For example, this is useful for login form. | ||||||||||||||||||||
| 354 | |||||||||||||||||||||
| 355 | =item C<submitted> | ||||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | Returns true if the form has been submitted. | ||||||||||||||||||||
| 358 | |||||||||||||||||||||
| 359 | This attribute will return true if a value for any known field name was submitted. | ||||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | =item C<has_error> | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | Return true if request has an error. | ||||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | =item C<submitted_and_valid> | ||||||||||||||||||||
| 366 | |||||||||||||||||||||
| 367 | Shorthand for C<< $form->submitted && !$form->has_error >> | ||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | =item C<params> | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | Returns form parameters. It is L<Hash::MultiValue> object. | ||||||||||||||||||||
| 372 | |||||||||||||||||||||
| 373 | =back | ||||||||||||||||||||
| 374 | |||||||||||||||||||||
| 375 | =head1 benchmarking | ||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | form generation | ||||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | Rate formfu shakan shakan_declare | ||||||||||||||||||||
| 380 | formfu 1057/s -- -77% -84% | ||||||||||||||||||||
| 381 | shakan 4695/s 344% -- -31% | ||||||||||||||||||||
| 382 | shakan_declare 6757/s 539% 44% -- | ||||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | =head1 What's shakan | ||||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | Shakan is 左官 in Japanese. | ||||||||||||||||||||
| 387 | |||||||||||||||||||||
| 388 | If you want to know about shakan, please see L<http://ja.wikipedia.org/wiki/%E5%B7%A6%E5%AE%98> | ||||||||||||||||||||
| 389 | |||||||||||||||||||||
| 390 | 左官 should pronounce 'sakan', formally. but, edokko pronounce 左官 as shakan. | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | =head1 METHODS | ||||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | =over 4 | ||||||||||||||||||||
| 395 | |||||||||||||||||||||
| 396 | =item C<< my $html = $shakan->render(); :Str >> | ||||||||||||||||||||
| 397 | |||||||||||||||||||||
| 398 | Render form. | ||||||||||||||||||||
| 399 | |||||||||||||||||||||
| 400 | =item C<< $shakan->render_field($name); :Str >> | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | Render partial form named C<<$name>>. | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | =item C<< $shakan->param($key:Str); :Value[s] >> | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | Retrive the value of the key from parameters. It's behaviour is similar to traditional request objects. (ex. CGI, Plack::Request) | ||||||||||||||||||||
| 407 | That is, it returns single scalar at scalar context and returns array at array context. | ||||||||||||||||||||
| 408 | |||||||||||||||||||||
| 409 | =back | ||||||||||||||||||||
| 410 | |||||||||||||||||||||
| 411 | =head1 AUTHOR | ||||||||||||||||||||
| 412 | |||||||||||||||||||||
| 413 | Tokuhiro Matsuno E<lt>tokuhirom @ gmail.comE<gt> | ||||||||||||||||||||
| 414 | |||||||||||||||||||||
| 415 | =head1 SEE ALSO | ||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | L<HTML::FormFu> | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | ToscaWidgets | ||||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | =head1 LICENSE | ||||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 424 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 425 | |||||||||||||||||||||
| 426 | =cut | ||||||||||||||||||||