Hi Ed
As I guess ...
Since the value container of a date field/widget is a java.util.Date object
it not possible (or I do not know how to) to set the wrong entered date by
the user and re-display it.
To avoid that the bad entered and converted date value will be re-entered by
the user you could probably use a regular expression provided as follows:
<fd:validation>
<fd:javascript>
return checkFormInputDate(widget, "\.(19|20)[0-9]{2}$");
</fd:javascript>
</fd:validation>
Above example assumes that the date must be entered with dots (1.1.2000).
In the following I post you the code of my checkFormInputDate() javascript
function.
The code is able to handle a regular expression for further date input
checking.
function checkFormInputDate (widget, regExpValidation,
regExpValidationError)
{
var debug = false;
if (debug) {
java.lang.System.out.println("\n--- enter
newCheckFormInputDate() ---");
}
var enteredValue = new
String(cocoon.request.getParameter(widget.getRequestParameterName()));
var trimedValue = enteredValue.replace(/\s/g, "");
var widgetValue = widget.getValue();
if (debug) {
java.lang.System.out.println("widget is object of class[" +
widget.getClass() + "]");
java.lang.System.out.println("widget.getRequestParameterName()["
+ widget.getRequestParameterName() + "]");
java.lang.System.out.println("regExpValidation[" +
regExpValidation + "]");
java.lang.System.out.println("enteredValue[" + enteredValue +
"]");
java.lang.System.out.println("trimedValue[" + trimedValue +
"]");
java.lang.System.out.println("widgetValue[" + widgetValue +
"]");
if (widgetValue != null) {
java.lang.System.out.println("widgetValue is object of
class[" + widgetValue.getClass() + "]");
java.lang.System.out.println("widgetValue.getYear()[" +
widgetValue.getYear() + "]");
}
}
if (trimedValue.match(/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{2}$/)) {
var dateParts = trimedValue.split(/\./);
var year = parseInt(dateParts[2], 10);
if (debug) { java.lang.System.out.println("two digit year
entered[" + year + "]"); }
if (year >= 0 && year <= 29) {
widgetValue.setYear(year + 100);
if (debug) { java.lang.System.out.println("year reset to["
+ widgetValue.getYear() + "]"); }
} else {
widgetValue.setYear(year);
if (debug) { java.lang.System.out.println("year reset to["
+ widgetValue.getYear() + "]"); }
}
if (debug) { java.lang.System.out.println("widgetValue is now["
+ widgetValue + "]"); }
}
if (trimedValue != '') {
if (!isDate(trimedValue, "d.M.yy") && !isDate(trimedValue,
"d.M.yyyy")) {
var valError = new
org.apache.cocoon.forms.validation.ValidationError("invalidDate");
widget.setValidationError(valError);
if (debug) {
java.lang.System.out.println("widget.setValidationError() done with
[invalidDate]"); }
return false;
} else if (regExpValidation != null) {
var reg = new RegExp(regExpValidation, "");
if (!reg.test(trimedValue)) {
if (regExpValidationError != null) {
var valError = new
org.apache.cocoon.forms.validation.ValidationError(regExpValidationError);
} else {
var valError = new
org.apache.cocoon.forms.validation.ValidationError("invalidDate");
}
widget.setValidationError(valError);
if (debug) {
java.lang.System.out.println("widget.setValidationError() done because of
regExp test failed"); }
return false;
}
}
}
return true;
}
The code block
if (trimedValue.match(/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{2}$/)) {
...
}
is used to auto correct entered 2-digit years (01 - 99). 0-29 become to
2000-2029, 30-99 become to 1930 - 1999.
If you don't want to use this feature you can omit the whole code block.
If you aspect that date parts are separated with a slash (/) instead of a
dot (.) you have to change the reg exp on that line of code.
My function uses a function isDate() that is part of CalendarPopup.js in the
mattkruse-lib.
If don't have this lib you can add that code as follows:
// ------------------------------------------------------------------
// isDate ( date_string, format_string )
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(val,format) {
var date=getDateFromFormat(val,format);
if (date==0) { return false; }
return true;
}
Hope this helps ... Raffaele
-----Ursprüngliche Nachricht-----
Von: Edward Elhauge [mailto:[email protected]]
Gesendet: Donnerstag, 5. Februar 2009 04:50
An: [email protected]
Betreff: [?? Probable Spam] Re: AW: More control over Cform Date Conversion
Thanks,
A good trick.
Almost does what I want. Problem below.
Well spoken Merico Raffaele <[email protected]> wrote:
>Hi Edward
>
>To access the entered value in a CForm I use the following construct based
>on a personal server side javascript function.
>
><fd:field id=3D"form_bis" required=3D"false">
> <fd:datatype base=3D"date">
> ...=09
> </fd:datatype>
> <fd:validation>
> <fd:javascript>
> return checkFormInputDate(widget);
> </fd:javascript>
> </fd:validation>
></fd:field>
>
>
>function checkFormInputDate (widget, regExpValidation,
>regExpValidationError)
>{
>=09
> var enteredValue =3D new
>String(cocoon.request.getParameter(widget.getRequestParameterName()));
> ...
>}
Let's say you have a date like 0/0/0, you detect its bogousity in your
fd:validation
and display an error message. Meanwhile your date field just became
11/30/0002,
because the conversion went on and reinitializes the field. The user looks
casually
(or is fixing a bunch of other RED EXCLAMATION MARKS above) and resubmits
the form.
Now the date field is validated, but not what the user typed in the first
time,
and not what they would have fixed it to.
For this to work I need to keep the control of the field in the user's hands
and not
have the system make a wild guess.
Thanks for the trick though,
Ed Elhauge
>Raffaele
>
>-----Urspr=FCngliche Nachricht-----
>Von: Edward Elhauge [mailto:[email protected]]=20
>Gesendet: Mittwoch, 4. Februar 2009 02:05
>An: [email protected]
>Betreff: [?? Probable Spam] More control over Cform Date Conversion
>
>Hi,
>
>I've run into a problem where my users want more detailed control over
>Date field validation. In the version that we're running (2.1.8)
>we don't have a "lenient" attribute for base=3D"date" widgets.
>
>So the user can type in "33/60/9z" as a date, which get converted into
>"10/30/2011". Not what was expected.
>
>What I'd like would be to a small:
> <fd:validation> <fd:javascript>
> ...
> </fd:javascript> </fd:validation>
>section to throw an error in those cases.
>
>Unfortunately this.value inside that javascript callback is already in a
>Date format.
>
>My question is:
> How do I get access to the users original text input
> within the fd:validation section?
>
> If there is no way to get access to the raw text, do I need to write
> my own validator to replace "FormattingDateConvertor" ?
--
Edward Elhauge <[email protected]>
"The right dose differentiates a poison and a remedy." -- Paracelsus (1493 -
1541)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]