From 73f265859db49431fcb8b4d21139708003b79950 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 14 Apr 2015 07:33:47 +0200 Subject: [PATCH] Improved date and datetime validation. --- .../controllers/_application_controller_form.js.coffee | 4 ++-- .../app/models/_application_model.js.coffee | 10 ++++++---- public/assets/tests/form-validation.js | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee index ec7e92baf..f03e69a16 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee @@ -2367,7 +2367,7 @@ class App.ControllerForm extends App.Controller throw "Invalid Date #{year}-#{format(month)}-#{format(day)}" param[ namespace[0] ] = "#{time.getUTCFullYear()}-#{format(time.getUTCMonth()+1)}-#{format(time.getUTCDate())}" catch err - param[ namespace[0] ] = null + param[ namespace[0] ] = 'invalid' console.log('ERR', err) else param[ namespace[0] ] = undefined @@ -2405,7 +2405,7 @@ class App.ControllerForm extends App.Controller time.setMinutes( time.getMinutes() + time.getTimezoneOffset() ) param[ namespace[0] ] = time.toISOString() catch err - param[ namespace[0] ] = null + param[ namespace[0] ] = 'invalid' console.log('ERR', err) else param[ namespace[0] ] = undefined diff --git a/app/assets/javascripts/app/models/_application_model.js.coffee b/app/assets/javascripts/app/models/_application_model.js.coffee index 1fe7cba83..22424127a 100644 --- a/app/assets/javascripts/app/models/_application_model.js.coffee +++ b/app/assets/javascripts/app/models/_application_model.js.coffee @@ -139,14 +139,16 @@ class App.Model extends Spine.Model errors["#{attributeName}_confirm"] = '' # check datetime - if attribute.tag is 'datetime' && data['params'][attributeName] is null - errors[attributeName] = 'invalid' + if attribute.tag is 'datetime' + if data['params'][attributeName] is 'invalid' + errors[attributeName] = 'invalid' # validate value # check date - if attribute.tag is 'date' && data['params'][attributeName] is null - errors[attributeName] = 'invalid' + if attribute.tag is 'date' + if data['params'][attributeName] is 'invalid' + errors[attributeName] = 'invalid' # validate value diff --git a/public/assets/tests/form-validation.js b/public/assets/tests/form-validation.js index 0dd77a272..ef2403093 100644 --- a/public/assets/tests/form-validation.js +++ b/public/assets/tests/form-validation.js @@ -243,14 +243,14 @@ test( "date validation check", function() { // check params params = App.ControllerForm.params( el ) test_params = { - date1: null, + date1: 'invalid', } deepEqual( params, test_params, 'params check' ) // check errors errors = form.validate(params) test_errors = { - date1: "invalid", + date1: 'invalid', } deepEqual( errors, test_errors, 'validation errors check' ) App.ControllerForm.validate( { errors: errors, form: el } )