
Spark::Form::Field - Superclass for all Form Fields

version 0.2103

package My::Field;
use Moose;
require Spark::Form::Field;
extends 'Spark::Form::Field';
with 'Spark::Form::Field::Role::Validateable';
with 'Spark::Form::Field::Role::Printable::XHTML';
sub _validate {
my $self = shift;
#validate existence of data
if ($self->value) {
#If we're valid, we should say so
$self->valid(1);
} else {
#error will call $self->valid(0) and also set an error.
$self->error('no value')
}
#And we should return boolean validity
$self->valid
}
sub to_xhtml {
#Rather poorly construct an XHTML tag
'<input type="checkbox" value="' . shift-value . '">';
}
Note that you might want to look into HTML::Tiny. Or better still, SparkX::Form::Field::Plugin::StarML.
There are a bunch of pre-built fields you can actually use in SparkX::Form::BasicFields.

Field superclass. Must subclass this to be considered a field.

Name of the field in the data source. Will be slurped on demand. Required at validation time, not at construction time.
Reference to the form it is a member of.
Value in the field.
Treat as read-only. Whether the field is valid.
Treat as read-only. The list of errors generated in validation.

Returns the label if present, else the field name.
Returns true always. Subclass and fill in _validate to do proper validation. See the synopsis.


James Laver http://jameslaver.com

This software is copyright (c) 2011 by James Laver <sprintf qw(%s@%s.%s cpan jameslaver com)>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.