Hi,
Is there a way to set a custom validation message and trigger the validation logic?
In my particular case, I have a multi-checkbox, which has over 50 items to choose from.
It is a required field. Therefore, if none of the checkboxes are checked, I should issue the standard "You must specify a value for this field". That is fine. However, I also need to check the selected count and limit it up to 7 choices. How can I implement such validation?
Apart from the issue at hand, how can I implement custom validation of a field in general?
Thank you in advance.
Is there a way to set a custom validation message and trigger the validation logic?
In my particular case, I have a multi-checkbox, which has over 50 items to choose from.
It is a required field. Therefore, if none of the checkboxes are checked, I should issue the standard "You must specify a value for this field". That is fine. However, I also need to check the selected count and limit it up to 7 choices. How can I implement such validation?
Apart from the issue at hand, how can I implement custom validation of a field in general?
Thank you in advance.
RE: custom validation
There are a few different approaches you can use to implement custom validation and error message functionality.
Generally the simplest option is to use the built in Raise Error event action. This action allows you to specify the field the error applies to, and the message to display.
To use this, you would add this new action to your submit button processing before the existing validation or submission step. You would then add a condition to this action to check if the error applies or not.
For fields that support multiple values, the hyf.util.getFieldValue function will return an array of all the selected values if more than one is selected. Therefore you should be able to use a Custom Check condition like the following to check if too many entries are selected:
(typeof(hyf.util.getFieldValue('multi_check')) != 'string') && (hyf.util.getFieldValue('multi_check').length > 7)
Unfortunately in your case, I think there is currently an issue with the Raise Error action when applied to multi checkbox fields which can cause the error to not be displayed. If you find this, you can work around it by using a Perform Custom Script action with the following kind of content instead of using the Raise Error action directly:
hyf.FMAction.raiseError.handle({name: 'ErrorField', option: 'PageField', value: 'multi_check1'}, {name: 'Message', option: 'Static', value: 'You can only select a maximum of 7 options'}, objEventSource);
Where the first value should be the name of the field with a 1 after it, and the second value provides the message to display.
If you need to do more advanced validation checking, or wish to have more control over the messages that are displayed, then you can use some custom JavaScript code to achieve this. Please see the following forum entries for more details:
How do I add my own custom validation checks in addition to built in functionality?
How do I customize the client side validation messages displayed on a page?
I hope this helps.
Please let me know if you have any further questions.
Regards,
Gerard
RE: custom validation
Thank you for the suggestion. It solved my problem. I have a separate custom validation function which will be called on some button click. I added the following call to issue the custom error message.
hyf.FMAction.raiseError.handle(
{name: 'ErrorField', option: 'PageField', value: lastSelectedItem.id},
{name: 'Message', option: 'Static', value: 'You can only select a maximum of 7 options'},
null);
As for the custom message, I set different message in "Custom Error Message" field under "Hint and Error Options" section of the Property tab, which overrode the error message I specified in the javascript. I guess I can use either one.
BTW, how do we call this validation function as part of the "as you type", meaning, when items are checked/unchecked?
RE: custom validation
I'm glad you managed to get it working.
The message entered under 'Custom Error Message' gets the highest priority, so yes this will be used instead of any messages that might otherwise be output (including from a 'raise error' action).
With regards to the 'as you type' validation, this uses onclick, onkeyup, and onchange events as appropriate for the control.
You can just add your own custom validation processing to an event for the control in the same way as you have done to the main form submission button.
Regards,
Gerard