@@ -271,7 +271,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
271271 return scope . form . validationMessage [ schemaError . code ] ||
272272 scope . form . validationMessage [ 'default' ] ;
273273 } else {
274- return scope . form . validationMessage . required ||
274+ return scope . form . validationMessage . number ||
275275 scope . form . validationMessage [ 'default' ] ||
276276 scope . form . validationMessage ;
277277 }
@@ -282,8 +282,8 @@ angular.module('schemaForm').provider('schemaFormDecorators',
282282 return schemaError . message ; //use tv4.js validation message
283283 }
284284
285- //Otherwise we only use required so it must be it.
286- return 'Required ' ;
285+ //Otherwise we only have input number not being a number
286+ return 'Not a number ' ;
287287
288288 } ;
289289 }
@@ -893,26 +893,26 @@ angular.module('schemaForm').factory('sfValidator', [function() {
893893 * @return a tv4js result object.
894894 */
895895 validator . validate = function ( form , value ) {
896-
896+ if ( ! form ) {
897+ return { valid : true } ;
898+ }
897899 var schema = form . schema ;
898900
899901 if ( ! schema ) {
900- //Nothings to Validate
901- return value ;
902+ return { valid : true } ;
902903 }
903904
904- //Type cast and validate against schema.
905- //Basic types of json schema sans array and object
906- if ( schema . type === 'integer' ) {
907- value = parseInt ( value , 10 ) ;
908- } else if ( schema . type === 'number' ) {
909- value = parseFloat ( value , 10 ) ;
910- } else if ( schema . type === 'boolean' && typeof value === 'string' ) {
911- if ( value === 'true' ) {
912- value = true ;
913- } else if ( value === 'false' ) {
914- value = false ;
915- }
905+ // Input of type text and textareas will give us a viewValue of ''
906+ // when empty, this is a valid value in a schema and does not count as something
907+ // that breaks validation of 'required'. But for our own sanity an empty field should
908+ // not validate if it's required.
909+ if ( value === '' ) {
910+ value = undefined ;
911+ }
912+
913+ // Numbers fields will give a null value, which also means empty field
914+ if ( form . type === 'number' && value === null ) {
915+ value = undefined ;
916916 }
917917
918918 // Version 4 of JSON Schema has the required property not on the
@@ -929,7 +929,6 @@ angular.module('schemaForm').factory('sfValidator', [function() {
929929 if ( angular . isDefined ( value ) ) {
930930 valueWrap [ propName ] = value ;
931931 }
932-
933932 return tv4 . validateResult ( valueWrap , wrap ) ;
934933
935934 } ;
@@ -1295,59 +1294,69 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', functio
12951294 return {
12961295 restrict : 'A' ,
12971296 scope : false ,
1297+ // We want the link function to be *after* the input directives link function so we get access
1298+ // the parsed value, ex. a number instead of a string
1299+ priority : 1000 ,
12981300 require : 'ngModel' ,
12991301 link : function ( scope , element , attrs , ngModel ) {
13001302 //Since we have scope false this is the same scope
13011303 //as the decorator
13021304 scope . ngModel = ngModel ;
13031305
13041306 var error = null ;
1305- var form = scope . $eval ( attrs . schemaValidate ) ;
1306- // Validate against the schema.
1307- var validate = function ( viewValue ) {
1307+
1308+ var getForm = function ( ) {
13081309 if ( ! form ) {
13091310 form = scope . $eval ( attrs . schemaValidate ) ;
13101311 }
1312+ return form ;
1313+ } ;
1314+ var form = getForm ( ) ;
13111315
1312- //Still might be undefined
1313- if ( ! form ) {
1314- return viewValue ;
1315- }
1316+ // Validate against the schema.
13161317
1317- // Is required is handled by ng-required?
1318- if ( angular . isDefined ( attrs . ngRequired ) && angular . isUndefined ( viewValue ) ) {
1319- return undefined ;
1320- }
1318+ // Get in last of the parses so the parsed value has the correct type.
1319+ if ( ngModel . $validators ) { // Angular 1.3
1320+ ngModel . $validators . schema = function ( value ) {
1321+ var result = sfValidator . validate ( getForm ( ) , value ) ;
1322+ error = result . error ;
1323+ return result . valid ;
1324+ } ;
1325+ } else {
13211326
1322- // An empty field gives us the an empty string, which JSON schema
1323- // happily accepts as a proper defined string, but an empty field
1324- // for the user should trigger "required". So we set it to undefined.
1325- if ( viewValue === '' ) {
1326- viewValue = undefined ;
1327- }
1327+ // Angular 1.2
1328+ ngModel . $parsers . push ( function ( viewValue ) {
1329+ form = getForm ( ) ;
1330+ //Still might be undefined
1331+ if ( ! form ) {
1332+ return viewValue ;
1333+ }
13281334
1329- var result = sfValidator . validate ( form , viewValue ) ;
1335+ var result = sfValidator . validate ( form , viewValue ) ;
13301336
1331- if ( result . valid ) {
1332- // it is valid
1333- ngModel . $setValidity ( 'schema' , true ) ;
1334- return viewValue ;
1335- } else {
1336- // it is invalid, return undefined (no model update)
1337- ngModel . $setValidity ( 'schema' , false ) ;
1338- error = result . error ;
1339- return undefined ;
1340- }
1341- } ;
1337+ if ( result . valid ) {
1338+ // it is valid
1339+ ngModel . $setValidity ( 'schema' , true ) ;
1340+ return viewValue ;
1341+ } else {
1342+ // it is invalid, return undefined (no model update)
1343+ ngModel . $setValidity ( 'schema' , false ) ;
1344+ error = result . error ;
1345+ return undefined ;
1346+ }
1347+ } ) ;
1348+ }
13421349
1343- // Unshift onto parsers of the ng-model.
1344- ngModel . $parsers . unshift ( validate ) ;
13451350
13461351 // Listen to an event so we can validate the input on request
13471352 scope . $on ( 'schemaFormValidate' , function ( ) {
13481353
1349- if ( ngModel . $commitViewValue ) {
1350- ngModel . $commitViewValue ( true ) ;
1354+ if ( ngModel . $validate ) {
1355+ ngModel . $validate ( ) ;
1356+ if ( ngModel . $invalid ) { // The field must be made dirty so the error message is displayed
1357+ ngModel . $dirty = true ;
1358+ ngModel . $pristine = false ;
1359+ }
13511360 } else {
13521361 ngModel . $setViewValue ( ngModel . $viewValue ) ;
13531362 }
0 commit comments