Skip to content

Added callback onElementValidate and possibility to customize errorMessa... #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2014

Conversation

lagden
Copy link
Contributor

@lagden lagden commented Mar 21, 2014

Added callback onElementValidate and possibility to customize errorMessage through errorMessageTemplate config param

Usage example

<form id="myTestForm" action="index.html" method="POST">
    <div>
        <label for="user">User</label>
        <input id="user" name="user" data-validation="required" data-validation-error-msg="The field user is required">
    </div>
    <div>
        <label for="birthday">Birthday</label>
        <input id="birthday" name="birthday" data-validation="date" data-validation-format="dd/mm/yyyy" data-validation-error-msg="Data inválida" data-mask="00/00/0000">
    </div>
    <div>
        <label for="free">Free</label>
        <input id="free" name="free">
    </div>
    <div>
        <button type="submit">OK</button>
    </div>
</form>
<script src="js/jquery.min.js"></script>
<script src="js/form-validator/jquery.form-validator.min.js"></script>
<script>
    $.validate({
        form: '#myTestForm',
        validateOnEvent: false,
        validateOnBlur: false,
        showHelpOnFocus: false,
        addSuggestions: false,

        // One way
        errorMessagePosition: 'custom',
        errorMessageCustom: function(form, title, errorMessages, conf) {
            console.log(errorMessages);
        },
        // end One way

        // Another way
        errorMessagePosition: 'top',
        errorMessageTemplate : {
            container: '<div class="{errorMessageClass} alert alert-danger">{messages}</div>',
            messages: '<h3>{errorTitle}</h3><ul>{fields}</ul>',
            field: '<li>{msg}</li>'
        },
        // end One way

        onError: function(f) {
            console.log(f, 'onError - Validation failed');
        },
        onSuccess: function(f) {
            console.log(f, 'onSuccess - The form is valid!');
        },

        // Callback Validate by field
        onElementValidate: function(isValid, field, validation) {
            if(validation !== null)
                console.log(isValid, field, validation);
            else
                console.log(field, 'without validation');
        }
    });

    $('#myTestForm').on('submit', function(ev){
        ev.preventDefault();
        console.log('submit!!');
    });
</script>

@victorjonsson
Copy link
Owner

That's a great solution. I'll add you to the contributers list.

victorjonsson added a commit that referenced this pull request Mar 21, 2014
Added callback onElementValidate and possibility to customize errorMessa...
@victorjonsson victorjonsson merged commit 2f392ea into victorjonsson:master Mar 21, 2014
@LinzardMac
Copy link

Is there any special way to use this above code? I have it added to my script and it is still not using the custom markup for the error message.

        form : '#well-wishes',
        addSuggestions: false,
        showHelpOnFocus: false,
        errorMessagePosition: 'custom',
        validateOnBlur: true,
        errorMessageTemplate : {
            container: '<div class="{errorMessageClass} alert alert-danger">{messages}</div>',
            messages: '<h3>{errorTitle}</h3><ul>{fields}</ul>',
            field: '<li>{msg}</li>'
        },
      modules: 'security'
    });```

@victorjonsson
Copy link
Owner

This feature is removed. See http://www.formvalidator.net/#configuration_position

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants