Permalink
Browse files

Also allow controls class passed in for errors

  • Loading branch information...
1 parent f61709c commit d6d189429ecb036a47b299e37b36e76242a3cf94 @jawsthegame jawsthegame committed Feb 2, 2013
Showing with 23 additions and 5 deletions.
  1. +10 −1 lib/forms.coffee
  2. +1 −1 package.json
  3. +3 −1 test/lib/form_view_spec_template.haml
  4. +9 −2 test/lib/forms_spec.coffee
View
@@ -22,6 +22,9 @@ class FormView extends View
if opts?.fieldClass?
@fieldClass = opts.fieldClass
+ if opts?.controlsClass?
+ @controlsClass = opts.controlsClass
+
save: (e) ->
e?.preventDefault()
local = new Deferred
@@ -70,7 +73,13 @@ class FormView extends View
_showErrors: (errs) ->
for name, errors of errs
field = @$el.find("[name=#{name}]")
- field.last().after(@errorTemplate(errors: errors))
+
+ errorTemplate = @errorTemplate(errors: errors)
+
+ if @controlsClass
+ field.closest(".#{@controlsClass}").append(errorTemplate)
+ else
+ field.last().after(errorTemplate)
parent = if @fieldClass
field.closest(".#{@fieldClass}") or field.parent()
View
@@ -1,6 +1,6 @@
{
"name": "quips",
- "version": "0.0.38",
+ "version": "0.0.39",
"dependencies": {
"coffee-script": "~1.3.3",
"jqueryify2": "0.0.1",
@@ -31,4 +31,6 @@
%p
.field.younger-than-mark
%label Younger Than Mark Years
- %input{name: 'youngerThanMark', type: 'text'}
+ .controls
+ .other-container
+ %input{name: 'youngerThanMark', type: 'text'}
View
@@ -206,14 +206,21 @@ 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()
+ it 'should use opts when adding an error', (done) ->
+ form = new TestForm(@lilBilly,
+ fieldClass: 'field'
+ controlsClass: 'controls').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'
+ expect(ytmField.find('.controls')).to.not.have.element 'ul.errors'
form.save().fail =>
expect(ytmField).to.have.class 'error'
+ expect(ytmField.find('.controls')).to.have.element 'ul.errors'
+ expect(ytmField.find('.other-container'))
+ .to.not.have.element 'ul.errors'
done()
it 'should remove errors when the problem is fixed', (done) ->

0 comments on commit d6d1894

Please sign in to comment.