سؤال

I'm trying to make a simple custom aui:validator for the birthday field on the registration form but it is simply not working. Any ideas?

<aui:input name="birthday" value="<%=birthday%>">
    <aui:validator name="custom" errorMessage="some-error">
        function (val, fieldNode, ruleValue) {
            return false;
        }
    </aui:validator>
</aui:input>

What I'd like to achieve is to check if the user is 18 years old at the registration.

Any hints would be greatly appreciated!

هل كانت مفيدة؟

المحلول 2

Unfortunately I wasn't able to use Lucky Boy's solution either. For some mysterious reason it didn't work at the create_account.jsp file.

As a workaround I create a new exception in the header of the file:

<liferay-ui:error exception="<%=NotOldEnoughException.class%>"
    message="Not old enough" />

And made the checking in a hooked version of the EditUserAction file.

نصائح أخرى

Here i am validating that selected date is greater than today's date.You can change as per your need.

<script>
 AUI().ready('aui-form-validator', 'aui-overlay-context-panel', function(A) {

   A.mix(
        YUI.AUI.defaults.FormValidator.RULES,
        {
            dateCustomRule: function(val, fieldNode, ruleValue) {
                var val = new Date(Date.parse(val,"MMM dd yyyy"));
                var today=new Date();

                return (val>today);

            }
        },
        true
    );

    var validator2 = new A.FormValidator({
        boundingBox: document.<portlet:namespace/>form,

        fieldContainer: 'p',

        rules: {        
            <portlet:namespace />dueDate:{
                required: true,
                dateCustomRule:true
            }
        },
    fieldStrings: {

            <portlet:namespace />dueDate: {
                required: 'Due Date Required',
                dateCustomRule: 'Date must be greater than Today\'s Date'
            }

        },

        on: {
            validateField: function(event) {
            },

            validField: function(event) {
            },

            errorField: function(event) {
            },

            submitError: function(event) {
                var formEvent = event.validator.formEvent;
                var errors = event.validator.errors;

            },

            submit: function(event) {
                var formEvent = event.validator.formEvent;
                return false;
            }
        }
    });

});
</script>

the correct way to make the validator would be something like this.

I hope it helps you,

                         <aui:validator name="custom"  errorMessage="Tienes que ser mayor de 18 años">
                                     function (val, fieldNode, ruleValue) {
                                             var val = new Date(Date.parse(val,"MMM dd yyyy"));
                                             var adult=new Date();

                                             adult.setFullYear(adult.getFullYear() - 18)

                                             return (val<adult);
                                     }
                              </aui:validator>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top