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 5098c777e..95930afcc 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee @@ -396,6 +396,17 @@ class App.ControllerForm extends App.Controller errors.year = 'invalid' if !_.isEmpty(errors) + + # if field is required, if not do not show error + if year is '' && day is '' && month + if attribute.null + e.preventDefault() + e.stopPropagation() + return + else + item.closest('.form-group').find('.help-inline').text( 'is required' ) + + # show invalid options for key, value of errors item.closest('.form-group').addClass('has-error') item.closest('.form-group').find("[name=\"#{fieldPrefix}___#{key}\"]").addClass('has-error') @@ -556,6 +567,17 @@ class App.ControllerForm extends App.Controller errors.minute = 'invalid' if !_.isEmpty(errors) + + # if field is required, if not do not show error + if year is '' && day is '' && month is '' && hour is '' && minute is '' + if attribute.null + e.preventDefault() + e.stopPropagation() + return + else + item.closest('.form-group').find('.help-inline').text( 'is required' ) + + # show invalid options for key, value of errors item.closest('.form-group').addClass('has-error') item.closest('.form-group').find("[name=\"#{fieldPrefix}___#{key}\"]").addClass('has-error') @@ -2302,17 +2324,22 @@ class App.ControllerForm extends App.Controller month = param[ "#{dateKey}month" ] day = param[ "#{dateKey}day" ] timezone = (new Date()).getTimezoneOffset()/60 - if year && month && day + if year && month && day && day && !lookupForm.find('[data-name="' + namespace[0] + '"]').hasClass('is-hidden') format = (number) -> if parseInt(number) < 10 number = "0#{number}" number try time = new Date( Date.parse( "#{year}-#{format(month)}-#{format(day)}T00:00:00Z" ) ) + if time && time.toString() is 'Invalid Date' + throw "Invalid Date #{year}-#{format(month)}-#{format(day)}" time.setMinutes( time.getMinutes() + time.getTimezoneOffset() ) - param[ namespace[0] ] = time.toISOString() + param[ namespace[0] ] = "#{time.getFullYear()}-#{format(time.getMonth()+1)}-#{format(time.getDate())}" catch err + param[ namespace[0] ] = undefined console.log('ERR', err) + else + param[ namespace[0] ] = undefined #console.log('T', time, time.getHours(), time.getMinutes()) @@ -2333,17 +2360,22 @@ class App.ControllerForm extends App.Controller hour = param[ "#{datetimeKey}hour" ] minute = param[ "#{datetimeKey}minute" ] timezone = (new Date()).getTimezoneOffset()/60 - if year && month && day && hour && minute + if year && month && day && hour && minute && !lookupForm.find('[data-name="' + namespace[0] + '"]').hasClass('is-hidden') format = (number) -> if parseInt(number) < 10 number = "0#{number}" number try time = new Date( Date.parse( "#{year}-#{format(month)}-#{format(day)}T#{format(hour)}:#{format(minute)}:00Z" ) ) + if time && time.toString() is 'Invalid Date' + throw "Invalid Date #{year}-#{format(month)}-#{format(day)}T#{format(hour)}:#{format(minute)}:00Z" time.setMinutes( time.getMinutes() + time.getTimezoneOffset() ) param[ namespace[0] ] = time.toISOString() catch err + param[ namespace[0] ] = undefined console.log('ERR', err) + else + param[ namespace[0] ] = undefined #console.log('T', time, time.getHours(), time.getMinutes()) diff --git a/public/assets/tests/form-validation.js b/public/assets/tests/form-validation.js index 514c8dc3b..acdd55d7e 100644 --- a/public/assets/tests/form-validation.js +++ b/public/assets/tests/form-validation.js @@ -110,7 +110,13 @@ test( "datetime validation check", function() { params: defaults, }); + // check params params = App.ControllerForm.params( el ) + test_params = { + datetime1: undefined, + } + deepEqual( params, test_params, 'params check' ) + errors = form.validate(params) test_errors = { datetime1: "is required", @@ -129,7 +135,14 @@ test( "datetime validation check", function() { el.find('[name="{datetime}datetime1___hour"]').val('12') el.find('[name="{datetime}datetime1___minute"]').val('42') + // check params params = App.ControllerForm.params( el ) + test_params = { + datetime1: "2015-01-01T11:42:00.000Z", + } + deepEqual( params, test_params, 'params check' ) + + // check errors errors = form.validate(params) test_errors = undefined deepEqual( errors, test_errors, 'validation errors check' ) @@ -172,35 +185,14 @@ test( "date validation check", function() { }); params = App.ControllerForm.params( el ) - errors = form.validate(params) - test_errors = { - date1: "is required", + + // check params + params = App.ControllerForm.params( el ) + test_params = { + date1: undefined, } - deepEqual( errors, test_errors, 'validation errors check' ) - App.ControllerForm.validate( { errors: errors, form: el } ) + deepEqual( params, test_params, 'params check' ) - equal( el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), true, 'check date1 has-error') - equal( el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check date1 error message') - - - // set new values - el.find('[name="{date}date1___day"]').val('1') - el.find('[name="{date}date1___month"]').val('1') - el.find('[name="{date}date1___year"]').val('2015') - params = App.ControllerForm.params( el ) - errors = form.validate(params) - test_errors = undefined - deepEqual( errors, test_errors, 'validation errors check' ) - - App.ControllerForm.validate( { errors: errors, form: el } ) - - equal( el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), false, 'check date1 has-error') - equal( el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), '', 'check date1 error message') - - el.find('[name="{date}date1___day"]').val('47') - el.find('[name="{date}date1___month"]').val('1') - el.find('[name="{date}date1___year"]').val('2015') - params = App.ControllerForm.params( el ) errors = form.validate(params) test_errors = { date1: "is required", @@ -208,4 +200,49 @@ test( "date validation check", function() { deepEqual( errors, test_errors, 'validation errors check' ) App.ControllerForm.validate( { errors: errors, form: el } ) + equal( el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), true, 'check date1 has-error') + equal( el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), 'is required', 'check date1 error message') + + // set new values + el.find('[name="{date}date1___day"]').val('1') + el.find('[name="{date}date1___month"]').val('1') + el.find('[name="{date}date1___year"]').val('2015') + + // check params + params = App.ControllerForm.params( el ) + test_params = { + date1: "2015-01-01", + } + deepEqual( params, test_params, 'params check' ) + + // check errors + errors = form.validate(params) + test_errors = undefined + deepEqual( errors, test_errors, 'validation errors check' ) + App.ControllerForm.validate( { errors: errors, form: el } ) + equal( el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), false, 'check date1 has-error') + equal( el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), '', 'check date1 error message') + + // set invalid values + el.find('[name="{date}date1___day"]').val('47') + el.find('[name="{date}date1___month"]').val('1') + el.find('[name="{date}date1___year"]').val('2015') + + // check params + params = App.ControllerForm.params( el ) + test_params = { + date1: undefined, + } + deepEqual( params, test_params, 'params check' ) + + // check errors + errors = form.validate(params) + test_errors = { + date1: "is required", + } + deepEqual( errors, test_errors, 'validation errors check' ) + App.ControllerForm.validate( { errors: errors, form: el } ) + equal( el.find('[data-name="date1"]').closest('.form-group').hasClass('has-error'), true, 'check date1 has-error') + equal( el.find('[data-name="date1"]').closest('.form-group').find('.help-inline').text(), '', 'check date1 error message') + }); \ No newline at end of file diff --git a/public/assets/tests/form.js b/public/assets/tests/form.js index 270d35c2b..12c4acec0 100644 --- a/public/assets/tests/form.js +++ b/public/assets/tests/form.js @@ -36,7 +36,7 @@ test( "form elements check", function() { { name: 'richtext1', display: 'Richtext1', tag: 'richtext', limit: 100, null: true, upload: true, default: defaults['richtext1'] }, { name: 'richtext2', display: 'Richtext2', tag: 'richtext', limit: 100, null: true, upload: true, default: defaults['richtext2'] }, { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: true, default: defaults['datetime1'] }, - { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: true, default: defaults['datetime2'] }, + { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: false, default: defaults['datetime2'] }, ] }, autofocus: true @@ -119,8 +119,11 @@ test( "form params check", function() { richtext7: "