[html-formfu] Callbacks

Benjamin Martin benjamin at opusvl.com
Thu Jul 8 12:53:39 GMT 2010


Hello,

The advice I got here was very useful but being abit of a novice I did 
not find it clearly in the docs ... so here is abit of POD for the 
FormFu CookBook that might help other novices see the light.

--- snip ---

=head2 Implement a unique username check.

A common task for a form is user registration. This usually requires 
that the username
be unique. By way of example this is how it can be done (using a typical 
Catalyst app)

     package MyApp::FormFu::Constraint::Username;
     use strict;
     use base 'HTML::FormFu::Constraint';
     sub constrain_value
     {
         my $self                = shift;
         my ( $value, $params)   = @_;
         my $c = $self->form->stash->{context};
         my $matched = $c->model('DB::Users')->search( { username => 
$value } )->count;
         $self->{message} = "Username already in use";
         return ( $matched > 0 ) ? 0 : 1;
     }
     1;

Then in your formfu config

     ---
     elements:
       - type: Text
         name: username
         label: "Enter your desired username"
         constraints:
           - Required
           - '+MyApp::FormFu::Constraint::Username'

For more info see the section 'Implement a custom constraint / validator'.

--- snip ---


On 05/07/10 14:00, Benjamin Martin wrote:
> Hi Carl,
>
> Thanks for the tip.. that sounds like an excellent, clean tidy way to 
> tackle my problem.
>
> When I have got a solution ready I'll put some POD on here for the 
> CookBook if you want.
>
> cheers,
> -Ben
>
>
> On 05/07/10 13:55, Carl Franks wrote:
>> Hi,
>>
>> I'd recommend writing a custom constraint class, rather than using 
>> Callback.
>> It just needs to inherit from HTML::FormFu::Constraint and have a sub
>> named 'constrain_value' which will receive the parameters ( $self,
>> $value, $params).
>> You can then access the Catalyst context via 
>> $self->form->stash->{context}
>>
>> It can also define a 'constrain_values' sub to override how multiple
>> values are handled, or override 'process' to take complete control -
>> see the source of HTML::FormFu::Constraint for details.
>>
>> Carl
>>
>>
>> On 2 July 2010 09:27, Benjamin Martin<benjamin at opusvl.com>  wrote:
>>> Hello,
>>>
>>> In my catalyst app I have a method that is used for a Callback 
>>> constraint.
>>> In my Root.pm controller use a session and log in a user the usual way.
>>>
>>>
>>>         package MyApp::Controller::ControllerA;
>>>         ...
>>>         sub formfu_callback_checka
>>>         {
>>>                 # need to validate using something in session?..
>>>         }
>>>
>>>
>>> My question is, what is the best way to get hold of the session/user 
>>> data
>>> from within my callback method?
>>>
>>> thanks for any help, apologizes if this gets asked all the time,
>>> -Ben
>>>
>>>
>>> _______________________________________________
>>> HTML-FormFu mailing list
>>> HTML-FormFu at lists.scsys.co.uk
>>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>>>
>> _______________________________________________
>> HTML-FormFu mailing list
>> HTML-FormFu at lists.scsys.co.uk
>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>>
>
>
> _______________________________________________
> HTML-FormFu mailing list
> HTML-FormFu at lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>




More information about the HTML-FormFu mailing list