NAME
Form::Tiny::Form - main role of the Form::Tiny system
SYNOPSIS
DESCRIPTION
This role gets automatically mixed in by importing Form::Tiny into your namespace.
ADDED INTERFACE
This section describes the interface added to your class after mixing in the Form::Tiny role.
ATTRIBUTES
Each of the attributes can be accessed by calling its name as a function on Form::Tiny object.
input
Contains the input data passed to the form.
writer: set_input
fields
Contains the validated and cleaned fields set after the validation is complete. Cannot be specified in the constructor.
field_defs
Contains an array reference of Form::Tiny::FieldDefinition instances fetched from the metaclass with context of current instance. Rebuilds everytime new input data is set.
errors
Contains an array reference of form errors which were detected by the last performed validation. Each error is an instance of Form::Tiny::Error.
predicate: has_errors
METHODS
This section describes standalone methods available in the module - they are not directly connected to any of the attributes.
new
This is a Moose-flavored constructor for the class. It accepts a hash or hash reference of parameters, which are the attributes specified above.
valid
Performs the validation and returns a boolean which indicates whether the form is valid or not.
Accepts no arguments. Input must be set before calling this method.
check
validate
These methods are here to ensure that a Form::Tiny instance can be used as a type validator itself by other form classes.
check returns a boolean value that indicates whether the validation of input data was successful.
validate does the same thing, but instead of returning a boolean it returns a list of errors that were detected, or undef if none.
Both methods take input data as the only argument.
value
if ($form->valid) {
my $value = $form->value('field_name');
my $values_array_ref = $form->value('array.*');
}
Can be used to interact with "fields" hashref indirectly with key safety checks.
After validation, this method returns form value of the given field. If the form was not yet validated or the validation was unsuccessful, returns undef
.
Accepts a single argument: a field name. Will raise an exception if such field was not defined.
If the field contains an array (*
) will return an array reference of all values for that field. Order of those values will be the same as in the input array(s).
Note: currently, the field name passed must be a full field name (not a part of field name). For example, passing hash.hash
when the field is specified as hash.hash.field
will raise an exception. This may change in the future.
errors_hash
Helper method which returns errors much like the errors
form attribute, but in a hash reference with form field names as keys. Errors not assigned to any specific field end up in empty string key. The values are array references of error messages (strings).
Each field will only be present in the hash if it has an error assigned to it. If no errors are present, the hash will be empty.
It allows you to get errors in format which is easier to navigate:
{
'' => [
# global form errors
],
# specific field errors
'field1' => [
'something went wrong'
],
}
add_error
$form->add_error($error_string);
$form->add_error($field_name => $error_string);
$form->add_error($error_object);
Adds an error to the form. If $error_object
style is used, it must be an instance of Form::Tiny::Error.