| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Spark::Form::Field; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.2102'; | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Superclass for all Form Fields | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 25 |  |  | 25 |  | 137223 | use Moose; | 
|  | 25 |  |  |  |  | 42 |  | 
|  | 25 |  |  |  |  | 149 |  | 
| 8 | 25 |  |  | 25 |  | 115328 | use MooseX::AttributeHelpers; | 
|  | 25 |  |  |  |  | 37 |  | 
|  | 25 |  |  |  |  | 522 |  | 
| 9 | 25 |  |  | 25 |  | 11950 | use MooseX::LazyRequire; | 
|  | 25 |  |  |  |  | 188402 |  | 
|  | 25 |  |  |  |  | 95 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | with qw(MooseX::Clone); | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | has name => ( | 
| 14 |  |  |  |  |  |  | isa      => 'Str', | 
| 15 |  |  |  |  |  |  | is       => 'ro', | 
| 16 |  |  |  |  |  |  | required => 1, | 
| 17 |  |  |  |  |  |  | ); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has form => ( | 
| 20 |  |  |  |  |  |  | isa      => 'Spark::Form', | 
| 21 |  |  |  |  |  |  | is       => 'rw', | 
| 22 |  |  |  |  |  |  | lazy_required => 1, | 
| 23 |  |  |  |  |  |  | weak_ref => 1, #De-circular-ref | 
| 24 |  |  |  |  |  |  | traits => [qw(NoClone)], #Argh, what will it be set to? | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has value => ( | 
| 28 |  |  |  |  |  |  | is       => 'rw', | 
| 29 |  |  |  |  |  |  | required => 0, | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | has valid => ( | 
| 33 |  |  |  |  |  |  | isa      => 'Bool', | 
| 34 |  |  |  |  |  |  | is       => 'rw', | 
| 35 |  |  |  |  |  |  | required => 0, | 
| 36 |  |  |  |  |  |  | default  => 0, | 
| 37 |  |  |  |  |  |  | ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | has _errors => ( | 
| 40 |  |  |  |  |  |  | metaclass => 'Collection::Array', | 
| 41 |  |  |  |  |  |  | isa       => 'ArrayRef[Str]', | 
| 42 |  |  |  |  |  |  | is        => 'ro', | 
| 43 |  |  |  |  |  |  | required  => 0, | 
| 44 |  |  |  |  |  |  | default   => sub { [] }, | 
| 45 |  |  |  |  |  |  | provides  => { | 
| 46 |  |  |  |  |  |  | push     => '_add_error', | 
| 47 |  |  |  |  |  |  | elements => 'errors', | 
| 48 |  |  |  |  |  |  | clear    => '_clear_errors', | 
| 49 |  |  |  |  |  |  | }, | 
| 50 |  |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub error { | 
| 53 | 41 |  |  | 41 | 1 | 210 | my ($self, $error) = @_; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 41 |  |  |  |  | 937 | $self->valid(0); | 
| 56 | 41 |  |  |  |  | 131 | $self->_add_error($error); | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 41 |  |  |  |  | 73 | return $self; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub human_name { | 
| 62 | 15 |  |  | 15 | 1 | 29 | my ($self) = @_; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 15 | 100 |  |  |  | 101 | if ($self->can('label')) { | 
| 65 | 6 | 50 |  |  |  | 151 | return $self->label if $self->label; | 
| 66 |  |  |  |  |  |  | } | 
| 67 | 15 | 50 |  |  |  | 55 | if ($self->can('name')) { | 
| 68 | 15 | 50 |  |  |  | 344 | return $self->name if $self->name; | 
| 69 |  |  |  |  |  |  | } | 
| 70 | 0 |  |  |  |  | 0 | return q(); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub validate { | 
| 74 | 64 |  |  | 64 | 1 | 66 | my ($self) = @_; | 
| 75 | 64 |  |  |  |  | 160 | $self->_clear_errors; | 
| 76 | 64 |  |  |  |  | 1393 | $self->valid(1); | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | #Set a default of the empty string, suppresses a warning | 
| 79 | 64 |  | 100 |  |  | 1509 | $self->value($self->value || q()); | 
| 80 | 64 |  |  |  |  | 170 | return $self->_validate; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 20 |  |  | 20 |  | 334 | sub _validate { return 1 } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 86 |  |  |  |  |  |  | 1; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =pod | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 NAME | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | Spark::Form::Field - Superclass for all Form Fields | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 VERSION | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | version 0.2102 | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Field superclass. Must subclass this to be considered a field. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | package My::Field; | 
| 107 |  |  |  |  |  |  | use Moose; | 
| 108 |  |  |  |  |  |  | require Spark::Form::Field; | 
| 109 |  |  |  |  |  |  | extends 'Spark::Form::Field'; | 
| 110 |  |  |  |  |  |  | with 'Spark::Form::Field::Role::Validateable'; | 
| 111 |  |  |  |  |  |  | with 'Spark::Form::Field::Role::Printable::XHTML'; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub _validate { | 
| 114 |  |  |  |  |  |  | my $self = shift; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | #validate existence of data | 
| 117 |  |  |  |  |  |  | if ($self->value) { | 
| 118 |  |  |  |  |  |  | #If we're valid, we should say so | 
| 119 |  |  |  |  |  |  | $self->valid(1); | 
| 120 |  |  |  |  |  |  | } else { | 
| 121 |  |  |  |  |  |  | #error will call $self->valid(0) and also set an error. | 
| 122 |  |  |  |  |  |  | $self->error('no value') | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | #And we should return boolean validity | 
| 126 |  |  |  |  |  |  | $self->valid | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | sub to_xhtml { | 
| 130 |  |  |  |  |  |  | #Rather poorly construct an XHTML tag | 
| 131 |  |  |  |  |  |  | '<input type="checkbox" value="' . shift-value . '">'; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Note that you might want to look into HTML::Tiny. | 
| 135 |  |  |  |  |  |  | Or better still, L<SparkX::Form::Field::Plugin::StarML>. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | There are a bunch of pre-built fields you can actually use in | 
| 138 |  |  |  |  |  |  | L<SparkX::Form::BasicFields>. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head1 ACCESSORS | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head2 name => Str | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | Name of the field in the data source. Will be slurped on demand. | 
| 145 |  |  |  |  |  |  | Required at validation time, not at construction time. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =head2 form => Spark::Form | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | Reference to the form it is a member of. | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head2 value => Any | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | Value in the field. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head2 valid => Bool | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | Treat as read-only. Whether the field is valid. | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =head2 errors => ArrayRef | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | Treat as read-only. The list of errors generated in validation. | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =head1 METHODS | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head2 human_name | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | Returns the label if present, else the field name. | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =head2 validate | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Returns true always. Subclass and fill in C<_validate> to do proper validation. See the synopsis. | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =head2 error (Str) | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | Adds an error to the current field's list. | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | =over 4 | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =item L<Spark::Form::Field::Role::Printable> - Fields that can be printed | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =item L<SparkX::Form::BasicValidators> - Set of validators to use creating fields | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =item L<SparkX::Form::BasicFields> - Ready to use fields | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =back | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =head1 AUTHOR | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | James Laver L<http://jameslaver.com> | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | This software is copyright (c) 2009 by James Laver C<< <sprintf qw(%s@%s.%s cpan jameslaver com)> >>. | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 200 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =cut | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | __END__ | 
| 207 |  |  |  |  |  |  |  |