Validation of multiple input controls on a form

We have an input form with a number of input fields, 2 of them need to be validated in such a way that one can be blank on submit but not both.
So I need a custom validator for each field that checks if the value is blank and if it is blank check if the other field is also blank.
I have the custom validator working for the control testing for a specific value, but when I test for a blank or null string it won’t work.

Examples:
The following works and the exception is thrown as expected when I type “test” into the control and click submit.

if (value.toString().equalsIgnoreCase("test"))

But I have tried many different things to get it to test for a blank/null string and can’t get it to work:

if (value == null)
if (value.toString().equalsIgnoreCase("")
if (StringUtils.isBlank(value.toString())
if (value.toString().length() == 0)

Any help would be greatly appreciated.

Hi,
using a custom validator will not work in this case cause the your custom validator gets called only when the input field has some value.
Some possible workarounds would be:

  • Add the check in the method (in your view bean) making use of those fields.
  • Add some javascript that makes this validation.
  • (for the lovers of complex solutions :wink: )Add a hidden input for each of the input fields you want to validate. They should have some initial dummy value. In the Blur Client Side event of your input field, empty the value of the corresponding hidden input if the text input has some value and viceversa. So that in the end you validate that both your hidden input fields do not have some value (hope I expressed myself clear enough)

None of the three is perfect, but I hope it helps you somehow…
best regards,
Javier

Thanks for the response :slight_smile:

I don’t have a lot of experience with Designer so this is all learning for me.
How do I access the input field from the java code? Or is it possible to access the fields in the javascript?

Hi
you can do both things…

// get the value in an input control
var nameValue = CAF.model('#{caf:cid('inputName')}').getValue();
// Define a variable to a hidden input control
var hiddenName = CAF.model('#{caf:cid('hiddenInputName')}');
if (nameValue == null || nameValue === 'undefined' || nameValue == '') {
	hiddenName.setValue("name input is empty");
} else {
	hiddenName.setValue('');
}

In the properties of some controls you will see “Client-Side Events”, where you can add the javascript you want to run on those events. Additionally you have some controls (in the Palette) under the Scripts category where you can add some more custom javascript.

hope this helps,
Javier

Thanks so much, that’s exactly what I needed :slight_smile: