| 1 | package HTML::Shakan::Field; | ||||||||||
| 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 | |||||||||||
| 6 | has id => ( | ||||||||||
| 7 | is => 'rw', | ||||||||||
| 8 | isa => 'Str', | ||||||||||
| 9 | trigger => sub { | ||||||||||
| 10 | 27 | ▶ | my ($self, $id) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 27 | $self->{attr}->{id} = $id; | |||||||||
| 12 | }, | ||||||||||
| 13 | 22 | ); | |||||||||
| 14 | |||||||||||
| 15 | has name => ( | ||||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => 'Str', | ||||||||||
| 18 | required => 1, | ||||||||||
| 19 | trigger => sub { | ||||||||||
| 20 | 72 | ▶ | my ( $self, $name ) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 72 | $self->{attr}->{name} = $name; | |||||||||
| 22 | }, | ||||||||||
| 23 | 22 | ); | |||||||||
| 24 | |||||||||||
| 25 | has value => ( | ||||||||||
| 26 | is => 'rw', | ||||||||||
| 27 | isa => 'Str', | ||||||||||
| 28 | required => 0, | ||||||||||
| 29 | trigger => sub { | ||||||||||
| 30 | 4 | ▶ | my ( $self, $value ) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 4 | $self->{attr}->{value} = $value; | |||||||||
| 32 | }, | ||||||||||
| 33 | 22 | ); | |||||||||
| 34 | |||||||||||
| 35 | has filters => ( | ||||||||||
| 36 | is => 'rw', | ||||||||||
| 37 | isa => 'ArrayRef[Str]', | ||||||||||
| 38 | 70 | ▶ | default => sub { +[] } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 22 | ); | |||||||||
| 40 | |||||||||||
| 41 | 22 | has widget => ( | |||||||||
| 42 | is => 'ro', | ||||||||||
| 43 | isa => 'Str', | ||||||||||
| 44 | required => 1, | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | has attr => ( | ||||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | isa => 'HashRef', | ||||||||||
| 50 | lazy => 1, | ||||||||||
| 51 | default => sub { | ||||||||||
| 52 | +{} | ||||||||||
| 53 | 0 | ▶ | }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 54 | 22 | ); | |||||||||
| 55 | |||||||||||
| 56 | has label => ( | ||||||||||
| 57 | is => 'ro', | ||||||||||
| 58 | isa => 'Str', | ||||||||||
| 59 | lazy => 1, | ||||||||||
| 60 | 29 | ▶ | default => sub { shift->name } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 22 | ); | |||||||||
| 62 | |||||||||||
| 63 | 22 | has required => ( | |||||||||
| 64 | is => 'ro', | ||||||||||
| 65 | isa => 'Bool', | ||||||||||
| 66 | default => 0, | ||||||||||
| 67 | ); | ||||||||||
| 68 | |||||||||||
| 69 | 22 | has inflator => ( | |||||||||
| 70 | is => 'ro', | ||||||||||
| 71 | isa => 'Object', | ||||||||||
| 72 | ); | ||||||||||
| 73 | |||||||||||
| 74 | 22 | has custom_validation => ( | |||||||||
| 75 | is => 'ro', | ||||||||||
| 76 | isa => 'CodeRef', | ||||||||||
| 77 | ); | ||||||||||
| 78 | |||||||||||
| 79 | has constraints => ( | ||||||||||
| 80 | is => 'ro', | ||||||||||
| 81 | isa => 'ArrayRef', | ||||||||||
| 82 | 72 | ▶ | default => sub { +[] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 22 | ); | |||||||||
| 84 | |||||||||||
| 85 | sub add_constraint { | ||||||||||
| 86 | 15 | ▶ | my ($self, @constraint) = @_; | ||||||||
|
Subroutine
add_constraint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 87 | 15 | push @{$self->{constraints}}, @constraint; | |||||||||
| 88 | 15 | $self; # method chain | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | has complex_constraints => ( | ||||||||||
| 92 | is => 'ro', | ||||||||||
| 93 | isa => 'ArrayRef', | ||||||||||
| 94 | 72 | ▶ | default => sub { +[] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 22 | ); | |||||||||
| 96 | |||||||||||
| 97 | sub add_complex_constraint { | ||||||||||
| 98 | 2 | ▶ | my ($self, @constraints) = @_; | ||||||||
|
Subroutine
add_complex_constraint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 99 | 2 | push @{$self->{complex_constraints}}, @constraints; | |||||||||
| 100 | 2 | $self; # method chain | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | sub get_constraints { | ||||||||||
| 104 | 62 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_constraints: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 105 | |||||||||||
| 106 | 62 | my @rule = @{$self->{constraints}}; | |||||||||
| 107 | 62 | ▶ | if ($self->required) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 15 | push @rule, 'NOT_NULL'; | |||||||||
| 109 | } | ||||||||||
| 110 | return ( | ||||||||||
| 111 | $self->name => \@rule, | ||||||||||
| 112 | 62 | @{ $self->complex_constraints }, | |||||||||
| 113 | ); | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 22 | ▶ | no Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 117 | 22 | __PACKAGE__->meta->make_immutable; | |||||||||
| 118 | __END__ | ||||||||||
| 119 | |||||||||||
| 120 | =encoding utf8 | ||||||||||
| 121 | |||||||||||
| 122 | =for stopwords attr | ||||||||||
| 123 | |||||||||||
| 124 | =head1 NAME | ||||||||||
| 125 | |||||||||||
| 126 | HTML::Shakan::Field - base class for field object | ||||||||||
| 127 | |||||||||||
| 128 | =head1 DESCRIPTION | ||||||||||
| 129 | |||||||||||
| 130 | This is a base class for filed object. | ||||||||||
| 131 | |||||||||||
| 132 | =head1 ATTRIBUTES | ||||||||||
| 133 | |||||||||||
| 134 | =over 4 | ||||||||||
| 135 | |||||||||||
| 136 | =item id | ||||||||||
| 137 | |||||||||||
| 138 | the 'id' attribute for the HTML elements. | ||||||||||
| 139 | |||||||||||
| 140 | =item name | ||||||||||
| 141 | |||||||||||
| 142 | the 'name' attribute for the HTML elements. | ||||||||||
| 143 | |||||||||||
| 144 | =item value | ||||||||||
| 145 | |||||||||||
| 146 | the 'value' attribute for the HTML elements. | ||||||||||
| 147 | |||||||||||
| 148 | =item filters: ArrayRef[Str] | ||||||||||
| 149 | |||||||||||
| 150 | This is parameter filters in arrayref. | ||||||||||
| 151 | |||||||||||
| 152 | For example, following field removes white space from parameter value in head and end. | ||||||||||
| 153 | |||||||||||
| 154 | TextField( | ||||||||||
| 155 | name => 'f', | ||||||||||
| 156 | required => 1, | ||||||||||
| 157 | filters => [qw'WhiteSpace'] | ||||||||||
| 158 | ), | ||||||||||
| 159 | |||||||||||
| 160 | =item widget | ||||||||||
| 161 | |||||||||||
| 162 | type of widget. | ||||||||||
| 163 | |||||||||||
| 164 | =item attr | ||||||||||
| 165 | |||||||||||
| 166 | hashref about the miscellaneous attributes. | ||||||||||
| 167 | |||||||||||
| 168 | =item label | ||||||||||
| 169 | |||||||||||
| 170 | label for this field. | ||||||||||
| 171 | |||||||||||
| 172 | =item required | ||||||||||
| 173 | |||||||||||
| 174 | is this field's value required? | ||||||||||
| 175 | |||||||||||
| 176 | =item custom_validation | ||||||||||
| 177 | |||||||||||
| 178 | TextField( | ||||||||||
| 179 | name => 'id', | ||||||||||
| 180 | custom_validation => sub { | ||||||||||
| 181 | my ($form, $field) = @_; | ||||||||||
| 182 | if (is_reserved_id($form->param($field->name))) { | ||||||||||
| 183 | $form->set_error($field->name() => 'reserved'); | ||||||||||
| 184 | } | ||||||||||
| 185 | } | ||||||||||
| 186 | ) | ||||||||||
| 187 | |||||||||||
| 188 | You can register custom validation callback. | ||||||||||
| 189 | |||||||||||
| 190 | The callback function takes two arguments. | ||||||||||
| 191 | |||||||||||
| 192 | =over 4 | ||||||||||
| 193 | |||||||||||
| 194 | =item $form | ||||||||||
| 195 | |||||||||||
| 196 | This is a instance of L<HTML::Shakan>. You can take query parameter value by this object. | ||||||||||
| 197 | |||||||||||
| 198 | =item $field | ||||||||||
| 199 | |||||||||||
| 200 | The field object itself. | ||||||||||
| 201 | |||||||||||
| 202 | =back | ||||||||||
| 203 | |||||||||||
| 204 | =item constraints | ||||||||||
| 205 | |||||||||||
| 206 | constraints for FormValidator::Lite. | ||||||||||
| 207 | |||||||||||
| 208 | =back | ||||||||||
| 209 | |||||||||||
| 210 | =head1 AUTHORS | ||||||||||
| 211 | |||||||||||
| 212 | tokuhirom | ||||||||||
| 213 | |||||||||||