Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#6 display field errors

  • Loading branch information...
commit fb0dff69dc738ef9dd740aa2ef473bd2768f74e8 1 parent 1de77f0
Rob Fletcher authored
View
5 .gitignore
@@ -18,4 +18,7 @@ target
/test/apps/grails-ng/grails-app/views/*/edit.gsp
/test/apps/grails-ng/grails-app/views/*/list.gsp
/test/apps/grails-ng/grails-app/views/*/show.gsp
-*Tests.groovy
+*Tests.groovy
+
+# OSX stuff
+.DS_Store
View
4 src/templates/scaffolding/create.html
@@ -36,11 +36,11 @@
required = (cp ? !(cp.propertyType in [boolean, Boolean]) && !cp.nullable && (cp.propertyType != String || !cp.blank) : false)
}
if (display) { %>
- <div class="control-group">
+ <div class="control-group" data-ng-class="{error: errors.${prefix}${p.name}}">
<label class="control-label" for="${prefix}${p.name}">${p.naturalName}</label>
<div class="controls">
<input type="text" id="${prefix}${p.name}" name="${prefix}${p.name}" required data-ng-model="item.${prefix}${p.name}">
- <span class="help-inline">{{errors.${prefix}${p.name}}}</span>
+ <span class="help-inline" data-ng-show="errors.${prefix}${p.name}">{{errors.${prefix}${p.name}}}</span>
</div>
</div>
<% } } %>
View
4 test/apps/grails-ng/src/templates/scaffolding/create.html
@@ -36,11 +36,11 @@
required = (cp ? !(cp.propertyType in [boolean, Boolean]) && !cp.nullable && (cp.propertyType != String || !cp.blank) : false)
}
if (display) { %>
- <div class="control-group">
+ <div class="control-group" data-ng-class="{error: errors.${prefix}${p.name}}">
<label class="control-label" for="${prefix}${p.name}">${p.naturalName}</label>
<div class="controls">
<input type="text" id="${prefix}${p.name}" name="${prefix}${p.name}" required data-ng-model="item.${prefix}${p.name}">
- <span class="help-inline">{{errors.${prefix}${p.name}}}</span>
+ <span class="help-inline" data-ng-show="errors.${prefix}${p.name}">{{errors.${prefix}${p.name}}}</span>
</div>
</div>
<% } } %>
View
15 test/apps/grails-ng/test/casper/includes/casper-angular.coffee
@@ -13,3 +13,18 @@ casper.getColumn = (index) ->
Array:: map.call cells, (e) -> $(e).text()
,
index: index
+
+# disables in-browser form validation so that tests can verify server-side validation
+casper.disableFormValidation = (selector = 'form') ->
+ @evaluate (selector) ->
+ $(selector).attr('novalidate', '').find('button').prop('disabled', false)
+ ,
+ selector: selector
+
+# asserts a selector is visible
+casper.test.assertVisible = (selector, message = null) ->
+ @assert casper.visible(selector), message
+
+# asserts a selector is not visible
+casper.test.assertNotVisible = (selector, message = null) ->
+ @assertNot casper.visible(selector), message
View
31 test/apps/grails-ng/test/casper/specs/field-errors-spec.coffee
@@ -0,0 +1,31 @@
+casper.start 'http://localhost:8080/album#/create', ->
+ @disableFormValidation()
+ @test.info 'when an empty form is submitted'
+ @click '.btn-primary'
+
+casper.then ->
+ @waitForSelector '#artist + .help-inline', ->
+ for field in ['artist', 'title']
+ @test.assertExists ".error ##{field}", "#{field} has error class"
+ @test.assertEquals @fetchText("##{field} + .help-inline"), "Property [#{field}] of class [class grails.plugin.angular.test.Album] cannot be null", "error message is displayed for #{field}"
+ @test.assertVisible "##{field} + .help-inline", "error message for #{field} is visible"
+
+ @test.info 'when a partially completed form is submitted'
+ @fill 'form',
+ artist: 'Yeasayer'
+ @click '.btn-primary'
+ , ->
+ fail 'submit did not complete'
+
+casper.then ->
+ @waitForSelector '#artist + .help-inline', ->
+ @test.assertNotExists ".error #artist", "artist does not have error class"
+ @test.assertEquals @fetchText("#artist + .help-inline"), '', "error message is not displayed for artist"
+ @test.assertNotVisible "#artist + .help-inline", "error message for artist is not visible"
+
+ @test.assertExists ".error #title", "title has error class"
+ @test.assertEquals @fetchText("#title + .help-inline"), "Property [title] of class [class grails.plugin.angular.test.Album] cannot be null", "error message is displayed for title"
+ @test.assertVisible "#title + .help-inline", "error message for title is visible"
+
+casper.run ->
+ @test.done()
View
8 test/apps/grails-ng/web-app/ng-templates/album/create.html
@@ -4,19 +4,19 @@
</div>
<form name="form" data-ng-submit="save(item)" class="form-horizontal">
- <div class="control-group">
+ <div class="control-group" data-ng-class="{error: errors.artist}">
<label class="control-label" for="artist">Artist</label>
<div class="controls">
<input type="text" id="artist" name="artist" required data-ng-model="item.artist">
- <span class="help-inline">{{errors.artist}}</span>
+ <span class="help-inline" data-ng-show="errors.artist">{{errors.artist}}</span>
</div>
</div>
- <div class="control-group">
+ <div class="control-group" data-ng-class="{error: errors.title}">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input type="text" id="title" name="title" required data-ng-model="item.title">
- <span class="help-inline">{{errors.title}}</span>
+ <span class="help-inline" data-ng-show="errors.title">{{errors.title}}</span>
</div>
</div>
Please sign in to comment.
Something went wrong with that request. Please try again.