Validating Field Content
...
Parameter values may contain unresolved references to IzPack variables, which are resolved at installation time.
Info |
---|
The <configuration> section including variable resolution is fully functional beginning from IzPack version 5.1.1. |
Using Validators
Validators are bound to user input fields using the nested <validator> element;
...
<field type="rule" variable="EMAILaddress"> <spec txt="Your Email address:" layout="O:12:U @ O:8:40 . A:4:4" set="0: 1:domain 2:com" resultFormat="displayFormat" /> <validator class="com.izforge.izpack.panels.userinput.validator.RegularExpressionValidator"
txt="Invalid email address!"> txt="Invalid <paramemail address!">
<param name="pattern" value="[a-zA-Z0-9._-]{3,}@[a-zA-Z0-9._-]+([.][a-zA-Z0 -9_-]+)*[.][a-zA-Z0-9._-]{2,4}" />
</validator> </field>
The example of using Regexp validator in text input field:
<field type="text" variable="EMAILaddress"> <spec txt="Your Email address:" set="you@domain.com" size="20" id="" /> <validator class="com.izforge.izpack.panels.userinput.validator.RegularExpressionValidator" txt="Invalid email address!"> <param name="pattern" value="[a-zA-Z0-9._-]{3,}@[a-zA-Z0-9._-]+([.][a-zA-Z0 -9_-]+)*[.][a-zA-Z0-9._-]{2,4}" /> </validator> </field>
An example of using regexp validator in a password field (attribute text wrapped for readability):
<field type="password" align="left" variable="db.password"> <spec> <pwd txt="DB Password:" size="25" set=""/> <pwd txt="Retype Password:" size="25" set=""/> </spec> <validator class="com.izforge.izpack.panels.userinput.validator.PasswordEqualityValidator" txt="Both DB passwords must match." id="key for the error text"/> <validator class="com.izforge.izpack.panels.userinput.validator.RegularExpressionValidator"
id="key for the error text">
txt="Service password must begin with a character and be 8-20 mixed-case characters, numbers, and special characters [#@!$_]." id="key for the error text"> >
<param name="pattern" value="^(?=[a-zA-Z])(?=.*[0-9])(?=.*[#@!$_]) (?=.*[A-Z])(?=.*[a-z])(?!.*[^a-zA-Z0-9#@!$_])(?!.*\s).{8,20}$"/> </validator> </field>
...
You can implement your own custom Validator implementation simply by creating a new class which implements the com.izforge.izpack.panels.userinput.validator.Validator interface. This interface specifies a single method: validate(ProcessingClient) , which returns a boolean value. You can retrieve the value entered by the user by casting the input ProcessingClient for example in the RuleInputField and call the RuleInputField.getText() method. You can also retrieve any parameters to your custom Validator by calling theRuleInputField.getValidatorParams() which returns a java.util.Map object containing parameter names mapped to parameter values. For an example, take a look at com.izforge.izpack.util.RegularExpressionValidator.
...