I'm using bootstrap 3.2.0, bootstrapvalidator 0.5.2, and mvc 5.1. I'm no expert in MVC, so the solution may be obvious to a more experienced user.
The problem I'm having is the model emits input element names in the format "modelname.fieldname", so, a username input element generated with
@Html.TextBoxFor(model => Model.LoginViewModel.UserName, new { @class = "form-control logininput", @placeholder = "User Name", @tabindex = 0 })
will generate html that looks like this:
<input class="form-control logininput" data-val="true" data-val-required="The Username field is required." id="LoginViewModel_UserName" name="LoginViewModel.UserName" placeholder="User Name" tabindex="0" type="text" value="">
The problem I'm having is that the bootstrap validator wants to reference the element by name, and if a period in the name (LoginViewModel.UserName) is invalid:
$('#loginform').bootstrapValidator({
fields: {
LoginViewModel.UserName: {
message: 'The user name is not valid',
container: '#erruser',
validators: {
notEmpty: {
message: 'The user name is required'
}
}
}
}
});
If I specify the name on the html helper, bootstrap validator works great, but the model won't validate:
@Html.TextBoxFor(model => Model.LoginViewModel.UserName, new { @class = "form-control logininput", @placeholder = "User Name", @tabindex = 0, Name="UserName" })
I've also tried escaping and bracketing, to no avail:
LoginViewModel\\.Password: {
and
LoginViewModel['Password']: {
Is there a way to make the bootstrapvalidator accept element names with a period, or do I have to modify the model in some way to map "simple" element names, to the model field name?