[Catalyst] General Web- and OO-question

Rodrigo rodrigolive at gmail.com
Fri Mar 27 16:07:01 GMT 2009


>
> My questions:
> a) Where do I store the intermediate values of the attributes. I can't put
> them in the object itself
> as it would generate a "invalid" object. Do I store them simply as data
> somewhere else? Do
> I have to create a "proxy" object with lighter rules?
>

I wouldn't. Just instantiate your object at the entry controller and store
the instance in the $c->session, or wherever you find appropiate depending
on the object characteristics and the kind of state handling you need.

Do your entry validation as part of the attribute checking validation, as in
Moose:

package MyClass;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'AcceptedAge' =3D> as 'Int' =3D> where { $_ >=3D 16 } =3D> message =
{ 'You
must be at least 16 to use this.' };
subtype 'NameStr' =3D> as 'Str' =3D> where { length($_) > 1 } =3D> message =
{ "What
kinda name is '$_'?" };
has 'name' =3D> (is=3D>'rw', isa=3D>'NameStr', required =3D> 1 );
has 'age' =3D> (is=3D>'rw', isa=3D>'AcceptedAge', required =3D> 1 );
has 'sex' =3D> (is=3D>'rw', isa=3D>'Str', required =3D> 1 );

In your (overly-simplified) controller:

sub submit : Local {
       $c->session->{obj} =3D new MyClass( $c->req->params );  # dies if
constraint fails, so trap errors
}


>
> b) How can I verify attribute rules as early as possible in that "enterin=
g"
> process? Do I have to
> dublicate the knowledge of the rules outside the class? (e.g. Length of
> String attribute less than 20)
> The class itself would check such a restriction.
>

Then, just finish validating your object in the "exit" controller.

sub wrap_it_up : Local {
     ...
     if( $c->session->{obj}->validate ) {
         ## yup
     } else {
           ## nope
    }
}

Back in your class, do the more complex validations:

sub validate {
      return 1 if(  ( $self->sex eq 'F' && $self->age >=3D16 )  ## girls
mature earlier...
                       || (  $self->sex eq 'M' && $self->age >=3D18 ) );
}


>
> What ways are you going? Which is the "right" OO-way of doing it?



TIMTOWTDI


>
> At the moment I really don't know where to place the input validation
> knowledge and where and how
> to store the partly created objects. I hope it sounds not to academic, but
> I want to clearly seperate
> the responsibilities.
>

Moose will give you a lot of ammo to help you with this, things such as
coersion and roles. Check with the moosers in their maling list for further
OO questions.

On the other hand, if you need client-side validation, take a peek at
HTML::FormFu, as it may help you with putting some of those rules on the
client side. Catalyst::Model::Adaptor may also come in handy if you feel you
need to turn your class into a model.

-rodrigo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090327/f92f7=
878/attachment.htm


More information about the Catalyst mailing list