0

I want to create a new type of aui validator. For example :

<aui:input name="firstName" type="text" maxlength="40">
    <aui:validator name="required" />
    <aui:validator name="alpha" />
</aui:input>

the alpha validator does not accept the space character, i want to use a type that accepts alpha characters plus the space character, i have already a solution using javascript to use a custom validator, but i want to define a new validator type to use like the others by invoking the validator tag e.g :

<aui:validator name="myValidator" />

Is that possible ?! and how can i do it ;)

Mathis Hobden
  • 356
  • 2
  • 7
  • 19

2 Answers2

1

Try your luck on it :)

<aui:input name="firstName" type="text">
    <aui:validator name="myValidator" errorMessage="numbers-not-allowed">
        function(val, fieldNode, ruleValue){ 
            var matches = val.match(/\d+/g);
            if(matches != null)
                return false;
            else
                return true;
       }
    </aui:validator>
</aui:input>
Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
  • Thank you Parkash Kumar, but i already have a javascript function that do the same, my question is about creating may be a new java class that extends Validator and do validation without using javascript – Mathis Hobden Dec 12 '13 at 19:44
  • Then you need to modify aui taglib and add your custom validator in it. – Parkash Kumar Dec 12 '13 at 19:48
  • What i did is to modify the file liferay-portal-6.1.2-ce-ga3\tomcat-7.0.40\webapps\ROOT\html\js\aui\aui-form-validator\aui-form-validator.js, i just modified the error message of the validator "number" type, i restarted my server but nothing happens, if i enter a text data into a field that should contains just number, the popup displays the original erro message not mien. i tried to copy the js file like we do with jsp hooks but nothing happened always the original error message is displayed. Could you please explain how to modify the aui taglib properly or give me a documentation link, thanx – Mathis Hobden Dec 13 '13 at 15:13
0

As an alternative to Parkash answer, you can make a separate jspf file and write your validations like this:

<aui:script use="liferay-form">
    var ns = '<portlet:namespace/>';

    window.onload = function() {
    
    var form = Liferay.Form.get(ns+'form_add_user');
    
    form.set(
        'fieldRules', 
        [
          {
                fieldName:      ns+'middlename',
                validatorName:  'custom_middlename',
                custom:         true,
                errorMessage:   'Only text and spaces here!',
                body:           function(value, field, rule) 
                {
                    return /^[a-zA-Z\s\.\u00E0-\u00FC]+$/.test(value);
                }
            },
            ...
        ]
     );
}
</aui:script>

Found in Liferay 7.0 documentation