HenrikJoreteg / Happy.js

$('form').isHappy() – Lightweight, extensible form validation plugin for jQuery/Zepto.js
http://projects.joreteg.com/Happy.js/
MIT License
412 stars 123 forks source link

Overwrite validation error message #44

Closed karlclement closed 8 years ago

karlclement commented 10 years ago

I would like to overwrite the way the error message is handled. I want to use the id of the field to select it and them use the placeholder attribute to show the error message.

How do I overwrite the default validation error?

This is what I have:

$('#generator').isHappy({
        errorTemplate: function( el ) {
            if(el && typeof(el) !== 'undefined') {
                console.log( el );
            }
        },
        fields: {
          // but you could certainly do $('[name=name]') as well.
          '#first-sleeper-first-name': {
            required: true,
            message: 'Might we inquire your name'
          },
          '#first-sleeper-last-name': {
            required: true,
            message: 'How are we to reach you sans email??',
            test: happy.email // this can be *any* function that returns true or false
          }
        }
  });

I keep getting these error on validation error:

screen shot 2014-07-17 at 12 29 05 am

Any help?

wraithgar commented 10 years ago

errorTemplate is intended to return the html of the error message itself and is then insterted .after() the input element defined by the selector. It is given only one parameter, an error object w/ the id and message from the validator. There is currently no mechanism to insert the error as an attribute of the input defined by the selector.

Adding feature label to this since doing this (allowing the error message to go into a defined attribute of the selected element) would be a new feature.

foodgy commented 10 years ago

How do I overwrite the default validation error?

Same problem here. Need an example of errorTemplate usage.

wraithgar commented 10 years ago

All it does is take the error object and return the html you want inserted as your 'rendered' error.

function customErrorTemplate(error) {
    //error is {id: errorId, message: 'validation error message'}
    return $("<p class='something' id='" + error.id + ">" + error.message + "</p>");
}
btpoe commented 8 years ago

Custom error messages have been added as of v0.9.

To change the error message, return an instance of Error. For example:

$('#form').isHappy({
    fields: {
        '#birthday': {
            required: true,
            test: function() {
                if(isEmpty) {
                    return false;
                }
                if(isFuture) {
                    return new Error(‘Your birthday must have already happened.’);
                }
                if (isWednesday) {
                    return new Error(‘Your birthday cannot have happened on a Wednesday.’);
                }
                return true;
            },
            message: ‘Please provide a valid birth date.'
        }
    }
});