View previous topic :: View next topic |
Author |
Message |
Isidor128 Smarty Regular
Joined: 27 Jul 2004 Posts: 35 Location: France
|
Posted: Thu Oct 14, 2004 1:59 pm Post subject: SmartyValidate: Help to contol Multiples Dates |
|
|
Hi all
The examples are fine in the Doc but when I try to validate this basic scheme I've seen that I do not have the answer
Code: |
Start Date : XX/XX/XXXX
End Date : XX/XX/XXXX
Announce Date : XX/XX/XXXX
1 - Any date could be blanc
2 - If both defined End Date must be after Start Date
3 - If both defined Announce Date must be before End Date
|
It's not complex when Dates aren't blank but if one 'is' on test 2 or 3 I'm blocked
If anyone could help
Regards
Last edited by Isidor128 on Tue Oct 19, 2004 7:28 pm; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Oct 14, 2004 3:59 pm Post subject: |
|
|
I assume you are referring to SmartyValidate...
You may have to create your own modified version of isDateAfter that ignores empty values for the either date when empty is set to true. |
|
Back to top |
|
Isidor128 Smarty Regular
Joined: 27 Jul 2004 Posts: 35 Location: France
|
Posted: Tue Oct 19, 2004 7:42 pm Post subject: |
|
|
mohrt wrote: | I assume you are referring to SmartyValidate.... |
Yes Sorry
mohrt wrote: | You may have to create your own modified version of isDateAfter that ignores empty values for the either date when empty is set to true. |
I think I solved my probleme by implementing a "Default Value" Transform function
so with my script as per :
Code: |
StartDate: <input type="text" name="dtefrom" id="dtefrom" size="10" value="{$dtefrom}" align="right">
{validate field="dtefrom" transform="default,makeDate" default="00/00/0000" criteria="isGDate" message="wrong start date format" halt="yes"}
End Date: <input type="text" name="dteto" id="dteto" size="10" value="{$dteto}">
{validate field="dteto" transform="default,makeDate" default="31/12/2037" criteria="isGDate" message="wrong end date format" halt="yes"}
PublishDate: <input type="text" name="dteann" id="dteann" size="10" value="{$dteann}">
{validate field="dteann" transform="default,makeDate" default="00/00/0000" criteria="isGDate" message="wrong publish date format" halt="yes"}
|
It works because I defaut the start date 0 and the end date ( to the last year end of unix calendar, I know it will go till 2038/01/19 but I prefer last day of the previous year.... ) and the PublishDate to 0
So my conditions are valid
Of cours this Transform function need to be added
[php:1:1872b38305]<?php
/**
* transform function, assign a default value if empty
*
* @param string $value the value of the field being transformed
* @param array $params the parameters passed to the transform function
* @param array $formvars the form variables
*/
function smarty_validate_transform_default($value, $params, &$formvars) {
if ($value == "") {
if(!empty($params['default'])) {
return $params['default'];
} else {
return;
}
}
}
?>[/php:1:1872b38305]
Monte
It will be great if you could add the "default" function as a standard option without having to define a new Transform function
I think
Code: | {validate field="foo" default="toto".... |
will be a grate thing
Regards |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Oct 19, 2004 7:51 pm Post subject: |
|
|
Don't handle default values from SmartyValidate, either assign a default value or use the default modifier:
Code: | <input type="text" name="dtefrom" id="dtefrom" size="10" value="{$dtefrom|default:"00/00/0000"}" align="right"> |
|
|
Back to top |
|
Isidor128 Smarty Regular
Joined: 27 Jul 2004 Posts: 35 Location: France
|
Posted: Tue Oct 19, 2004 8:01 pm Post subject: |
|
|
mohrt wrote: | Don't handle default values from SmartyValidate, either assign a default value or use the default modifier:
Code: | <input type="text" name="dtefrom" id="dtefrom" size="10" value="{$dtefrom|default:"00/00/0000"}" align="right"> |
|
No thats not working, the default value assignation need to be done after the validation, not when displaying the form to the user.
Otherwhise if I display "2037/12/31" in the end date, and most of my users will blank that date and the tests won't work.
I think to have the feature of a default value assigned if the user leave blank a field is quite common.
Regards |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 20, 2004 1:27 pm Post subject: |
|
|
There would be a conflict with the empty parameter:
{validate ... empty=false default="foo"}
So if empty values are not allowed, does the validator fail or does the default value come into play? Since transforms are applied before anything else, maybe that is the best method. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 20, 2004 2:28 pm Post subject: |
|
|
I added the "default" transform function to the repository. I left it as a transform function so the user has full control of where it happens in the transform chain of events (before or after trim for example)
example:
{validate field="foo" criteria="isInt" default="0" transform="trim,default" ...}
Monte |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Oct 20, 2004 3:16 pm Post subject: |
|
|
IMHO, there shouldn't be a default transform. Its not the type of thing that the template should be specifying and it confuses/blurs the use of Smarty's existing default modifier. Default values should come from the datasource handler, not a validator. I think it is a bad practice because it removes a distinction that Smarty is trying to enforce, namely separation of display from function.
But that's just me |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 20, 2004 4:17 pm Post subject: |
|
|
From a purely semantic viewpoint I agree, although form validation and form presentation are two separate things. The default modifier supplied by Smarty would be used for form presentation, where as the default transform function of SmartyValidate would be a function of the form validation after the form is submitted.
SmartyValidate already blurs the line by putting validation criteria in the template, but purely from the usefulness and time saved in validation management, it has justified itself as an exception for my purposes and hopefully others too. |
|
Back to top |
|
|