Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge "Make error display on forms more flexible"

  • Loading branch information...
commit 3626d890f51b46e93315ddc5c56e2c525efa1a1c 2 parents 5a63fdf + 4ca95f4
@jawsthegame jawsthegame authored Gerrit Code Review committed
View
19 lib/forms.coffee
@@ -12,12 +12,15 @@ class FormView extends View
errorTemplate: ->
throw('Must override errorTemplate')
- constructor: (@model) ->
+ constructor: (@model, opts) ->
@deferred = new Deferred()
@events or= {}
@events['submit form'] = 'save'
- super
+ super(@model)
+
+ if opts?.fieldClass?
+ @fieldClass = opts.fieldClass
save: (e) ->
e?.preventDefault()
@@ -66,9 +69,15 @@ class FormView extends View
_showErrors: (errs) ->
for name, errors of errs
- @$el.find("[name=#{name}]").last()
- .after(@errorTemplate(errors: errors))
- .parent().addClass('error')
+ field = @$el.find("[name=#{name}]")
+ field.last().after(@errorTemplate(errors: errors))
+
+ parent = if @fieldClass
+ field.closest(".#{@fieldClass}") or field.parent()
+ else
+ field.parent()
+
+ parent.addClass('error')
_enableForm: ->
@$el.find('input').prop('disabled', false)
View
5 test/lib/form_view_spec_template.haml
@@ -27,3 +27,8 @@
%p
%label Older Than Andrew
%input{name: 'olderThanAndrew', type: 'checkbox'}
+
+ %p
+ .field.younger-than-mark
+ %label Younger Than Mark Years
+ %input{name: 'youngerThanMark', type: 'text'}
View
12 test/lib/forms_spec.coffee
@@ -20,6 +20,7 @@ class TestForm extends forms.FormView
someDateTime: forms.dateTimeField
gender: forms.stringField
olderThanAndrew: forms.boolField
+ youngerThanMark: forms.intField
describe 'Form View', ->
@@ -31,6 +32,7 @@ describe 'Form View', ->
age: 23
gender: 'male'
olderThanAndrew: true
+ youngerThanMark: 8
someDateTime: '2012-02-24 12:00 PM'
@lilBilly.url = '/lils/billy'
@@ -204,6 +206,16 @@ describe 'Form View', ->
expect(ageDiv.attr('class')).to.include 'error'
done()
+ it 'should add an error class when fieldClass provided', (done) ->
+ form = new TestForm(@lilBilly, fieldClass: 'field').render()
+ form.$el.find('[name=youngerThanMark]').val('Not a bool')
+ ytmField = form.$el.find('.field.younger-than-mark')
+ expect(ytmField).to.not.have.class 'error'
+
+ form.save().fail =>
+ expect(ytmField).to.have.class 'error'
+ done()
+
it 'should remove errors when the problem is fixed', (done) ->
test.when 'POST', '/lils/billy', (req) ->
body: JSON.stringify(age: 18)
Please sign in to comment.
Something went wrong with that request. Please try again.