| 1 | package HTML::Widget::Constraint::Regex; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'HTML::Widget::Constraint'; | ||
| 6 | |||
| 7 | __PACKAGE__->mk_accessors(qw/regex/); | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | HTML::Widget::Constraint::Regex - Regex Constraint | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | my $c = $widget->constraint( 'Regex', 'foo' ); | ||
| 16 | $c->regex(qr/^\w+$/); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Regex Constraint. | ||
| 21 | |||
| 22 | The Regex constraint always allows an C<undef> value or an empty string. | ||
| 23 | If you don't want this behaviour, also use the | ||
| 24 | L<HTML::Widget::Constraint::All|All constraint>. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | =head2 regex | ||
| 29 | |||
| 30 | Arguments: qr/ regex / | ||
| 31 | |||
| 32 | =head2 validate | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub validate { | ||
| 37 | my ( $self, $value ) = @_; | ||
| 38 | my $regex = $self->regex || qr/.*/; | ||
| 39 | return 0 if ( defined $value | ||
| 40 | && $value ne '' | ||
| 41 | && $value !~ $regex ); | ||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | =head1 AUTHOR | ||
| 46 | |||
| 47 | Sebastian Riedel, C<sri@oook.de> | ||
| 48 | |||
| 49 | =head1 LICENSE | ||
| 50 | |||
| 51 | This library is free software, you can redistribute it and/or modify it under | ||
| 52 | the same terms as Perl itself. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | 1; |