Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #33 - adds presence validation support for radio btns #58

Merged
merged 1 commit into from
May 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/assets/javascripts/judge.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,12 @@
judge.eachValidators = {
// ActiveModel::Validations::PresenceValidator
presence: function(options, messages) {
return closed(this.value.length ? [] : [messages.blank]);
if (this.type === 'radio') {
var is_selected = root.document.querySelectorAll('[name="'+this.name+'"]:checked').length;
return closed(is_selected ? [] : [messages.blank]);
} else {
return closed(this.value.length ? [] : [messages.blank]);
}
},

// ActiveModel::Validations::LengthValidator
Expand Down
19 changes: 19 additions & 0 deletions spec/javascripts/judge-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,25 @@ describe('judge', function() {
el.value = 'foo';
expect(validator({}, { blank: 'Must not be blank' })).toBeValid();
});
it('returns invalid Validation if radio has no selection', function() {
el.type = 'radio'
el.name = 'radio_group'
el.value = 'option1'
// eachValidators.presence for radio btns rely on querySelectorAll
// so we have to add the el to the body
document.body.appendChild(el);
expect(validator({}, { blank: 'Must not be blank' })).toBeInvalid();
});
it('returns valid Validation if radio has selection', function() {
el.type = 'radio'
el.name = 'radio_group'
el.value = 'option1'
el.checked = true;
// eachValidators.presence for radio btns rely on querySelectorAll
// so we have to add the el to the body
document.body.appendChild(el);
expect(validator({}, { blank: 'Must not be blank' })).toBeValid();
});
});

describe('length', function() {
Expand Down