Milligram plugin

Integrate with Milligram framework. Support Milligram v1.3.0

Usage

The following piece of code is the starting point to validate the form made in Milligram:




    
    


    
...
document.addEventListener('DOMContentLoaded', function(e) { FormValidation.formValidation( document.getElementById('demoForm'), { fields: { ... }, plugins: { milligram: new FormValidation.plugins.Milligram(), ... }, } ); });

The sample code above assumes that the FormValidation files are placed inside the vendors directory. You might need to change the path depending on where you place them on the server.

The next sections list out some examples of various forms made with Milligram.

Horizontal form

Stacked form

You need to add the fv-stacked-form class to the form element such as:


...

In order to add the correct class for error message and the field element when it is a valid or invalid, we need to specify the CSS selector of the field container.

By default, the Milligram plugin will look for the .row element. But in the stacked form below, every field will be placed inside our own .fv-row container.

We need to use the rowSelector option to help the plugin determine the field containers as following:


milligram: new FormValidation.plugins.Milligram({
    rowSelector: function(field, ele) {
        // field is the field name
        // ele is the field element
        return '.fv-row';
    }

    // Or you can just simply pass it as a string:
    // rowSelector: '.fv-row',
}),

Multiple fields on the same row

In the following example, the firstName and lastName fields are placed inside .column-25 containers. Meanwhile, the city, state and zipcode fields can be found inside the .column-20 containers.

Again, we need to use the rowSelector option:


milligram: new FormValidation.plugins.Milligram({
    rowSelector: function(field, ele) {
        // field is the field name
        // ele is the field element
        switch (field) {
            case 'firstName':
            case 'lastName':
                return '.column-25';
            
            case 'city':
            case 'state':
            case 'zipcode':
                return '.column-20';
            
            default:
                return '.row';
        }
    }
}),

Changelog

  • v1.2.0: First release. It means that the Milligram plugin requires FormValidation v1.2.0 or newer.