| 1 | package HTML::AutoForm::Field::AnyText; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use utf8; |
|---|
| 6 | |
|---|
| 7 | our @ISA; |
|---|
| 8 | our %Defaults; |
|---|
| 9 | |
|---|
| 10 | BEGIN { |
|---|
| 11 | @ISA = qw(HTML::AutoForm::Field); |
|---|
| 12 | %Defaults = ( |
|---|
| 13 | min_length => undef, |
|---|
| 14 | max_length => undef, |
|---|
| 15 | regexp => undef, |
|---|
| 16 | ); |
|---|
| 17 | Class::Accessor::Lite->mk_accessors(keys %Defaults); |
|---|
| 18 | }; |
|---|
| 19 | |
|---|
| 20 | sub new { |
|---|
| 21 | my $klass = shift; |
|---|
| 22 | my $self = $klass->SUPER::new( |
|---|
| 23 | %Defaults, |
|---|
| 24 | @_ == 1 ? %{$_[0]} : @_, |
|---|
| 25 | ); |
|---|
| 26 | if (my $r = $self->regexp) { |
|---|
| 27 | # special mappings |
|---|
| 28 | if (! ref($r) && $r eq 'email') { |
|---|
| 29 | # from http://www.tt.rim.or.jp/~canada/comp/cgi/tech/mailaddrmatch/ |
|---|
| 30 | $self->regexp(qr/^[\x01-\x7F]+@(([-a-z0-9]+\.)*[a-z]+|\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])/oi); |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | $self; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub render { |
|---|
| 37 | my ($self, $values) = @_; |
|---|
| 38 | return HTML::AutoForm::_build_element( |
|---|
| 39 | 'input', |
|---|
| 40 | { |
|---|
| 41 | class => 'autoform_field_' . $self->type, |
|---|
| 42 | %$self, |
|---|
| 43 | }, |
|---|
| 44 | { |
|---|
| 45 | type => $self->type, |
|---|
| 46 | $values && @$values ? (value => $values->[0]) : (), |
|---|
| 47 | }, |
|---|
| 48 | \%Defaults, |
|---|
| 49 | ); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | sub validate { |
|---|
| 53 | my ($self, $values) = @_; |
|---|
| 54 | |
|---|
| 55 | return HTML::AutoForm::Error->new( |
|---|
| 56 | message => '不正な入力値です', |
|---|
| 57 | field => $self, |
|---|
| 58 | ) if @$values >= 2; |
|---|
| 59 | if (@$values == 0 || $values->[0] eq '') { |
|---|
| 60 | # is empty |
|---|
| 61 | return unless $self->required; |
|---|
| 62 | return HTML::AutoForm::Error->new( |
|---|
| 63 | message => $self->label . 'を入力してください', |
|---|
| 64 | field => $self, |
|---|
| 65 | ); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | my $value = $values->[0]; |
|---|
| 69 | if (my $l = $self->min_length) { |
|---|
| 70 | return HTML::AutoForm::Error->new( |
|---|
| 71 | message => $self->label . 'が短すぎます', |
|---|
| 72 | field => $self, |
|---|
| 73 | ) if length($value) < $l; |
|---|
| 74 | } |
|---|
| 75 | if (my $l = $self->max_length) { |
|---|
| 76 | return HTML::AutoForm::Error->new( |
|---|
| 77 | message => $self->label . 'が長すぎます', |
|---|
| 78 | field => $self, |
|---|
| 79 | ) if $l < length $value; |
|---|
| 80 | } |
|---|
| 81 | if (my $r = $self->regexp) { |
|---|
| 82 | return HTML::AutoForm::Error->new( |
|---|
| 83 | message => '無効な' . $self->label . 'です', |
|---|
| 84 | field => $self, |
|---|
| 85 | ) if $value !~ /$r/; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | return; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | 1; |
|---|