On Feb 4, 2006, at 7:03 PM, RJ Herrick wrote:

In validating my form input I look at my DB tables to see what types of input they will accept (valid values, maxlength, NULL ok, etc). This data is about the database, not the values coming in off the forms. It will stay the same until I alter my tables, which I won't be doing very often (in production at least :) ). So I'm looking to avoid gathering this metadata via DB calls every time I need to validate a form by instead using the cached metadata, stored somewhere by mod_perl. My thought was to put it into it's own namespace and have it available globally, loaded from one of my modules at startup. Does anyone else understand what I'm trying to say? I apologize if I'm not expressing myself in the right vernacular.
Thanks,
RJ Herrick

Oh. In that case... i put stuff like that in its own namespace during the startup.pl phase.

Suggestion though- (and this gets completely off topic)

it might make sense to have some sort of form builder/validator class

At the risk of making some people here cringe, in my projects I do something like

/Form.pm
/Form/account/registration.pm
/Form/account/settings.pm

where Form.pm is a base class that has my config data (ie, use libapreq for form parsing, db handles, etc) , and inherits from my form parsing class registration would be a subclass of Form that has a symantic description like

our %Fields=(
'email' =>{
        type=>'text',
        'maxchars'=>'6'
        ...
        'validators' => ['REGEX','SQL_FUNCTION'],
        'validator_regex = 'email',
        validator_sql = ['false','SELECT user_id FROM user WHERE user_id = ?;']
        ' validator_regex' => 'not an email',
        'validator_email' => 'already registerd'
}
);

when I do a form validation, i just do:
        my $form = Form::Account::Registration->new();
        $form->validate();
        if ( $form->has_errors() )
        {
                $self->printform;return;
}
        my $email = $form->get_validated('email')

i do it in classes so that I can re-use the forms. i had one project where I did a formfield class that had versions of every field used, and then i'd just specify while field description in the form class. but that saved me 2% memory while it gave me 30% more confusion. so i said screw that.

there are a bunch of CPAN packages that do form validation like that , but most are focused on building , which can be overkill.

Reply via email to