JulioHM
JulioHM

Reputation: 1309

Add a default validator to my custom UIComponent

I am developing a custom JSF UIInput which will expect a specially crafted value from the user. In order to validate the user input, I need to run a series of checks which relate only the application at hand. It's not just a normal email, or phonenumber input field. It's a text input which has a composite value that needs to be deconstructed into parts and each part needs to be validated.

I managed to create the component itself.

@FacesComponent("MyComponent")
public class MyComponent extends UIInput {
   public String getFamily() {...}

   encodeBegin() {...}
   encodeEnd() {...}

}

mycomponent.taglib.xml is bundled inside META-INF and I can use and test the component successfully.

I also have a validator class implemented which does all the work. It's ready.

public class AutomationDetectionValidator implements Validator {
   public void validate(...) throws ValidatorException {...}
}

However, I need to include a custom validator to this component as a default. I can do this manually as follows:

<custom:myComponent>
    <f:validator validatorId=”myComponentValidator”/>
</custom:myComponent>

But the expected usage would be

<custom:myComponent/>

And the validator should be there automatically.

I've been googling all day now, but I can find are basic examples of the usage we don't want.

Upvotes: 1

Views: 360

Answers (1)

BalusC
BalusC

Reputation: 1108632

Just add it in component's constructor.

public MyComponent() {
    addValidator(new AutomationDetectionValidator());
}

Upvotes: 2

Related Questions