I\'m working on a JSF form prototype for inserting data into database table using AJAX data validation This is the JSF page:
You need to create a Validator.
Kickoff example:
@FacesValidator("sessionIdValidator")
public class SessionIdValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
// ...
if (yourDataService.existSessionId(value)) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Session ID is already in use, please choose another.", null));
}
}
}
Use it as follows:
Note that you should use a to show validation messages. You haven't any one in your view. Apply the same for all your other fields:
If you want to use @Resource or @EJB or @Inject in the validator, then replace the @FacesValidator annotation by @ManagedBean or (as you seem to use CDI for some reason) @Named:
@Named
public class SessionIdValidator implements Validator {
@Resource
private DataSource dataSource;
@Inject
private YourDataService yourDataService;
// ...
}
and use it as follows instead