Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorial:validation_process [2020/04/16 10:56] (current)
Line 1: Line 1:
 +==== Principles ====
 +The validation process is totally integrated to the editor.
 +  * You cannot save an editor when a validation error is encountered..
 +  * When displaying, modifying, saving data, the validation process is triggered.
 +To illustrate this issue, choose in the menu the option <wrap adicode>​Film / Create Film</​wrap>:​\\ \\
 +You can see that you obtain 2 errors corresponding to 2 mandatory fields. Each time you give a value in a field, an error is removed.
 +<columns 100% l 500px middle>
 +{{:​features:​new_film.jpg?​500 |Launch query}} ​
 +<​newcolumn>​
 +In fact, the attribute <wrap adicode>​mandatory="​true"</​wrap>​ of fields <wrap adicode>​title,​ languageByLanguageId </​wrap>​ is transformed as a validator during the generation process of UI classes.\\ \\
 +So, 2 errors occur when asking for a new Film record and you must fulfill these fields.\\ \\
 +Validators is a way to control data when there are displayed or seized.\\
 +There are two kinds of Validator:
 +  * XML written Validators. All the code needed for the validator is written in the XML file.
 +  * Validators calling a validator class: Create a Java class which implements interface <wrap adicode>​org.adichatz.engine.validation.IValidator and reference it in the XML file.</​wrap>​
 +</​columns>​
 +\\
 +
 +
 +
 +==== Adding validator in XML file ====
 +For example, open file <wrap adicode>​$projectDirectory/​resources/​xml/​include/​detail/​FilmDIGENERATED.xml with XML editor.</​wrap>​
 +Change element:
 +<sxh xml; title: excerpt from '​FilmDI.axml'​ file.>
 +        <text textLimit="​255"​ property="​description"​ layoutData="​span 3" id="​description"/>​
 +</​sxh>​
 +by:
 +<sxh xml; highlight: [2,​3,​4,​5,​6,​7];​ title: excerpt from '​FilmDI.axml'​ file.>
 +        <text layoutData="​span 3" property="​description"​ id="​description">​
 + <​validators>​
 +                <​validator key="​titleLength"​ errorMessage="​Description should not be null" warningMessage="​Description generally has more than 10 characters.">​
 + <​warningWhen>​return ((String) getValue()).length() &lt; 11;</​warningWhen>​
 + <​errorWhen>​return null == getValue() || 0 == ((String) getValue()).trim().length();</​errorWhen>​
 + </​validator>​
 + </​validators>​
 + </​text>​
 +</​sxh>​
 +<WRAP adihi>A validator has just been created that:
 +  * triggers an error if the description field contains only blank characters or is a null string.
 +  * triggers a warning (not blocking) when description text contains less than 11 characters.
 +</​WRAP>​\\
 +==== Using a pre-defined validator class ====
 +Some validator could be complex or could be used in several xml files. For that, a better way is to use a predefined validator class. Syntax is:
 +<sxh xml; highlight: [7]; title: excerpt from '​FilmDI.axml'​ file.>
 +        <text property="​description"​ layoutData="​span 3" id="​description">​
 +            <​validators>​
 +                <​validator key="​titleLength"​ errorMessage="​Description should not be null" warningMessage="​Description generally has more than 10 characters.">​
 +                    <​errorWhen>​return null == getValue() || 0 == ((String) getValue()).trim().length();</​errorWhen>​
 +                    <​warningWhen>​return ((String) getValue()).length() &lt; 11;</​warningWhen>​
 +                </​validator>​
 +                <​validator key="​alphaNumeric"​ validatorClassName="​org.mycompany.myproject.gencode.custom.TitleValidator"/>​
 +            </​validators>​
 +        </​text>​
 +</​sxh>​
 + The <wrap adicode>​TitleValidator</​wrap>​ class must extends the class <wrap adicode>​org.adichatz.engine.validation.AValidator</​wrap>​.
 +If you create the class in a subpackage of org.adichatz.gencode in source folder resources/​gencode/​src,​ you will not have to close and relaunch the application because these classes are dynamically loaded. To create the class, use the Eclipse IDE from which you launched the application.
 +Code could be like this:
 +<sxh java>
 +package org.mycompany.myproject.gencode.custom;​
 +
 +import java.util.regex.Pattern;​
 +
 +import org.adichatz.engine.controller.IValidableController;​
 +import org.adichatz.engine.controller.field.TextController;​
 +import org.adichatz.engine.validation.AValidator;​
 +import org.eclipse.jface.dialogs.IMessageProvider;​
 +
 +public class TitleValidator extends AValidator {
 +
 + public TitleValidator(IValidableController triggeringController,​ String key) {
 + super(triggeringController,​ key);
 + }
 +
 + @Override
 + public int validate() {
 + String title = (String) ((TextController) triggeringController)
 + .getValue();​
 + Pattern pattern = Pattern.compile("​(\\w| )+");
 + if (null != title && !pattern.matcher(title).matches()) {
 + return setLevel(IMessageProvider.ERROR,​
 + "​Only alphanumeric characters are accepted."​);​
 + } else
 + return setLevel(IMessageProvider.NONE,​ null);
 + }
 +
 +}
 +</​sxh>​
 +<WRAP adihi>A validator has just been created. The validator triggers an error when the title don't have an alphanumeric value.</​WRAP>​