You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Steve Wells <we...@gmail.com> on 2006/12/08 04:02:52 UTC

Bean validator - validation method not being called

A bit of validation grief here (Tapestry 4.0.2), I hope someone can help or
provide an example of their own of this (besides the one in Kent Tongs
book).

I'm trying to create a custom validator that grabs maxLength and required
info from the data model.  The "Bean" I define as the validator takes 3
parameters which are being set ok when the form is submitted but the *
validate* method is not called, and despite trying variations to acheive
this it still does not work.

Best elaborated in code:

Validator Bean:

public class DataObjectFieldValidator extends BaseValidator {
    // *** These fields are populated OK via their setters
    private String dataObjectName;
    private String fieldName;
    private DataContext dataContext;

    // This does not get called
    public void validate(IFormComponent iFormComponent, ValidationMessages
validationMessages, Object object) throws ValidatorException {
        if (log.isInfoEnabled()) {
            log.info("iFormComponent: " + iFormComponent);
            log.info("object: " + object);
        }
        throw new ValidatorException("Why is this not getting here???");

.page spec:

<bean name="myValidator" class="com.validator.DataObjectFieldValidator">
        <set name="dataContext" value="ognl:dataContext"/>
        <set name="dataObjectName" value="literal:MyDataObject"/>
        <set name="fieldName" value="literal:myField"/>
    </bean>
    <component id="question" type="TextField">
        <binding name="value" value="question"/>
        <binding name="validators" value="bean:myValidator"/>
    </component>

Java page class:

// This will work for built in validators but not the custom bean
if (getDelegate().getHasErrors()) {
    log.info("Entry Errors found: " + getDelegate().getFirstError());
    return null;
}

TIA,

Steve

Re: Bean validator - validation method not being called

Posted by Steve Wells <we...@gmail.com>.
ok naturally I stumbled over my own solution not 5 mins after posting this.

The solution was to change the bean-validator method to return true
public boolean getAcceptsNull() {
        return true;  // was false
    }

The giveaway was when I actually typed something in and then got the
validation failure, I stupidly kept trying the empty field case. doh!

On 08/12/06, Steve Wells <we...@gmail.com> wrote:
>
> A bit of validation grief here (Tapestry 4.0.2), I hope someone can help
> or provide an example of their own of this (besides the one in Kent Tongs
> book).
>
> I'm trying to create a custom validator that grabs maxLength and required
> info from the data model.  The "Bean" I define as the validator takes 3
> parameters which are being set ok when the form is submitted but the *
> validate* method is not called, and despite trying variations to acheive
> this it still does not work.
>
> Best elaborated in code:
>
> Validator Bean:
>
> public class DataObjectFieldValidator extends BaseValidator {
>     // *** These fields are populated OK via their setters
>     private String dataObjectName;
>     private String fieldName;
>     private DataContext dataContext;
>
>     // This does not get called
>     public void validate(IFormComponent iFormComponent, ValidationMessages
> validationMessages, Object object) throws ValidatorException {
>         if (log.isInfoEnabled()) {
>             log.info("iFormComponent: " + iFormComponent);
>             log.info("object: " + object);
>         }
>         throw new ValidatorException("Why is this not getting here???");
>
> .page spec:
>
> <bean name="myValidator" class="com.validator.DataObjectFieldValidator">
>         <set name="dataContext" value="ognl:dataContext"/>
>         <set name="dataObjectName" value="literal:MyDataObject"/>
>         <set name="fieldName" value="literal:myField"/>
>     </bean>
>     <component id="question" type="TextField">
>         <binding name="value" value="question"/>
>         <binding name="validators" value="bean:myValidator"/>
>     </component>
>
> Java page class:
>
> // This will work for built in validators but not the custom bean
> if (getDelegate().getHasErrors()) {
>     log.info("Entry Errors found: " + getDelegate().getFirstError());
>     return null;
> }
>
> TIA,
>
> Steve
>