Form-Processor

 view release on metacpan or  search on metacpan

lib/Form/Processor/Field.pm  view on Meta::CPAN

        # These should probably be 'get_set' here and then 'get_set_init' any
        # place that needs to define an initial value.
        password  => { interface => 'get_set_init' },    # don't return field in $form->fif
        required  => { interface => 'get_set_init' },    # field is requried
        writeonly => { interface => 'get_set_init' },    # don't call format_value on this field
        clear     => { interface => 'get_set_init' },    # don't validate and remove from database

        # disabled and readonly mirror the html form specification
        # disabled fields are not suppose to be "successful" and thus
        # should not be updated.  But.. see "noupdate" below.
        disabled => { interface => 'get_set_init' } .    # Don't update this field in the database.

            # readonly fields are basically like hidden fields that the UI
            # should no be able to modify but still are submitted.
            readonly => { interface => 'get_set_init' },    # Flag to indicate readonly field

        # Since disabled and readonly effect the UI differently
        # use a separate flag to tell the model to not update a field.
        noupdate => { interface => 'get_set_init' },        # don't update this field in the database

        must_submit => { interface => 'get_set' }           # override use_existing_values
    ],

    array => [
        errors        => {},
        reset_errors  => { interface => 'reset', hash_key => 'errors' },
        add_error_str => { interface => 'push', hash_key => 'errors' },
    ],
);


## Should $value be overridden to only return a value if there are not
#  any errors?

# ABSTRACT: Base class for Fields used with Form::Processor



sub init {
    my $self = shift;

    $self->SUPER::init( @_ );

    die "Need to supply name parameter"
        unless $self->name;
}


sub full_name {
    my $field = shift;

    my $name   = $field->name;
    my $form   = $field->form || return $name;
    my $parent = $form->parent_field || return $name;
    return $parent->name . '.' . $name;
}


sub form {
    my $self = shift;
    return Scalar::Util::weaken( $self->{form} = shift ) if ( @_ );
    return $self->{form};
}


sub init_id {
    my $field = shift;
    my $form_name = $field->form ? $field->form->name : 'fld-';
    return $field->form->name . $field->name
}


sub init_widget {'text'}


sub init_order {1}


sub set_order {
    my $field = shift;
    my $form  = $field->form;
    my $order = $form->field_counter || 1;
    $field->order( $order );
    $form->field_counter( $order + 1 );
}


sub init_required {0}


sub add_error {
    my $self = shift;

    my $form = $self->form;

    my $lh;

    # By default errors get attached to the field where they happen.
    my $error_field = $self;

    # Running without a form object?
    if ( $form ) {
        $lh = $form->language_handle;

        # If we are a sub-form then redirect errors to the parent field
        $error_field = $form->parent_field if $form->parent_field;
    }
    else {
        $lh = $Form::Processor::LANGUAGE_HANDLE || $ENV{LANGUAGE_HANDLE} || Form::Processor::I18N->get_handle ||
            die "Failed call to Text::Maketext->get_handle";
    }

    $self->add_error_str( $lh->maketext( @_ ) );

    return;

}


sub init_max_size {10_000}    # sanity check

lib/Form/Processor/Field.pm  view on Meta::CPAN

=pod

=encoding UTF-8

=head1 NAME

Form::Processor::Field - Base class for Fields used with Form::Processor

=head1 VERSION

version 1.162360

=head1 SYNOPSIS

    # Used from another class
    use base 'Form::Processor::Field::Text';
    my $field = Form::Processor::Field::Text->new( name => $name );

=head1 DESCRIPTION

This is a base class that allows basic functionality for form fields.
Form fields inherit from this class and thus may have additional methods.
See the documentation or source for the individual fields.

Look at the L<validate_field> method for how individual fields are validated.

You are encouraged to create specific fields for your application instead of
simply using the fields included with Form::Processor.

=head1 METHODS

=over 4

=item new [parameters]

Create a new instance of a field.  Any initial values may be passed in
as a list of parameters.

=item must_submit

This boolean value defaults to false.

When true AND when the form's attribute "use_existing_values" is
set then this field will not default to any existing value.

This provides a way to selectively disable "use_existing_values"
on a per-field basis.

This has no effect if "use_existing_values" is false on the form.

=item full_name

This returns the name of the field, but if the field
is a child field will prepend the field with the parent's field
name.  For example, if a field is "month" and the parent's field name
is "birthday" then this will return "birthday.month".

=item form

This is a reference to the parent form object.
It's stored weakened references.

=item sub_form

A single field can be represented by more than one sub-fields
contained in a form.  This is a reference to that form.

=item id

Returns an id for the field, which is by default:

    $field->form->name . $field->id

A field may override with "init_id".

=item 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.

The default type is 'text'.

=item order

This is the field's order used for sorting errors and field lists.

=item set_order

This sets the field's order to the form's field_counter
and increments the counter.

The purpose of this is when displaying fields, say in a template,
this can be called with displaying the field to set its order.
Then a summary of error messages can be displayed in the order



( run in 0.490 second using v1.01-cache-2.11-cpan-39bf76dae61 )