The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

VERSION

version 0.2102

DESCRIPTION

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

SYNOPSIS

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.

ACCESSORS

name => Str

Name of the field in the data source. Will be slurped on demand. Required at validation time, not at construction time.

form => Spark::Form

Reference to the form it is a member of.

value => Any

Value in the field.

valid => Bool

Treat as read-only. Whether the field is valid.

errors => ArrayRef

Treat as read-only. The list of errors generated in validation.

METHODS

human_name

Returns the label if present, else the field name.

validate

Returns true always. Subclass and fill in _validate to do proper validation. See the synopsis.

error (Str)

Adds an error to the current field's list.

SEE ALSO

Spark::Form::Field::Role::Printable - Fields that can be printed
SparkX::Form::BasicValidators - Set of validators to use creating fields
SparkX::Form::BasicFields - Ready to use fields

AUTHOR

James Laver L<http://jameslaver.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2009 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.