javascript - Error management in grails -
i quite new grails , in application need check dates. in former java program have used 2 javascript functions different detail granularity. both accept dates 1970-01-01 2099-12-31. 1 demands correct date , (optionally) time , tells user he/she made erroneous entry:
function okdate1(dtstr) { var ok = true; // first trim off leading , trailing white space var trimpattern = /(?:\b(.*)\b)/; dtstr = (dtstr.match(trimpattern))[1]; // verify input within range , correct var pat = /^((?:19[7-9][0-9])|(?:20[0-9][0-9]))-((?:(?:0)?[1-9])|(?:1[0-2]))-((?:(?:0)?[1-9])|(?:[1-2][0-9])|(?:3[01]))(?: ((?:(?:0|1)[0-9])|(?:2[0-3])):([0-5][0-9]))?$/; var dtm = dtstr.match(pat); if (!dtm) { ok = false; } else { // verify day in in range given month var days = array(31,28,31,30,31,30,31,31,30,31,30,31); // compensate leap year if ((((dtm[1] % 4) === 0) && !((dtm[1] % 100) === 0)) || ((dtm[1] % 400) === 0)) { days[1] = 29; } if (dtm[3] > days[dtm[2] - 1]) ok = false; } if (!ok) alert("enter date , (optionally) time on form 'yyyy-mm-dd hh:mm'"); return ok; }
and other checks went wrong accepting wider range on numeric parts of input string:
function okdate2(dtstr) { // first trim off leading , trailing white space var trimpattern = /(?:\b(.*)\b)/; dtstr = (dtstr.match(trimpattern))[1]; // if nothing skip rest if (!dtstr) return datetimealert(0); // pattern recognize 'dddd-dd-dd[ dd:dd]' pattern var pat = /^(?:(\d{4})-(\d{1,2})-(\d{1,2}))(?: (\d{1,2}):(\d{2}))?$/; var dtm = dtstr.match(pat); // if not follow pattern: out if (!dtm) return datetimealert(0); // convert each group number // if no time notation corresponding groups become nan (var = 1; < dtm.length; i++) { dtm[i] = number(dtm[i]); } // check correct year interval if (dtm[1] < 1970 || dtm[1] > 2099) return datetimealert(1); // check correct month notation if (dtm[2] < 1 || dtm[2] > 12) return datetimealert(2); // array correct numer of days each month var mdays = array(31,28,31,30,31,30,31,31,30,31,30,31); // compensate leap year if ((((dtm[1] % 4) === 0) && !((dtm[1] % 100) === 0)) || ((dtm[1] % 400) === 0)) { mdays[1] = 29; } // check day given month if (dtm[3] < 1 || mdays[dtm[2] - 1] < dtm[3]) return datetimealert(3); // if date given , no time, ok if (isnan(dtm[4]) && isnan(dtm[5])) return true; // can not happen according pattern, ... if (isnan(dtm[4]) || isnan(dtm[5])) return datetimealert(4); // check given hour if (dtm[4] > 23) return datetimealert(5); // check given minutes if (dtm[5] > 59) return datetimealert(6); // if no error return true; }
where function datetimealert puts out alert (hopefully) error message , returns false. 'trimpattern' in both function strip leading , trailing whitespace.
i used them in forms made calls them in "onsubmit" function. objective here not discuss 2 functions comments on them are, of course, welcome.
in grails application use jquery datepicker extended trent richardsons jquery timepicker addon, text string result. call datetimepicker in form:
<form ... <dl ... <dt>start date <span class="required-indicator">*</span></dt> <dd> <div class="fieldcontain ${haserrors(bean: todoinstance, field: 'startdate', 'error')} required"> <g:textfield name="startdate" id="datepicker" class="datepicker" value="${formatdate(format:'yyyy-mm-dd hh:mm',date:todoinstance?.startad)}" required="" /> </div> </dd> ...
for other 'required' fields little 'tooltip'-like message telling me enter value field.
now, want use 2 datetime javascript in grails application don't want alert boxes popping up, want use them in static-constraints section in domain classes , messages in same manner other fields. how integrate them error management system grails?
this error messages provided validation api. inplement own customized validations can use constraint validator.
i'm assuming declared field java.util.date
in domain class, need date object in validator. default, grails handle dates g:datepicker
, split date in day, month , year fields.
to bind single string format date object, can register custom date property editor, this example.
the grails validation api server side validation. in case 1 option jquery validation ui plugin, provides client side validation through jquery. plugin supports standard constraints , can create own validations (like date validations), checkout extensibility docs session.
Comments
Post a Comment