Magento 1.9 Validation – Difference Between Validator and Validator.validate()

javascriptmagento-1.9PHP

In the login.phtml, what is the difference between validator and validate – Could someone explain what is the meaning of each of the lines below.

function onepageLogin(button)
        {
            if(loginForm.validator && loginForm.validator.validate()){
                button.disabled = true;
                loginForm.submit();
            }
        }

Thanks.

Best Answer

Take a look at js/prototype/validation.js

.....
onSubmit :  function(ev){
    if(!this.validate()) Event.stop(ev);
},
validate : function() {
    var result = false;
    var useTitles = this.options.useTitles;
    var callback = this.options.onElementValidate;
...

Then take a look at js/mage/adminhtml/form.js or (/js/varien/form.js)

varienForm.prototype = {
    initialize : function(formId, validationUrl){
        this.formId = formId;
        this.validationUrl = validationUrl;
        this.submitUrl = false;

        if($(this.formId)){
            this.validator  = new Validation(this.formId, {onElementValidate : this.checkErrors.bind(this)});
        }
        this.errorSections = $H({});
    },

then

var loginForm = new VarienForm('loginForm', false);

validator is a variable (see this.validator = new Validation..) that store the validation instance.

function onepageLogin(button)
{
    //check to see if the validator instance exist and programmatically validate the form
    if(loginForm.validator && loginForm.validator.validate()){  
        //disable the button to prevent multiple click             
        button.disabled = true;
        //submit the form since all the required field are entered 
        loginForm.submit();
    }
}

See Invoke validation

Related Topic