| 1 | package QBit::WebInterface::Controller::Form::Field; | ||
| 2 | $QBit::WebInterface::Controller::Form::Field::VERSION = '0.033'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Class); | ||
| 6 | |||
| 7 | use Exception::Form; | ||
| 8 | |||
| 9 | __PACKAGE__->mk_accessors(qw(form name value)); | ||
| 10 | __PACKAGE__->abstract_methods(qw(control_html)); | ||
| 11 | |||
| 12 | sub init { | ||
| 13 | my ($self) = @_; | ||
| 14 | |||
| 15 | $self->SUPER::init(); | ||
| 16 | weaken($self->{'form'}); | ||
| 17 | |||
| 18 | ($self->{items_key}, $self->{items_label}) = %{$self->{items_key} || {'value' => 'label'}}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub is_hidden {0} | ||
| 22 | |||
| 23 | sub value_from_request { | ||
| 24 | my ($self, $request) = @_; | ||
| 25 | |||
| 26 | my $value = $request->param($self->{'name'}, $self->{'value'}); | ||
| 27 | $value = '' unless defined($value); | ||
| 28 | |||
| 29 | return $value; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub clean {$_[1]} | ||
| 33 | |||
| 34 | sub check {1} | ||
| 35 | |||
| 36 | sub trim { | ||
| 37 | for ($_[1]) { | ||
| 38 | s/^\s+//; | ||
| 39 | s/\s+$//; | ||
| 40 | } | ||
| 41 | |||
| 42 | return $_[1]; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub as_html { | ||
| 46 | my ($self) = @_; | ||
| 47 | return | ||
| 48 | '<div class="control-group' | ||
| 49 | . ($self->has_error() ? ' error' : '') | ||
| 50 | . ($self->{'required'} ? ' required' : '') . '">' | ||
| 51 | . $self->_html_label() | ||
| 52 | . '<div class="controls">' | ||
| 53 | . $self->control_html() | ||
| 54 | . '</div>' | ||
| 55 | . '</div>'; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub process { | ||
| 59 | my ($self, $controller) = @_; | ||
| 60 | |||
| 61 | $self->{'value'} = $self->value_from_request($controller->request); | ||
| 62 | $self->{'value'} = $self->trim($self->{'value'}) if $self->{'trim'}; | ||
| 63 | $self->{'value'} = $self->clean($self->{'value'}); | ||
| 64 | $self->{'value'} = $self->{'clean_func'}($self->{'value'}) | ||
| 65 | if exists($self->{'clean_func'}); | ||
| 66 | |||
| 67 | throw Exception::Form gettext('Required field') | ||
| 68 | if $self->{'required'} && $self->{'value'} !~ /\S/; | ||
| 69 | |||
| 70 | $self->check(); | ||
| 71 | $self->{'check'}($self, $self->{'value'}) | ||
| 72 | if exists($self->{'check'}); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub has_error { | ||
| 76 | my ($self) = @_; | ||
| 77 | |||
| 78 | return FALSE unless defined $self->{'name'}; | ||
| 79 | |||
| 80 | return exists($self->form->{'__FIELDS_ERROR__'}{$self->{'name'}}); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub error_text { | ||
| 84 | my ($self) = @_; | ||
| 85 | |||
| 86 | return html_encode($self->form->{'__FIELDS_ERROR__'}{$self->{'name'}}->message()); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _html_label { | ||
| 90 | my ($self, %opts) = @_; | ||
| 91 | |||
| 92 | return '' unless defined($self->{'label'}) && length($self->{'label'}); | ||
| 93 | |||
| 94 | return '<label class="control-label"' | ||
| 95 | . ( | ||
| 96 | defined($self->{'id'}) ? ' for="' . html_encode($self->{'id'}) . '"' | ||
| 97 | : '' | ||
| 98 | ) | ||
| 99 | . '>' | ||
| 100 | . html_encode( | ||
| 101 | ref($self->{'label'}) eq 'CODE' ? $self->{'label'}() | ||
| 102 | : $self->{'label'} | ||
| 103 | ) . '</label>'; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _class { | ||
| 107 | my ($self, $req_class) = @_; | ||
| 108 | |||
| 109 | $req_class = [] unless defined($req_class); | ||
| 110 | $req_class = [$req_class] if ref($req_class) ne 'ARRAY'; | ||
| 111 | |||
| 112 | my $class = $self->{'class'}; | ||
| 113 | $class = [] unless defined($class); | ||
| 114 | $class = [$class] if ref($class) ne 'ARRAY'; | ||
| 115 | |||
| 116 | return @$req_class + @$class ? ' class="' . join(' ', map {html_encode($_)} @$req_class, @$class) . '"' : ''; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _html_error { | ||
| 120 | my ($self) = @_; | ||
| 121 | |||
| 122 | return $self->has_error() ? '<span class="help-inline">' . $self->error_text() . '</span>' : ''; | ||
| 123 | } | ||
| 124 | |||
| 125 | TRUE; |