Permalink
Browse files

Merge pull request #462 from arr-ee/fix_fields_for_name_conversion

Fixed handling of fields_for for has_one associations
  • Loading branch information...
bcardarella committed Jan 16, 2013
2 parents 1f07ef0 + d9dc45b commit 29a6e49aa3d62fbbfdd112a7a767483911e0dd78
@@ -35,7 +35,7 @@ $.fn.isValid = (validators) ->
validateElement(obj, validatorsFor(@[0].name, validators))
validatorsFor = (name, validators) ->
- name = name.replace(/_attributes\]\[\w+\]/g,"_attributes][]")
+ name = name.replace(/_attributes\]\[\w+\]\[(\w+)\]/g, "_attributes][][$1]")
validators[name] || {}
validateForm = (form, validators) ->
@@ -9,6 +9,7 @@ module('Validate Element', {
'user[password]':{"confirmation":[{"message": "must match confirmation"}]},
'user[agree]':{"acceptance": [{"message": "must be accepted"}]},
'user[email]':{"uniqueness":[{"message": "must be unique"}],"presence":[{"message": "must be present"}]},
+ 'user[info_attributes][eye_color]':{"presence":[{"message": "must be present"}]},
'user[phone_numbers_attributes][][number]':{"presence":[{"message": "must be present"}]}
}
}
@@ -74,6 +75,12 @@ module('Validate Element', {
name: 'user[phone_numbers_attributes][new_1234][number]',
id: 'user_phone_numbers_attributes_new_1234_number',
type: 'text'
+ }))
+ .append($('<label for="user_info_attributes_eye_color">Eye Color</label>'))
+ .append($('<input />', {
+ name: 'user[info_attributes][eye_color]',
+ id: 'user_info_attributes_eye_color',
+ type: 'text'
}));
$('form#new_user').validate();
@@ -121,8 +128,8 @@ test('Validate nested attributes', function() {
input = form.find('input#user_phone_numbers_attributes_0_number');
label = $('label[for="user_phone_numbers_attributes_0_number"]');
input.trigger('focusout');
- equal(input.parent().hasClass('field_with_errors'), false);
- equal(label.parent().hasClass('field_with_errors'), false);
+ ok(!input.parent().hasClass('field_with_errors'));
+ ok(!label.parent().hasClass('field_with_errors'));
input = form.find('input#user_phone_numbers_attributes_new_1234_number');
label = $('label[for="user_phone_numbers_attributes_new_1234_number"]');
@@ -131,6 +138,16 @@ test('Validate nested attributes', function() {
ok(label.parent().hasClass('field_with_errors'));
});
+test('Validate additional attributes', function() {
+ var form = $('form#new_user'), input, label;
+
+ input = form.find('input#user_info_attributes_eye_color');
+ label = $('label[for="user_info_attributes_eye_color"]');
+ input.trigger('focusout');
+ ok(input.parent().hasClass('field_with_errors'));
+ ok(label.parent().hasClass('field_with_errors'));
+});
+
test('Validate when keyup on confirmation', function() {
var form = $('form#new_user'), password = form.find('input#user_password'), confirmation = form.find('input#user_password_confirmation');
var label = $('label[for="user_password"]');
@@ -314,4 +331,3 @@ test('Validate when focusouting and field has disabled validations', function()
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
});
-

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 29a6e49

Please sign in to comment.