<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
My questions:<br>
a) Where do I store the intermediate values of the attributes. I can't put them in the object itself<br>
as it would generate a "invalid" object. Do I store them simply as data somewhere else? Do<br>
I have to create a "proxy" object with lighter rules?<br>
</blockquote><div><br>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. <br>
<br>Do your entry validation as part of the attribute checking validation, as in Moose:<br><br>package MyClass;<br>use Moose;<br>use Moose::Util::TypeConstraints;<br>subtype 'AcceptedAge' => as 'Int' => where { $_ >= 16 } => message { 'You must be at least 16 to use this.' };<br>
subtype 'NameStr' => as 'Str' => where { length($_) > 1 } => message { "What kinda name is '$_'?" };<br>has 'name' => (is=>'rw', isa=>'NameStr', required => 1 );<br>
has 'age' => (is=>'rw', isa=>'AcceptedAge', required => 1 );<br>has 'sex' => (is=>'rw', isa=>'Str', required => 1 );<br><br>In your (overly-simplified) controller: <br>
<br>sub submit : Local {<br> $c->session->{obj} = new MyClass( $c->req->params ); # dies if constraint fails, so trap errors <br>}<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
b) How can I verify attribute rules as early as possible in that "entering" process? Do I have to<br>
dublicate the knowledge of the rules outside the class? (e.g. Length of String attribute less than 20)<br>
The class itself would check such a restriction.<br>
</blockquote><div><br>Then, just finish validating your object in the "exit" controller.<br><br>sub wrap_it_up : Local {<br> ...<br> if( $c->session->{obj}->validate ) {<br> ## yup<br> } else {<br>
## nope<br> }<br>}<br><br>Back in your class, do the more complex validations:<br><br>sub validate {<br> return 1 if( ( $self->sex eq 'F' && $self->age >=16 ) ## girls mature earlier...<br>
|| ( $self->sex eq 'M' && $self->age >=18 ) ); <br>}<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
What ways are you going? Which is the "right" OO-way of doing it?</blockquote><div><br><br>TIMTOWTDI<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
At the moment I really don't know where to place the input validation knowledge and where and how<br>
to store the partly created objects. I hope it sounds not to academic, but I want to clearly seperate<br>
the responsibilities.<br>
</blockquote><div><br>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. <br><br>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.<br>
<br></div></div>-rodrigo<br>