Advanced Validation

4 Messages Forum Options Options
Embed this topic
Permalink
al_masson
Advanced Validation
Reply Threaded MoreMore options
Print post
Permalink
Hi,

What would be the best approach for advanced validation? I mean, checking if fields are populated are quite simple with Click default validation but requirements are more complicated than that. I need to go to the DB to check some more stuff before accepting the form.

Is there something built-in on Click to ease my life?

Regards,

Alexandre Jacques

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Click-user mailing list
Click-user@...
https://lists.sourceforge.net/lists/listinfo/click-user
sabob
Re: Advanced Validation
Reply Threaded MoreMore options
Print post
Permalink
Hi Alexandre,

Alexandre Jacques wrote:
>
> What would be the best approach for advanced validation? I mean,
> checking if fields are populated are quite simple with Click default
> validation but requirements are more complicated than that. I need to go
> to the DB to check some more stuff before accepting the form.


Is the validation presentation logic or business logic? If it is
business logic I would recommend you creating a separate package for
your validation rules.


>
> Is there something built-in on Click to ease my life?


Not really no. But you can use anonymous inner classes to encapsulate
complex validation:

form = new Form() {
   public void validate() {
     super.validate();

     if (getError() != null) {
       //error already present
       return;
     }

     String value = getValue();

     //do complex validation for value
   }
}


kind regards

bob

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Click-user mailing list
Click-user@...
https://lists.sourceforge.net/lists/listinfo/click-user
Malcolm Edgar-2
Re: Advanced Validation
Reply Threaded MoreMore options
Print post
Permalink
In reply to this post by al_masson
In the past I have been a big proponent if leveraging the database
constraints in form validation logic, which is what CayenneForm is all
about. However, I find in practice the field level database contraints
are quite limited, i.e. mandator status and field length, and your
applicaiton needs to provider additional constraints, i.e. a email
VARCHAR(50) column should use a EmailField to apply the email address
validation rules.

Even things like column mandator status aren't always useful in real
life, as objects often go through a lifecycle where they pick up
information, so you can even make many columns mandator because in
some scenarios they aren't, while in other scenarios they become
mandator.

How you should go about building your validation rules really depends
upon the problem you are solving. If its a well defined and
constrained application, I would tend to put my validation logic in
the Pages and maybe use custom Fields classes which have extended
valdiation logic, if there is opportunity for reuse.

If the problem is more generic, I think there is merit in looking at a
more meta-data driven validation model, where the business tier
exposes the validation constraints, which are applied in the UI tier.
The thing to keep in mind here is the cost/benefit, i.e. don't build
something you aint going to need.

regards Malcolm Edgar

On Mon, Aug 11, 2008 at 1:36 PM, Alexandre Jacques <dev.lists@...> wrote:

> Hi,
>
> What would be the best approach for advanced validation? I mean, checking if
> fields are populated are quite simple with Click default validation but
> requirements are more complicated than that. I need to go to the DB to check
> some more stuff before accepting the form.
>
> Is there something built-in on Click to ease my life?
>
> Regards,
>
> Alexandre Jacques
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Click-user mailing list
> Click-user@...
> https://lists.sourceforge.net/lists/listinfo/click-user
>
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Click-user mailing list
Click-user@...
https://lists.sourceforge.net/lists/listinfo/click-user
florin.g
Re: Advanced Validation
Reply Threaded MoreMore options
Print post
Permalink
Not sure how advanced my method is, yet I ended up using Apache commons validation at field level:
[...]
NumberField field = new NumberField("txtFld", true) {
            public void validate() {
                super.validate();
                // by this point, Click makes sure that the field value is not null and that it is a number
                if (!IntegerValidator.getInstance().isInRange(Integer.parseInt(getValueObject().toString()), 0,99)) {
                    error = getLabel() + " Must be a number between 0-99"; // or localized

                }
            }
        };

form.add(field);
[...]
I looked at Wicket, oVal and JGoodies. Both Wicket and JGoodies make sense within their own framework. If used outside of their frameworks (as it is possible), they are reduced to the value of apache commons.

Something more complex such as oVal, if I understood it correctly, requires AspectJ binding as well as validation at every single method call not only when Form is submitted. Too much overhead for my needs.

Here is a wicket example used outside of wicket:

                Validatable v = new Validatable(getValueObject());
                NumberValidator.RangeValidator.range(0,99).validate(v);
                if (!v.isValid()) {
                    error = getLabel() + v.getErrors().get(0).toString();
                }