jQuery плагин formValidation не читает имена безопасных форм рельсов
Я использую плагин formValidation для проверки моей формы Rails 4. В соответствии с этим вопросом мне пришлось изменить поле "names" с "name="firstname"на" name="user[firstname]", чтобы формы были отправлены правильно. Однако плагин formValidation использует "имя" поля, а не "идентификатор" поля для запуска кода проверки. Я могу заставить форму formValidation работать, если имя поля "firstname", но НЕ, если имя "user [firstname]". Есть ли способ написать значение "имя" в Javascript (новичок здесь), используя скобки?
Вот моя форма. я пробовал
<%= form_for(resource, :as => resource_name, id: 'new_user', :url => registration_path(resource_name)) do |f| %>
<% if resource.errors.any? %>
<%= devise_error_messages! %>
<% end %>
<%= f.text_field :firstname, :name=>'user[firstname]', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'FIRST NAME' %>
<%= f.text_field :lastname, :name =>'user[lastname]', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'LAST NAME ' %>
<%= f.email_field :email, :name=>'user[email]', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'YOUR EMAIL', :style=>"width:100%" %>
<%= f.password_field :password, :class => 'form-control ', :name=>'user[password]', :autofocus => true, :required => true, :placeholder => 'YOUR PASSWORD' %>
<%= f.password_field :password_confirmation, :name=>'user[password_confirmation]', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'CONFIRM YOUR PASSWORD' %>
<%= f.submit 'Create Account', :class => 'btn btn-aqua btn-lg btn-block',
:style => 'margin-bottom:5px' %>
<%end%>
<script>
$(document).ready(function() {
$('#new_user').formValidation({
framework: 'bootstrap',
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
user[firstname]: {
validators: {
notEmpty: {
message: 'Please enter your first name'
}
}
},
user[lastname]: {
validators: {
notEmpty: {
message: 'Please enter your last name'
}
}
},
user[email]: {
validators: {
notEmpty: {
message: 'The email address is required'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
user[password]: {
validators: {
notEmpty: {
message: 'The password is required'
},
}
},
user[password_confirmation]: {
validators: {
notEmpty: {
message: 'Please confirm your password'
},
identical: {
field: 'user[password]',
message: 'The passwords do not match'
}
}
}
button: {
// The submit buttons selector
selector: '[type="submit"]:not([formnovalidate])',
// The disabled class
disabled: 'disabled'
}
}
});
});
</script>
2 ответа
Согласно документации для вашего плагина валидатора,
Если имя поля содержит специальные символы, такие как., [, ], Вы должны заключить его в одинарные или двойные кавычки. См. Поле "Проверка" со специальным именем.
Так что просто поместите ваши имена полей в кавычки ""
Мой исходный код не использовал правильный формат для именования "имен"... но я не смог отладить это, так как я также пропустил случайную запятую прямо над "кнопкой". Я выкладываю рабочий код:
<script>
$(document).ready(function() {
$('#new_user').formValidation({
framework: 'bootstrap',
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
'user[firstname]': {
validators: {
notEmpty: {
message: 'Please enter your first name'
}
}
},
'user[lastname]': {
validators: {
notEmpty: {
message: 'Please enter your last name'
}
}
},
'user[email]': {
validators: {
notEmpty: {
message: 'The email address is required'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
'user[password]': {
validators: {
notEmpty: {
message: 'The password is required'
},
}
},
'user[password_confirmation]': {
validators: {
notEmpty: {
message: 'Please confirm your password'
},
identical: {
field: 'user[password]',
message: 'The passwords do not match'
}
}
},
button: {
// The submit buttons selector
selector: '[type="submit"]:not([formnovalidate])',
// The disabled class
disabled: 'disabled'
}
}
});
});
</script>