Skip to content

Conversation

@collingo
Copy link
Contributor

@collingo collingo commented Apr 4, 2012

Hi,

I added the HTML5 required attribute value to the formToArray function. I found this helpful when using the beforeSubmit callback for validation. The required property is defined once in the html and can then be handled with generic logic in the callback.

A simple example would be as follows...

beforeSubmit: function(arr, $form, options) {
    // The array of form data takes the following form:
    // [ { name: 'username', value: 'jresig', required: true }, { name: 'password', value: 'secret', required: true } ]
    // [ { name: 'title', value: '', required: false } ]

    $.each(arr, function(index, item) {
        if(item.required && !item.value.length) {
            console.log(item.name+" is required");
        }
    });
}

It currently only applies to elements of type input (see changes in commit) but this might need to be extended or limited depending on the W3C spec.

Nick

malsup added a commit that referenced this pull request Apr 6, 2012
@malsup malsup merged commit b15b338 into jquery-form:master Apr 6, 2012
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.

2 participants