I have foreign key inside my Customer table.
@JoinColumn(name = "DISCOUNT_CODE", referencedColumnName = "DISCOUNT_CODE")
@ManyToOne(optional = false)
private DiscountCode discountCode;
I have a form that contains all fields of this table (including the foreign key discountCode and its description from the other table).
I want to be able to show a message that this foreign key does not exist in case that the user entered an input that does not exist in the foreign key table.
When I onblur this field, then I’m retriving its description from the table. How can I show the error message of invalid field when the user onblurs it and it does not exist in the table?
What you need is a
Validator. It should look like this:In your .xhtml file, you can declare this validator as following:
One thing to note is I assume that you would inject an EJB to check the existence of the discount code with the
checkDiscountCodeExistence()function. Hence, I annotated the above Validator as a@ManagedBean. If you don’t need to inject any EJBs, you can annotate the Validator with@FacesValidator.