You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Stojce Dimski <sd...@yahoo.it> on 2007/08/18 17:17:06 UTC

generic validator questions

Hi I am new to wicket and during my studying phase (1.3b2) I wrote a
small validator for my pilot project.
Validator checks that class indicated by some control implements
specified interface or is subclass of specified class. It works like
charm.
But I have a big problems locating the messages for display purposes.
What is the correct way to make a _generic_ validator (like
'PatternValidator') not tied to any form and specify his messages in
some .properties file ???
Can some kind soul enlight me ?



import org.apache.wicket.validation.*;
import org.apache.wicket.validation.validator.*;

public class ClassValidator extends AbstractValidator {
    private static final long serialVersionUID = 1L;
    private final Class <?> validClass;

    public ClassValidator (final Class <?> validClass) {
        this.validClass = validClass;
    }

    @Override
    protected void onValidate (final IValidatable validatable) {
        final String validatableClassName = (String)
validatable.getValue();
        try {
            final Class <?> validatableClass =
Class.forName(validatableClassName);
            final Object validatableInstance =
validatableClass.newInstance();
            if (!validClass.isInstance(validatableInstance))
                error(validatable, "notValid");
        } catch (ClassNotFoundException problem) {
            error(validatable, "notFound");
        } catch (InstantiationException problem) {
            error(validatable, "instantiation");
        } catch (IllegalAccessException problem) {
            error(validatable, "illegalAccess");
        }
    }
}



      ___________________________________ 
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: http://it.docs.yahoo.com/nowyoucan.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: generic validator questions

Posted by Igor Vaynberg <ig...@gmail.com>.
im not sure exactly what your usecase is.

we have something that is kind of similar, namely new TextField("number",
Integer.class); which would type convert the entered string into an Integer
and error out if the conversion could not be performed.

but i dont think this is the exact match to whatever it is you are trying to
achieve.

-igor


On 8/18/07, Stojce Dimski <sd...@yahoo.it> wrote:
>
> Hi Igor,
>
> First thing, thanks, your hints gave me the direction and I solved the
> issue... I like the approach 'Classname.Key' and I think it's right for
> this category of generic validators as are those in 'validator'
> package.
> It was just a question of overriding one method:
>
> @Override
> public void error (final IValidatable validatable, final String
> resourceKey) {
>     super.error(validatable, resourceKey() + "." + resourceKey);
> }
>
> and everything worked like a charm ;-)
>
> What do you think of adding this validator in trunk ?
>
> Cheers,
> Stojce
>
>
>
>       ___________________________________
> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail:
> http://it.docs.yahoo.com/nowyoucan.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: generic validator questions

Posted by Stojce Dimski <sd...@yahoo.it>.
Hi Igor,

First thing, thanks, your hints gave me the direction and I solved the
issue... I like the approach 'Classname.Key' and I think it's right for
this category of generic validators as are those in 'validator'
package.
It was just a question of overriding one method:

@Override
public void error (final IValidatable validatable, final String
resourceKey) {
    super.error(validatable, resourceKey() + "." + resourceKey);
}

and everything worked like a charm ;-)

What do you think of adding this validator in trunk ?

Cheers,
Stojce



      ___________________________________ 
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: http://it.docs.yahoo.com/nowyoucan.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: generic validator questions

Posted by Igor Vaynberg <ig...@gmail.com>.
On 8/18/07, Stojce Dimski <sd...@yahoo.it> wrote:
>
> Hi Igor
>
> The error message says:
>
> Could not locate error message for error:
> [org.apache.wicket.validation.ValidationError message=[null],
> keys=[notFound, ClassValidator], variables=[]]


the problem looks like is that its looking for a "notFound" key or a
"ClassValidator" key, not for a "ClassValidator.notFound" key. so just
adjust your code.

-igor


notFound - is my key
> ClassValidator - is the name of the class
>
> the .propeties file is together with .class file and and have identical
> names...
>
> inside .properties i have a row like this:
>
> ClassValidator.notFound=${input} cannot be found on a classpath.
>
> What is wrong with this ?
>
> Thanks,
> Stojce
>
>
>
>       ___________________________________
> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail:
> http://it.docs.yahoo.com/nowyoucan.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: generic validator questions

Posted by Stojce Dimski <sd...@yahoo.it>.
Hi Igor

The error message says:

Could not locate error message for error:
[org.apache.wicket.validation.ValidationError message=[null],
keys=[notFound, ClassValidator], variables=[]]

notFound - is my key
ClassValidator - is the name of the class

the .propeties file is together with .class file and and have identical
names...

inside .properties i have a row like this:

ClassValidator.notFound=${input} cannot be found on a classpath.

What is wrong with this ?

Thanks,
Stojce



      ___________________________________ 
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: http://it.docs.yahoo.com/nowyoucan.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: generic validator questions

Posted by Igor Vaynberg <ig...@gmail.com>.
hmm, im not sure. debug and see what key it is actually constructing. tbh i
dont use abstractvalidator, just dont see the value it brings.

-igor


On 8/18/07, Stojce Dimski <sd...@yahoo.it> wrote:
>
> Thanks Igor,
>
> I tried with 'application-scoped .properties' by putting
> 'WebAppName'.properties in the same package as my app class with
> content as:
> ClassValidator.notValid=...
> ClassValidator.notFound=...
>
> but it didn't worked, where I am wrong ?
>
>
>
>       ___________________________________
> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail:
> http://it.docs.yahoo.com/nowyoucan.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: generic validator questions

Posted by Stojce Dimski <sd...@yahoo.it>.
Thanks Igor,

I tried with 'application-scoped .properties' by putting
'WebAppName'.properties in the same package as my app class with
content as:
ClassValidator.notValid=...
ClassValidator.notFound=...

but it didn't worked, where I am wrong ?



      ___________________________________ 
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: http://it.docs.yahoo.com/nowyoucan.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: generic validator questions

Posted by Igor Vaynberg <ig...@gmail.com>.
right now you have a few choices:
you can load that .properties file yourself from inside your validator
you can put these properties into application-scoped .properties file.

you can also add an rfe into our jira to allow validators to have their own
.properties bundles.

-igor


On 8/18/07, Stojce Dimski <sd...@yahoo.it> wrote:
>
> Hi I am new to wicket and during my studying phase (1.3b2) I wrote a
> small validator for my pilot project.
> Validator checks that class indicated by some control implements
> specified interface or is subclass of specified class. It works like
> charm.
> But I have a big problems locating the messages for display purposes.
> What is the correct way to make a _generic_ validator (like
> 'PatternValidator') not tied to any form and specify his messages in
> some .properties file ???
> Can some kind soul enlight me ?
>
>
>
> import org.apache.wicket.validation.*;
> import org.apache.wicket.validation.validator.*;
>
> public class ClassValidator extends AbstractValidator {
>     private static final long serialVersionUID = 1L;
>     private final Class <?> validClass;
>
>     public ClassValidator (final Class <?> validClass) {
>         this.validClass = validClass;
>     }
>
>     @Override
>     protected void onValidate (final IValidatable validatable) {
>         final String validatableClassName = (String)
> validatable.getValue();
>         try {
>             final Class <?> validatableClass =
> Class.forName(validatableClassName);
>             final Object validatableInstance =
> validatableClass.newInstance();
>             if (!validClass.isInstance(validatableInstance))
>                 error(validatable, "notValid");
>         } catch (ClassNotFoundException problem) {
>             error(validatable, "notFound");
>         } catch (InstantiationException problem) {
>             error(validatable, "instantiation");
>         } catch (IllegalAccessException problem) {
>             error(validatable, "illegalAccess");
>         }
>     }
> }
>
>
>
>       ___________________________________
> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail:
> http://it.docs.yahoo.com/nowyoucan.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>