| 1 | package HTML::Widget::Result; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw/HTML::Widget::Accessor/; | ||
| 6 | use HTML::Widget::Container; | ||
| 7 | use HTML::Widget::Error; | ||
| 8 | use HTML::Element; | ||
| 9 | use Storable 'dclone'; | ||
| 10 | use Carp qw/carp croak/; | ||
| 11 | |||
| 12 | __PACKAGE__->mk_accessors( | ||
| 13 | qw/attributes container subcontainer strict submitted | ||
| 14 | element_container_class implicit_subcontainer/ | ||
| 15 | ); | ||
| 16 | __PACKAGE__->mk_attr_accessors(qw/action enctype id method empty_errors/); | ||
| 17 | |||
| 18 | use overload '""' => sub { return shift->as_xml }, fallback => 1; | ||
| 19 | |||
| 20 | *attrs = \&attributes; | ||
| 21 | *name = \&id; | ||
| 22 | *error = \&errors; | ||
| 23 | *has_error = \&has_errors; | ||
| 24 | *have_errors = \&has_errors; | ||
| 25 | *element = \&elements; | ||
| 26 | *parameters = \¶ms; | ||
| 27 | *tag = \&container; | ||
| 28 | *subtag = \&subcontainer; | ||
| 29 | *is_submitted = \&submitted; | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | HTML::Widget::Result - Result Class | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | see L<HTML::Widget> | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | Result Class. | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | =head2 action | ||
| 46 | |||
| 47 | Arguments: $action | ||
| 48 | |||
| 49 | Return Value: $action | ||
| 50 | |||
| 51 | Contains the form action. | ||
| 52 | |||
| 53 | =head2 as_xml | ||
| 54 | |||
| 55 | Return Value: $xml | ||
| 56 | |||
| 57 | Returns xml. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub as_xml { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | my $element_container_class = $self->{element_container_class}; | ||
| 65 | |||
| 66 | my $c = HTML::Element->new( $self->container ); | ||
| 67 | |||
| 68 | $c->attr( $_ => ${ $self->attributes }{$_} ) | ||
| 69 | for ( keys %{ $self->attributes } ); | ||
| 70 | |||
| 71 | my $params = dclone $self->{_params}; | ||
| 72 | |||
| 73 | for my $element ( | ||
| 74 | $self->_get_elements( | ||
| 75 | $self->{_elements}, $params, $element_container_class | ||
| 76 | ) ) | ||
| 77 | { | ||
| 78 | $c->push_content( $element->as_list ) unless $element->passive; | ||
| 79 | } | ||
| 80 | return $c->as_XML; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 container | ||
| 84 | |||
| 85 | Arguments: $tag | ||
| 86 | |||
| 87 | Return Value: $tag | ||
| 88 | |||
| 89 | Contains the container tag. | ||
| 90 | |||
| 91 | =head2 enctype | ||
| 92 | |||
| 93 | Arguments: $enctype | ||
| 94 | |||
| 95 | Return Value: $enctype | ||
| 96 | |||
| 97 | Contains the form encoding type. | ||
| 98 | |||
| 99 | =head2 errors | ||
| 100 | |||
| 101 | =head2 error | ||
| 102 | |||
| 103 | Arguments: $name, $type | ||
| 104 | |||
| 105 | Return Value: @errors | ||
| 106 | |||
| 107 | Returns a list of L<HTML::Widget::Error> objects. | ||
| 108 | |||
| 109 | my @errors = $form->errors; | ||
| 110 | my @errors = $form->errors('foo'); | ||
| 111 | my @errors = $form->errors( 'foo', 'ASCII' ); | ||
| 112 | |||
| 113 | L</error> is an alias for L</errors>. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub errors { | ||
| 118 | my ( $self, $name, $type ) = @_; | ||
| 119 | |||
| 120 | return 0 if $name && !$self->{_errors}->{$name}; | ||
| 121 | |||
| 122 | my $errors = []; | ||
| 123 | my @names = $name || keys %{ $self->{_errors} }; | ||
| 124 | for my $n (@names) { | ||
| 125 | for my $error ( @{ $self->{_errors}->{$n} } ) { | ||
| 126 | next if $type && $error->{type} ne $type; | ||
| 127 | push @$errors, $error; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | return @$errors; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 elements | ||
| 134 | |||
| 135 | =head2 element | ||
| 136 | |||
| 137 | Arguments: $name (optional) | ||
| 138 | |||
| 139 | Return Value: @elements | ||
| 140 | |||
| 141 | If C<$name> argument is supplied, returns a L<HTML::Widget::Container> | ||
| 142 | object for the first element matching C<$name>. Otherwise, returns a list | ||
| 143 | of L<HTML::Widget::Container> objects for all elements. | ||
| 144 | |||
| 145 | my @form = $f->elements; | ||
| 146 | my $age = $f->elements('age'); | ||
| 147 | |||
| 148 | L</element> is an alias for L</elements>. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub elements { | ||
| 153 | my ( $self, $name ) = @_; | ||
| 154 | |||
| 155 | my $params = dclone $self->{_params}; | ||
| 156 | |||
| 157 | if ( $self->implicit_subcontainer ) { | ||
| 158 | return $self->_get_elements( | ||
| 159 | $self->{_elements}->[0]->content, $params, | ||
| 160 | $self->{element_container_class}, $name | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | return $self->_get_elements( $self->{_elements}, $params, | ||
| 165 | $self->{element_container_class}, $name ); | ||
| 166 | } | ||
| 167 | |||
| 168 | =head2 elements_ref | ||
| 169 | |||
| 170 | Arguments: $name (optional) | ||
| 171 | |||
| 172 | Return Value: \@elements | ||
| 173 | |||
| 174 | Accepts the same arguments as L</elements>, but returns an arrayref | ||
| 175 | of results instead of a list. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub elements_ref { | ||
| 180 | my $self = shift; | ||
| 181 | |||
| 182 | return [ $self->elements(@_) ]; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 find_result_element | ||
| 186 | |||
| 187 | Arguments: $name | ||
| 188 | |||
| 189 | Return Value: @elements | ||
| 190 | |||
| 191 | Looks for the named element and returns a L<HTML::Widget::Container> | ||
| 192 | object for it if found. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub find_result_element { | ||
| 197 | my ( $self, $name ) = @_; | ||
| 198 | |||
| 199 | my @elements = $self->find_elements( name => $name ); | ||
| 200 | return unless @elements; | ||
| 201 | |||
| 202 | my $params = dclone $self->{_params}; | ||
| 203 | |||
| 204 | return $self->_get_elements( [ $elements[0] ], | ||
| 205 | $params, $self->{element_container_class}, $name ); | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 elements_for | ||
| 209 | |||
| 210 | Arguments: $name | ||
| 211 | |||
| 212 | Return Value: @elements | ||
| 213 | |||
| 214 | If the named element is a Block or NullContainer element, return a list | ||
| 215 | of L<HTML::Widget::Container> objects for the contents of that element. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub elements_for { | ||
| 220 | my ( $self, $name ) = @_; | ||
| 221 | |||
| 222 | my @elements = $self->find_elements( name => $name ); | ||
| 223 | return unless @elements; | ||
| 224 | |||
| 225 | my $ble = $elements[0]; | ||
| 226 | return unless $ble->isa('HTML::Widget::Element::NullContainer'); | ||
| 227 | |||
| 228 | my $params = dclone $self->{_params}; | ||
| 229 | |||
| 230 | return $self->_get_elements( $ble->content, $params, | ||
| 231 | $self->{element_container_class} ); | ||
| 232 | } | ||
| 233 | |||
| 234 | # code reuse++ | ||
| 235 | sub _get_elements { | ||
| 236 | my ( $self, $elements, $params, $element_container_class, $name ) = @_; | ||
| 237 | |||
| 238 | my %javascript; | ||
| 239 | for my $js_callback ( @{ $self->{_js_callbacks} } ) { | ||
| 240 | my $javascript = $js_callback->( $self->name ); | ||
| 241 | for my $key ( keys %$javascript ) { | ||
| 242 | $javascript{$key} .= $javascript->{$key} if $javascript->{$key}; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | # the hashref of args is carried through - recursively as necessary | ||
| 247 | # - to _containerize_elements(). | ||
| 248 | return $self->_containerize_elements( | ||
| 249 | $elements, | ||
| 250 | { name => $name, | ||
| 251 | params => $params, | ||
| 252 | element_container_class => $element_container_class, | ||
| 253 | javascript => \%javascript, | ||
| 254 | toplevel => 1, | ||
| 255 | submitted => $self->submitted, | ||
| 256 | } ); | ||
| 257 | } | ||
| 258 | |||
| 259 | # also called by HTML::Element::Block, so code reuse++ again | ||
| 260 | sub _containerize_elements { | ||
| 261 | my ( $self, $elements, $argsref ) = @_; | ||
| 262 | |||
| 263 | my $args = dclone $argsref; # make copy to pass on | ||
| 264 | my ( $element_container_class, $javascript, $name, $params, $toplevel ) | ||
| 265 | = @$args{qw(element_container_class javascript name params toplevel)}; | ||
| 266 | delete $args->{toplevel}; | ||
| 267 | |||
| 268 | my @content; | ||
| 269 | for my $element (@$elements) { | ||
| 270 | local $element->{container_class} = $element_container_class | ||
| 271 | if $element_container_class; | ||
| 272 | local $element->{_anonymous} = 1 | ||
| 273 | if ( $self->implicit_subcontainer and $toplevel ); | ||
| 274 | my ( $value, $error ) = ( undef, undef ); | ||
| 275 | my $ename = $element->{name}; | ||
| 276 | $value = $params->{$ename} if ( defined($ename) && $params ); | ||
| 277 | next if ( defined($name) && defined($ename) && ( $ename ne $name ) ); | ||
| 278 | $value = $params->{$ename} if ( defined($ename) && $params ); | ||
| 279 | $error = $self->{_errors}->{$ename} if defined $ename; | ||
| 280 | my $container = $element->containerize( $self, $value, $error, $args ); | ||
| 281 | $container->{javascript} ||= ''; | ||
| 282 | $container->{javascript} .= $javascript->{$ename} | ||
| 283 | if ( $ename and $javascript->{$ename} ); | ||
| 284 | return $container if defined $name; | ||
| 285 | push @content, $container; | ||
| 286 | } | ||
| 287 | return @content; | ||
| 288 | } | ||
| 289 | |||
| 290 | =head2 find_elements | ||
| 291 | |||
| 292 | Return Value: @elements | ||
| 293 | |||
| 294 | Exactly the same as L<HTML::Widget/find_elements> | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | sub find_elements { | ||
| 299 | |||
| 300 | # WARNING: Not safe for subclassing | ||
| 301 | return shift->HTML::Widget::find_elements(@_); | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 empty_errors | ||
| 305 | |||
| 306 | Arguments: $bool | ||
| 307 | |||
| 308 | Return Value: $bool | ||
| 309 | |||
| 310 | Create spans for errors even when there's no errors.. (For AJAX validation validation) | ||
| 311 | |||
| 312 | =head2 has_errors | ||
| 313 | |||
| 314 | =head2 has_error | ||
| 315 | |||
| 316 | =head2 have_errors | ||
| 317 | |||
| 318 | Arguments: $name | ||
| 319 | |||
| 320 | Return Value: $bool | ||
| 321 | |||
| 322 | Returns a list of element names. | ||
| 323 | |||
| 324 | my @names = $form->has_errors; | ||
| 325 | my $error = $form->has_errors($name); | ||
| 326 | |||
| 327 | L</has_error> and L</have_errors> are aliases for L</has_errors>. | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | sub has_errors { | ||
| 332 | my ( $self, $name ) = @_; | ||
| 333 | my @names = keys %{ $self->{_errors} }; | ||
| 334 | return @names unless $name; | ||
| 335 | return 1 if grep {/$name/} @names; | ||
| 336 | return 0; | ||
| 337 | } | ||
| 338 | |||
| 339 | =head2 id | ||
| 340 | |||
| 341 | Arguments: $id | ||
| 342 | |||
| 343 | Return Value: $id | ||
| 344 | |||
| 345 | Contains the widget id. | ||
| 346 | |||
| 347 | =head2 legend | ||
| 348 | |||
| 349 | Arguments: $legend | ||
| 350 | |||
| 351 | Return Value: $legend | ||
| 352 | |||
| 353 | Contains the legend. | ||
| 354 | |||
| 355 | =head2 method | ||
| 356 | |||
| 357 | Arguments: $method | ||
| 358 | |||
| 359 | Return Value: $method | ||
| 360 | |||
| 361 | Contains the form method. | ||
| 362 | |||
| 363 | =head2 param | ||
| 364 | |||
| 365 | Arguments: $name | ||
| 366 | |||
| 367 | Return Value (scalar context): $value or \@values | ||
| 368 | |||
| 369 | Return Value (list context): @values | ||
| 370 | |||
| 371 | Returns valid parameters with a CGI.pm-compatible param method. (read-only) | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | sub param { | ||
| 376 | my $self = shift; | ||
| 377 | |||
| 378 | carp 'param method is readonly' if @_ > 1; | ||
| 379 | |||
| 380 | if ( @_ == 1 ) { | ||
| 381 | |||
| 382 | my $param = shift; | ||
| 383 | |||
| 384 | my $valid = $self->valid($param); | ||
| 385 | if ( !$valid || ( !exists $self->{_params}->{$param} ) ) { | ||
| 386 | return wantarray ? () : undef; | ||
| 387 | } | ||
| 388 | |||
| 389 | if ( ref $self->{_params}->{$param} eq 'ARRAY' ) { | ||
| 390 | return (wantarray) | ||
| 391 | ? @{ $self->{_params}->{$param} } | ||
| 392 | : $self->{_params}->{$param}->[0]; | ||
| 393 | } | ||
| 394 | else { | ||
| 395 | return (wantarray) | ||
| 396 | ? ( $self->{_params}->{$param} ) | ||
| 397 | : $self->{_params}->{$param}; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | return $self->valid; | ||
| 402 | } | ||
| 403 | |||
| 404 | =head2 params | ||
| 405 | |||
| 406 | =head2 parameters | ||
| 407 | |||
| 408 | Return Value: \%params | ||
| 409 | |||
| 410 | Returns validated params as hashref. | ||
| 411 | |||
| 412 | L</parameters> is an alias for L</params>. | ||
| 413 | |||
| 414 | =cut | ||
| 415 | |||
| 416 | sub params { | ||
| 417 | my $self = shift; | ||
| 418 | my @names = $self->valid; | ||
| 419 | my %params; | ||
| 420 | for my $name (@names) { | ||
| 421 | my @values = $self->param($name); | ||
| 422 | if ( @values > 1 ) { | ||
| 423 | $params{$name} = \@values; | ||
| 424 | } | ||
| 425 | else { | ||
| 426 | $params{$name} = $values[0]; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | return \%params; | ||
| 430 | } | ||
| 431 | |||
| 432 | =head2 subcontainer | ||
| 433 | |||
| 434 | Arguments: $tag | ||
| 435 | |||
| 436 | Return Value: $tag | ||
| 437 | |||
| 438 | Contains the subcontainer tag. | ||
| 439 | |||
| 440 | =head2 strict | ||
| 441 | |||
| 442 | Arguments: $bool | ||
| 443 | |||
| 444 | Return Value: $bool | ||
| 445 | |||
| 446 | Only consider parameters that pass at least one constraint valid. | ||
| 447 | |||
| 448 | =head2 submitted | ||
| 449 | |||
| 450 | =head2 is_submitted | ||
| 451 | |||
| 452 | Return Value: $bool | ||
| 453 | |||
| 454 | Returns true if C<< $widget->process >> received a C<$query> object. | ||
| 455 | |||
| 456 | L</is_submitted> is an alias for L</submitted>. | ||
| 457 | |||
| 458 | =head2 valid | ||
| 459 | |||
| 460 | Return Value: @names | ||
| 461 | |||
| 462 | Arguments: $name | ||
| 463 | |||
| 464 | Return Value: $bool | ||
| 465 | |||
| 466 | Returns a list of element names. Returns true/false if a name is given. | ||
| 467 | |||
| 468 | my @names = $form->valid; | ||
| 469 | my $valid = $form->valid($name); | ||
| 470 | |||
| 471 | =cut | ||
| 472 | |||
| 473 | sub valid { | ||
| 474 | my ( $self, $name ) = @_; | ||
| 475 | my @errors = $self->has_errors; | ||
| 476 | my @names; | ||
| 477 | if ( $self->strict ) { | ||
| 478 | for my $constraint ( @{ $self->{_constraints} } ) { | ||
| 479 | my $names = $constraint->names; | ||
| 480 | push @names, @$names if $names; | ||
| 481 | } | ||
| 482 | } | ||
| 483 | else { | ||
| 484 | @names = keys %{ $self->{_params} }; | ||
| 485 | } | ||
| 486 | my %valid; | ||
| 487 | CHECK: for my $name (@names) { | ||
| 488 | for my $error (@errors) { | ||
| 489 | next CHECK if $name eq $error; | ||
| 490 | } | ||
| 491 | $valid{$name}++; | ||
| 492 | } | ||
| 493 | my @valid = keys %valid; | ||
| 494 | return @valid unless $name; | ||
| 495 | return 1 if grep {/\Q$name/} @valid; | ||
| 496 | return 0; | ||
| 497 | } | ||
| 498 | |||
| 499 | =head2 add_valid | ||
| 500 | |||
| 501 | Arguments: $key, $value | ||
| 502 | |||
| 503 | Return Value: $value | ||
| 504 | |||
| 505 | Adds another valid value to the hash. | ||
| 506 | |||
| 507 | =cut | ||
| 508 | |||
| 509 | sub add_valid { | ||
| 510 | my ( $self, $key, $value ) = @_; | ||
| 511 | $self->{_params}->{$key} = $value; | ||
| 512 | return $value; | ||
| 513 | } | ||
| 514 | |||
| 515 | =head2 add_error | ||
| 516 | |||
| 517 | Arguments: \%attributes | ||
| 518 | |||
| 519 | Return Value: $error | ||
| 520 | |||
| 521 | $result->add_error({ name => 'foo' }); | ||
| 522 | |||
| 523 | This allows you to add custom error messages after the widget has processed | ||
| 524 | the input params. | ||
| 525 | |||
| 526 | Accepts 'name', 'type' and 'message' arguments. | ||
| 527 | The 'name' argument is required. The default value for 'type' is 'Custom'. | ||
| 528 | The default value for 'message' is 'Invalid Input'. | ||
| 529 | |||
| 530 | An example of use. | ||
| 531 | |||
| 532 | if ( ! $result->has_errors ) { | ||
| 533 | my $user = $result->valid('username'); | ||
| 534 | my $pass = $result->valid('password'); | ||
| 535 | |||
| 536 | if ( ! $app->login( $user, $pass ) ) { | ||
| 537 | $result->add_error({ | ||
| 538 | name => 'password', | ||
| 539 | message => 'Incorrect Password', | ||
| 540 | }); | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | In this example, the C<$result> initially contains no errors. If the login() | ||
| 545 | is unsuccessful though, add_error() is used to add an error to the password | ||
| 546 | Element. If the user is shown the form again using C<< $result->as_xml >>, | ||
| 547 | they will be shown an appropriate error message alongside the password | ||
| 548 | field. | ||
| 549 | |||
| 550 | =cut | ||
| 551 | |||
| 552 | sub add_error { | ||
| 553 | my ( $self, $args ) = @_; | ||
| 554 | |||
| 555 | croak "name argument required" unless defined $args->{name}; | ||
| 556 | |||
| 557 | $args->{type} = 'Custom' if not exists $args->{type}; | ||
| 558 | $args->{message} = 'Invalid Input' if not exists $args->{message}; | ||
| 559 | |||
| 560 | my $error = HTML::Widget::Error->new($args); | ||
| 561 | |||
| 562 | push @{ $self->{_errors}->{ $args->{name} } }, $error; | ||
| 563 | |||
| 564 | return $error; | ||
| 565 | } | ||
| 566 | |||
| 567 | =head1 AUTHOR | ||
| 568 | |||
| 569 | Sebastian Riedel, C<sri@oook.de> | ||
| 570 | |||
| 571 | =head1 LICENSE | ||
| 572 | |||
| 573 | This library is free software, you can redistribute it and/or modify it under | ||
| 574 | the same terms as Perl itself. | ||
| 575 | |||
| 576 | =cut | ||
| 577 | |||
| 578 | 1; |