You can easily define the data constraints for each field on a page using the Field Details tab within FormMaker. In addition, if you create your controls from a datasource (eg XSD) that specifies this information, then these details will be populated automatically.
At runtime, these constraint details will be used to automatically validate the details entered by the user.
It is also possible to change these constraints using JavaScript to dynamically adjust the vaildation that should be applied to a field. Each constraint value is stored as an attribute on the HTML component for the field (eg input tag), and can be easily changed as needed. All these attribute names start with an underscore and are detailed in the following table. You should be able to easily match these with the options in the studio.
For example, if you wanted to make a field mandatory if a checkbox is ticked, you could add an onclick event to the checkbox field to call a function like the following.
It is important to remember that depending on what you are trying to achieve, there may be existing options provided by WebMaker that avoid the need for writing custom script functions. For example, an alternative option to make a field conditionally required would be to place it in a group, and set the group to be conditionally visible based on the value of the checkbox field. In this way the field will not even be visible on the screen (and so wont be validated) until the checkbox is ticked.
It is also possible to add custom validation checks to handle more complex scenarios. For more details on this, please see How do I add my own custom validation checks in addition to the built in functionality?
At runtime, these constraint details will be used to automatically validate the details entered by the user.
It is also possible to change these constraints using JavaScript to dynamically adjust the vaildation that should be applied to a field. Each constraint value is stored as an attribute on the HTML component for the field (eg input tag), and can be easily changed as needed. All these attribute names start with an underscore and are detailed in the following table. You should be able to easily match these with the options in the studio.
[tr][td]_required[/td][td]Must be 'true' or 'false'[/td][/tr] | |||
[tr][td]_type[/td][td]Must be one of 'string' | 'number' | 'boolean' | or 'date'[/td][/tr] |
[tr][td]_minInclusive[/td][td]Applies to number or date types[/td][/tr] | |||
[tr][td]_minExclusive[/td][td]Applies to number or date types[/td][/tr] | |||
[tr][td]_maxInclusive[/td][td]Applies to number or date types[/td][/tr] | |||
[tr][td]_maxExclusive[/td][td]Applies to number or date types[/td][/tr] | |||
[tr][td]_length[/td][td]Applies to string types[/td][/tr] | |||
[tr][td]_minLength[/td][td]Applies to string types[/td][/tr] | |||
[tr][td]_maxLength[/td][td]Applies to string types[/td][/tr] | |||
[tr][td]_regularExpression[/td][td]Applies to string and number types. Can contain multiple regular expressions if separated by the string '*@*@*'[/td][/tr] | |||
[tr][td]_validate[/td][td]Overall control of whether to valdiate this field at all. Must be 'true' or 'false'[/td][/tr] |
For example, if you wanted to make a field mandatory if a checkbox is ticked, you could add an onclick event to the checkbox field to call a function like the following.
function adjustConstraints()
{
var checkboxField = document.getElementById('checkbox_field');
var fieldToAdjust = document.getElementById('adjust_field');
if (checkboxField.checked)
fieldToAdjust.setAttribute('_required', 'true');
else
fieldToAdjust.setAttribute('_required', 'false');
}
It is important to remember that depending on what you are trying to achieve, there may be existing options provided by WebMaker that avoid the need for writing custom script functions. For example, an alternative option to make a field conditionally required would be to place it in a group, and set the group to be conditionally visible based on the value of the checkbox field. In this way the field will not even be visible on the screen (and so wont be validated) until the checkbox is ticked.
It is also possible to add custom validation checks to handle more complex scenarios. For more details on this, please see How do I add my own custom validation checks in addition to the built in functionality?
RE: Can I dynamically change the data constraints for a field?