Validating Persistent Record Fields and using feedback panel to show errors

I’m working on a gateway module that needs to store some properties as Persistence Records. I’m validating a StringField, and instead of throwing an exception that would cause the UI form to exit, I want to display a message on the feedback panel at the top of the form, to give the user a chance to fix the input. Something like this:

For context here is the snippet I am using:

public static final StringField FooBar = new StringField(META, "FooBar")
      .addValidator(new SValidatorI() {
        @Override
        public void onValidate(SFieldMeta field, SRecordInstance instance) throws SException.Validation {
          Boolean isEnabledValue = instance.getBoolean(isEnabled);
          String foobarValue = instance.getString(field);

          if (isEnabledValue && (foobarValue == null || foobarValue.trim().isEmpty())) {            
            throw new SException.Validation("Foo bar cannot be null or empty when enabled.");
          }
        }
      });

Has anyone else encountered this before?

You don't need a validator for that case, just create the StringField with SFieldFlags.SMANDATORY instead:

public static final StringField HOSTNAME = 
   new StringField(META, "Hostname", SFieldFlags.SMANDATORY);

When you do use a validator, implement IValidator, and call IValidatable::error when there's a validation error.

Thanks so much for the quick reply!

You’re right - StringField with SFieldFlags.SMANDATORY does work. However, in my case, the validation logic also needs to take into account the values of some other fields.

I didn’t come across IValidator in the SDK docs. Just to confirm, I’m working with SDK version 8.1.37.

It's part of Wicket, not part of our SDK docs.