How do I make a custom attribute required field on keycloak registration form

Viewed 7665

I have the registration process working so that the custom attribute of "phone" is being stored when the user clicks register, if the user typed a phone number in that is. However, I would like it to be a required field and prevent registration from occurring if left blank. I have not found any keycloak documentation about how to do this.

1 Answers

You have to implement a SPI that extend the registration form. You can use standard FormAction as base. You will have to add you validation logic to validate() method. For instance:

String phoneNumber = formData.getFirst(FIELD_PHONE_NUMBER);
if (Validation.isBlank(phoneNumber) {
  errors.add(new FormMessage(FIELD_PHONE_NUMBER, MISSING_PHONE_NUMBER));
}
Related