NAME
Form::Processor::Field - Base class for Fields used with Form::Processor
SYNOPSIS
# Used from another class
use base 'Form::Processor::Field::Text';
my $field = Form::Processor::Field::Text->new( name => $name );
DESCRIPTION
This is a base class that allows basic functionallity for form fields.
METHODS
- new
-
Creates new field. Will accept
name, value, required (and errors)
as parameters. Only 'name' is required.
- name
-
Sets or returns the name of the field.
- id
-
Returns an id for the field, which is
$field->form->name . $field->id
- init_widget
-
This is the generic type of widget that could be used to generate, say, the HTML markup for the field. It's similar to the field's type(), but less specific since fields of different types often use the same widget type.
For example, a Text field would have both the type and widget values of "Text", where an Integer field would have "Integer" for the type value and "Text" as the widget value.
Normally you do not need to set this in a field class as it should pick it up from the base field class used for the specific field.
The basic types are:
Type : Example fields ------------:----------------------------------- Text : Text, Integer, Single field dates Checkbox : Checkbox Radio : Boolean (yes,no), OneToTen Select : Select, Multiple Textarea : HtmlArea Compound : A field made up of other fields
Note that a Select could be a drop down list or a radio group, and that might be determined in the template code based on how many select options there are.
Multiple select fields, likewise, might be an option list or a group of checkboxes.
- value
-
Sets or returns the internal value of the field.
The "validate" field method must set this value if the field validates.
- required
-
Sets or returns the required flag on the field
- errors
-
returns the error (or list of errors if more than one was set)
- add_error
-
Add an error to the list of errors
- reest_errors
-
Resets the list of errors. The validate method clears the errors by default.
- validate_field
-
This method does standard validation, which currently tests:
required -- if field is required and value exists
Then if a value exists:
test_multiple -- looks for multiple params passed in when not allowed test_options -- tests if the params passed in are valid options
If all of those pass then the field's validate method is called
$self->validate;
The field's error list and internal value are reset upon entry.
- validate
-
This method is normally overridden in a field's class. It does more specific test like checking that that the field is of the correct format.
The default is to copy the input data to the internal value.
- trim_value
-
Trims leading and trailing white space for single parameters. If the parameter is an array ref then each value is trimmed.
Pass in the value to trim and returns value back
- required_message
-
Returns text for use in "required" message.
- test_multiple
-
Returns false if $self->input is a reference (assuming it's an array ref). Subclasses that allow multiple values should override.
Returns true or false;
- any_input
-
Returns true if $self->input contains any non-blank input.
- test_options
-
If the field has an "options" method then the input value (or values if an array ref) is tested to make sure they all are valid options.
Returns true or false
- format_value
-
This method takes $field->value and formats it into a hash that is merged in to the final params hash. It's purpose is to take the internal value an create the key/value pairs.
By default it returns:
( $field->name, $field->value )
A Date field subclass might expaned the value into:
my $name = $field->name; return ( $name . 'd' => $day, $name . 'm' => $month, $name . 'y' => $year, );
It's up to you to not use duplicate hash values.
You might want to override test_required() if you don't use a matching field name (e.g. $name . 'd' instead of just $name).
- noupdate
-
This boolean flag indicates a field that should not be updated. Field's flagged as noupdate are skipped when processing by the model.
This is usesful when a form contains extra fields that are not directly written to the data store.
- writeonly
-
Fields flagged as writeonly are not fetched from the model when $form->params is called. This means the field's formatted value will not be included in the hash returned by $form->fif when first populating a form with existing values.
An example might be a situation where a trigger is used to create a copy of a row before an update. In this case you might have a required "update_reason" column that should only be written to the database on updates.
Unlike the
password
flag, this only prevents populating a field from the field's initial value, but not from the parameter hash passed to the form. Redrawn forms (after validation failures) will display the value submitted in the form. - password
-
This is a boolean flag and if set the $form->params method will remove that field when calling $form->fif.
This is different than the
writeonly
method above in that the value is removed from the hash every time its fetched. - value_changed
-
Returns true if the value in the item has changed from what is currently in the field's value.
This only does a string compare (arrays are sorted and joined).
- required_text
-
Returns "required" or "optional" based on the field's setting.
- has_error
-
Returns the count of errors on the field.
- dump_field
-
A little debugging.
AUTHOR
Bill Moseley - with *much* help from John Siracusa. Most of this is based on Rose-HTML-Form. It's basically a very trimmed down version without all the HTML generation and the ability to do compound fields.