1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
module('Validate Element', {
setup: function() {
ClientSideValidations.forms['new_user'] = {
type: 'ActionView::Helpers::FormBuilder',
input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>',
validators: {
'user[name]':{"presence":[{"message": "must be present"}], "format":[{"message":"is invalid","with":/\d+/}]},
'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"}]},
'user[phone_numbers_attributes][country_code][][code]':{"presence":[{"message": "must be present"}]},
'user[phone_numbers_attributes][deeply][nested][][attribute]':{"presence":[{"message": "must be present"}]}
}
}
$('#qunit-fixture')
.append($('<form />', {
action: '/users',
'data-validate': true,
method: 'post',
id: 'new_user'
}))
.find('form')
.append($('<label for="user_name">Name</label>'))
.append($('<input />', {
name: 'user[name]',
id: 'user_name',
type: 'text'
}))
.append($('<label for="user_password">Password</label>'))
.append($('<input />', {
name: 'user[password]',
id: 'user_password',
type: 'password'
}))
.append($('<label for="user_password_confirmation">Password Confirmation</label>'))
.append($('<input />', {
name: 'user[password_confirmation]',
id: 'user_password_confirmation',
type: 'password'
}))
.append($('<label for="user_agree">Agree</label>'))
.append($('<input />', {
name: 'user[agree]',
id: 'user_agree',
type: 'checkbox',
value: 1
}))
.append($('<input />', {
name: 'user[email]',
id: 'user_email',
type: 'text'
}))
.append($('<label for="user_phone_numbers_attributes_0_number">Phone Number</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][0][number]',
id: 'user_phone_numbers_attributes_0_number',
type: 'text'
})
.append($('<input />', {
name: 'user[phone_numbers_attributes][0][_destroy]',
id: 'user_phone_numbers_attributes_0__destroy',
type: 'hidden',
value: "1"
})))
.append($('<label for="user_phone_numbers_attributes_1_number">Phone Number</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][1][number]',
id: 'user_phone_numbers_attributes_1_number',
type: 'text'
}))
.append($('<label for="user_phone_numbers_attributes_new_1234_number">Phone Number</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][new_1234][number]',
id: 'user_phone_numbers_attributes_new_1234_number',
type: 'text'
}))
.append($('<label for="user_phone_numbers_attributes_country_code_0_code">Country code</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][country_code][0][code]',
id: 'user_phone_numbers_attributes_country_code_0_code',
type: 'text'
}))
.append($('<label for="user_phone_numbers_attributes_deeply_nested_0_attribute">Deeply nested attribute</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][deeply][nested][0][attribute]',
id: 'user_phone_numbers_attributes_deeply_nested_0_attribute',
type: 'text'
}))
.append($('<label for="user_phone_numbers_attributes_deeply_nested_5154ce728c06dedad4000001_attribute">Deeply nested attribute</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][deeply][nested][5154ce728c06dedad4000001][attribute]',
id: 'user_phone_numbers_attributes_deeply_nested_5154ce728c06dedad4000001_attribute',
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();
},
teardown: function() {
$('#qunit-fixture').remove('form');
delete ClientSideValidations.forms.new_user;
}
});
test('Validate when focusouting', function() {
var form = $('form#new_user'), input = form.find('input#user_name');
var label = $('label[for="user_name"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
});
test('Validate when checkbox is clicked', function() {
var form = $('form#new_user'), input = form.find('input#user_agree');
var label = $('label[for="user_agree"]');
input.prop('checked', false)
input.trigger('click');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
});
test('Validate when focusout 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"]');
password.val('password');
confirmation.trigger('focusout');
ok(password.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
});
test('Validate nested attributes', function() {
var form = $('form#new_user'), input, label;
input = form.find('input#user_phone_numbers_attributes_1_number');
label = $('label[for="user_phone_numbers_attributes_1_number"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
input = form.find('input#user_phone_numbers_attributes_0_number');
label = $('label[for="user_phone_numbers_attributes_0_number"]');
input.trigger('focusout');
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"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
input = form.find('input#user_phone_numbers_attributes_country_code_0_code');
label = $('label[for="user_phone_numbers_attributes_country_code_0_code"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
input = form.find('input#user_phone_numbers_attributes_deeply_nested_0_attribute');
label = $('label[for="user_phone_numbers_attributes_deeply_nested_0_attribute"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
input = form.find('input#user_phone_numbers_attributes_deeply_nested_5154ce728c06dedad4000001_attribute');
label = $('label[for="user_phone_numbers_attributes_deeply_nested_5154ce728c06dedad4000001_attribute"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
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"]');
password.val('password');
confirmation.trigger('keyup');
ok(password.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
confirmation.val('password')
confirmation.trigger('keyup');
ok(!password.parent().hasClass('field_with_errors'));
ok(!label.parent().hasClass('field_with_errors'));
});
test('Forcing remote validators to run last', function() {
var form = $('form#new_user'), input = form.find('input#user_email');
input.trigger('focusout');
equal(input.parent().find('label').text(), "must be present")
});
test("Don't validate when value hasn't changed", function() {
var form = $('form#new_user'), input = form.find('input#user_name');
var label = $('label[for="user_name"]');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
input.val('123');
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
input.trigger('change');
input.trigger('focusout');
ok(!input.parent().hasClass('field_with_errors'));
ok(!label.parent().hasClass('field_with_errors'));
});
test('Validate when error message needs to change', function() {
var form = $('form#new_user'), input = form.find('input#user_name');
var label = $('label[for="user_name"]');
input.trigger('focusout');
equal(input.parent().find('label.message').text(), "must be present");
input.val('abc');
input.trigger('change')
input.trigger('focusout');
equal(input.parent().find('label.message').text(), "is invalid");
})
test("Don't validate confirmation when not a validatable input", function() {
$('#qunit-fixture')
.append($('<form />', {
action: '/users',
'data-validate': true,
method: 'post',
id: 'new_user_2'
}))
.find('form')
.append($('<label for="user_2_password">Password</label>'))
.append($('<input />', {
name: 'user_2[password]',
id: 'user_2_password',
type: 'password'
}))
.append($('<label for="user_2_password_confirmation">Password Confirmation</label>'))
.append($('<input />', {
name: 'user_2[password_confirmation]',
id: 'user_2_password_confirmation',
type: 'password'
}))
ClientSideValidations.forms['new_user_2'] = {
type: 'ActionView::Helpers::FormBuilder',
input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>',
validators: { }
}
$('form#new_user_2').validate();
var form = $('form#new_user_2'), input = form.find('input#user_2_password_confirmation');
input.val('123');
input.trigger('focusout');
ok(!input.parent().hasClass('field_with_errors'));
});
test("Don't validate disabled inputs", function() {
$('#qunit-fixture')
.append($('<form />', {
action: '/users',
'data-validate': true,
method: 'post',
id: 'new_user_2'
}))
.find('form')
.append($('<label for="user_2_name">name</label>'))
.append($('<input />', {
name: 'user_2[name]',
id: 'user_2_name',
type: 'name',
disabled: 'disabled'
}))
ClientSideValidations.forms['new_user_2'] = {
type: 'ActionView::Helpers::FormBuilder',
input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>',
validators: { 'user_2[name]':{"presence":{"message": "must be present"}}}
}
$('form#new_user_2').validate();
var form = $('form#new_user_2'), input = form.find('input#user_2_name');
input.val('');
input.trigger('focusout');
ok(!input.parent().hasClass('field_with_errors'));
});
test("Don't validate dynamically disabled inputs", function() {
$('#qunit-fixture')
.append($('<form />', {
action: '/users',
'data-validate': true,
method: 'post',
id: 'new_user_2'
}))
.find('form')
.append($('<label for="user_2_name">name</label>'))
.append($('<input />', {
name: 'user_2[name]',
id: 'user_2_name',
type: 'name',
}))
ClientSideValidations.forms['new_user_2'] = {
type: 'ActionView::Helpers::FormBuilder',
input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>',
validators: { 'user_2[name]':{"presence":{"message": "must be present"}}}
}
$('form#new_user_2').validate();
var form = $('form#new_user_2'), input = form.find('input#user_2_name');
input.attr('disabled', 'disabled');
input.val('');
input.trigger('focusout');
ok(!input.parent().hasClass('field_with_errors'));
});
test('ensure label is scoped to form', function() {
var form = $('form#new_user'), input = form.find('input#user_name');
var label = $('label[for="user_name"]');
$('#qunit-fixture')
.prepend($('<form />', { id: 'other_form', 'data-validate': true })
.append($('<label for="user_name">Name</label>')));
var otherLabel = $('form#other_form').find('label')
input.trigger('focusout');
ok(!otherLabel.parent().hasClass('field_with_errors'));
});
test("Return validation result", function() {
var input = $('#user_name');
ok(!input.isValid(ClientSideValidations.forms['new_user'].validators));
input.val('123').data('changed', true);
ok(input.isValid(ClientSideValidations.forms['new_user'].validators));
});
test('Validate when focusouting and field has disabled validations', function() {
var form = $('form#new_user'), input = form.find('input#user_name');
var label = $('label[for="user_name"]');
input.disableClientSideValidations();
input.trigger('focusout');
ok(!input.parent().hasClass('field_with_errors'));
ok(!label.parent().hasClass('field_with_errors'));
input.enableClientSideValidations();
input.trigger('focusout');
ok(input.parent().hasClass('field_with_errors'));
ok(label.parent().hasClass('field_with_errors'));
});
|