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

NAME

MVC::Neaf::X::Form - Form validator for Not Even A Framework

CAUTION

This module should be moved into a separate distribution or (ideally) merged with an existing module with similar functionality.

Possible candidates include Validator::LIVR, Data::FormValidator, Data::CGIForm, and more.

DESCRIPTION

Ths module provides hashref validation mechanism that allows for showing per-value errors, post-validation user-defined checks, and returning the original content for resubmission.

SINOPSYS

    use MVC::Neaf::X::Form;

    # At the start of the application
    my $validator = MVC::Neaf::X::Form->new( \%profile );

    # Much later, multiple times
    my $form = $validator->validate( \%user_input );

    if ($form->is_valid) {
        do_intended_stuff( $form->data ); # a hashref
    } else {
        display_errors( $form->error ); # a hashref
        show_form_for_resubmission( $form->raw ); # also a hashref
    };

As you can see, nothing here has anything to do with http or html, it just so happens that the above pattern is common in web applications.

METHODS

new( \%profile )

Receives a validation profile, returns a validator object.

In the default implementation, %profile must be a hash with keys corresponding to the data being validated, and values in the form of either regexp, [ regexp ], or [ required => regexp ].

Regular expressions are accepted in qr(...) and string format, and will be compiled to only match the whole line.

NOTE One may need to pass qr(...)s in order to allow multiline data (e.g. in textarea).

NOTE Format may be subject to extention with extra options.

make_rules( \%profile )

Preprocesses the validation profile before doing actual validation.

Returns an object or reference to be stored in the rules property.

This method is called from new() and is to be overridden in a subclass.

validate( \%data )

Returns a MVC::Neaf::X::Form::Data object with methods:

  • is_valid - true if validation passed.

  • data - data that passed validation as hash (MAY be incomplete, must check is_valid() before usage).

  • error - errors encountered. May be extended if called with 2 args. (E.g. failed to load an otherwise correct item from DB). This also affects is_valid.

  • raw - user params as is. Only the known keys end up in this hash. Useful to send data back for resubmission.

do_validate( $raw_data )

Returns a pair of hashes: the cleaned data and errors.

This is called by validate() and is to be overridden in subclasses.

known_keys()

Returns list of data keys subject to validation.

All other keys present in the input SHOULD be ignored.

LICENSE AND COPYRIGHT

This module is part of MVC::Neaf suite.

Copyright 2016-2018 Konstantin S. Uvarin khedin@cpan.org.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.