Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

SmartyValidate: Help to contol Multiples Dates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
Isidor128
Smarty Regular


Joined: 27 Jul 2004
Posts: 35
Location: France

PostPosted: Thu Oct 14, 2004 1:59 pm    Post subject: SmartyValidate: Help to contol Multiples Dates Reply with quote

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 Confused
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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Oct 14, 2004 3:59 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Isidor128
Smarty Regular


Joined: 27 Jul 2004
Posts: 35
Location: France

PostPosted: Tue Oct 19, 2004 7:42 pm    Post subject: Reply with quote

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 Smile

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Oct 19, 2004 7:51 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Isidor128
Smarty Regular


Joined: 27 Jul 2004
Posts: 35
Location: France

PostPosted: Tue Oct 19, 2004 8:01 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Oct 20, 2004 1:27 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Oct 20, 2004 2:28 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Oct 20, 2004 3:16 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Oct 20, 2004 4:17 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP