Special field name
Validating field with special name
When using FormValidation with some server-side frameworks such as
CakePHP,
Symfony,
Rails,
Spring, etc, there is a relationship between the model name and the
name
attribute of the input.
For example, assuming that a user is modeled by the User
class with firstName
, lastName
, username
, emails[]
properties.
Following the naming convention defined by these frameworks, the inputs in the form of adding a new user might have the name attribute such as:
<input type="text" name="user[firstName]" />
<input type="text" name="user[lastName]" />
<input type="text" name="user[emails][]" />
<input type="text" name="user.lastName" />
<input type="text" name="user.firstName" />
<input type="text" name="user.emails[]" />
When using FormValidation to validate those kinds of fields, you must wrap the field name between single or double quotes as follows:
FormValidation.formValidation(
form,
{
fields: {
'user[firstName]': {
validators: {
...
}
},
'user[lastName]': {
validators: {
...
}
},
'user[emails][]': {
validators: {
...
}
},
},
}
);
Below is a sample example: