The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Form::Tiny::FieldDefinition - definition of a field to be validated

SYNOPSIS

        # you usually don't have to do this by hand, see examples in Form::Tiny::Manual
        # name is the only required attribute
        my $definition = Form::Tiny::FieldDefinition->new(
                name => 'something',
                type => Str,
                ...
        );

DESCRIPTION

This class keeps all the data for a field definition and contains method that handle single field validation.

ATTRIBUTES

Each of the attributes can be accessed by calling its name as a function on Form::Tiny::FieldDefinition object. See Form::Tiny::Manual for more examples.

name

The only required attribute for the constructor.

A string which should specify the hash structure path of the field.

Special characters are:

  • dot [.], which specifies nesting

  • star [*], which specifies any number of array elements, but only if it is the only character on level, like a.*.b

They both can be escaped by a backslash \ to lose their special meaning.

required

A field is not required by default (value 0), which means that its absence does not produce an error.

A field can also be soft required ('soft') or hard required ('hard' or 1).

Soft required field produce errors only if it is undefined or not present in the input data.

Hard required field also checks if the field is not an empty string.

type

The type attribute is where you can plug in a Type::Tiny type object. It has to be an instance of a class that provider validate and check methods, just like Type::Tiny. This can also be a Form::Tiny form instance.

predicate: has_type

addons

Hash reference for internal use only - readable and writable under the addons method. If you need additional data for a field definition that will be used in metaclasses (while extending Form::Tiny), put it here.

coerce

Coercions take place just before the validation. By default, values are not coerced. Specifying value 1 will cause the field to use coercions from the type object.

It can also be a code reference which will be called to coerce the value, passing in a field value as its only argument.

adjust

Adjustments take place just after the validation. By default, values are not adjusted. You can specify a code reference which will be called to adjust the value (change the value after the validation).

predicate: is_adjusted

writer: set_adjustment

default

A coderef returning the default value for the field. Will be used when the field is not present in the input at all. Making the field hard-required will make the default value be used in place of undefined / empty value as well.

This coderef will be passed form instance as the only argument and is expected to return a scalar value.

predicate: has_default

writer: set_default

message

If type class error messages are not helpful enough, you can specify your own message string which will be inserted into form errors if the validation for the field fails.

predicate: has_message

data

Custom data for the field. Can be anything and will not be used by Form::Tiny system itself. It should be anything that will help user's own system use the form instance.

writer: set_data

predicate: has_data

METHODS

is_subform

Checks if the field definition's type is a form - whether it mixes in Form::Tiny::Form role.

get_name_path

Parses and returns the name of the field as an object of Form::Tiny::Path class.

hard_required

Checks if the field is hard-required (any of the two values which are allowed for this flag)

validate

Validates a scalar value. Arguments are $parent_form, $field_value. Returns a boolean, whether the validation passed.