|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Spookyfish Smarty Rookie
Joined: 18 Mar 2005 Posts: 8
|
Posted: Fri Mar 18, 2005 12:30 pm Post subject: SmartyValidate: smarty_validate_criteria_isDateOnOrAfter() |
|
|
I've created a small change to smarty_validate_criteria_isDateOnOrAfter() to allow for "+3 months" validation on dates:
Code: |
$_date2 = array_key_exists ($params['field2'], $formvars) ? strtotime($formvars[$params['field2']]) : strtotime($params['field2']);
|
This allows for:
Code: |
{validate field="mydate" criteria="isDateOnOrAfter" field2="+3 months" transform="makeDate" assign="mydate_error" message="Date not three months from now"}
|
Can we merge this into the next release?
Full code for smarty_validate_criteria_isDateOnOrAfter() below:
Code: |
function smarty_validate_criteria_isDateOnOrAfter($value, $empty, &$params, &$formvars) {
if(strlen($value) == 0)
return $empty;
if(!isset($params['field2'])) {
trigger_error("SmartyValidate: [isDateAfter] parameter 'field2' is missing.");
return false;
}
$_date1 = strtotime($value);
$_date2 = array_key_exists ($params['field2'], $formvars) ? strtotime($formvars[$params['field2']]) : strtotime($params['field2']);
if($_date1 == -1) {
trigger_error("SmartyValidate: [isDateAfter] parameter 'field' is not a valid date.");
return false;
}
if($_date2 == -1) {
trigger_error("SmartyValidate: [isDateAfter] parameter 'field2' is not a valid date.");
return false;
}
return $_date1 >= $_date2;
}
|
_________________ Spookyfish |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Mar 18, 2005 2:18 pm Post subject: |
|
|
The field2 is a field name, not a time value. Its not a good idea to mix them. You could add an alternate value like "date":
{validate field="mydate" criteria="isDateOnOrAfter" date="+3 months" ...}
Or write a separate validator with your wanted behavior. |
|
Back to top |
|
Spookyfish Smarty Rookie
Joined: 18 Mar 2005 Posts: 8
|
Posted: Fri Mar 18, 2005 3:54 pm Post subject: |
|
|
mohrt wrote: | The field2 is a field name, not a time value. Its not a good idea to mix them. You could add an alternate value like "date":
{validate field="mydate" criteria="isDateOnOrAfter" date="+3 months" ...}
Or write a separate validator with your wanted behavior. |
Ok, I agree on the difference between a field name and a time value. A seperate check seems superfluous. I've changed my modification to this:
Code: |
function smarty_validate_criteria_isDateOnOrAfter($value, $empty, &$params, &$formvars) {
if(strlen($value) == 0)
return $empty;
if(!(isset($params['field2'])) ^ (isset($params['date']))) {
trigger_error("SmartyValidate: [isDateOnOrAfter] parameter 'field2' or 'date' is missing or are both set");
return false;
}
$_date1 = strtotime($value);
$_date2 = isset($params['date']) ? strtotime($params['date']) : strtotime($formvars[$params['field2']]);
if($_date1 == -1) {
trigger_error("SmartyValidate: [isDateOnOrAfter] parameter 'field' is not a valid date");
return false;
}
if($_date2 == -1) {
trigger_error("SmartyValidate: [isDateOnOrAfter] parameter 'field2' or 'date' is not a valid date");
return false;
}
return $_date1 >= $_date2;
}
|
Does this qualify for inclusion? _________________ Spookyfish |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Mar 18, 2005 4:35 pm Post subject: |
|
|
Why not do this:
Code: | {validate name="startDate" criteria="isOnOrAfter" field2="endDate" message="end date must be on or after start date"}
<input type="text" name="startDate">
<input type="hidden" name="endDate" value="+3 months">
|
My only reservation with the "date" field is handling the criteria from the PHP side (the new way in CVS):
[php:1:6b12bd479d]SmartyValdiate::register_validator('v_date','endDate','isDateOnOrAfter:startDate');[/php:1:6b12bd479d]
Code: | {validate id="v_date" message="end date must be on or after start date"}
<input type="text" name="startDate">
<input type="text" name="endDate">
|
Here, the startDate field to is equivalent to "field2". If you have a date field instead of field2, then there is an ambiguity. You can still put the date in the field:
Code: | {validate id="v_date" message="end date must be on or after start date"}
<input type="text" name="startDate">
<input type="hidden" name="endDate" value="+3 months">
|
|
|
Back to top |
|
Spookyfish Smarty Rookie
Joined: 18 Mar 2005 Posts: 8
|
Posted: Fri Mar 18, 2005 4:46 pm Post subject: |
|
|
mohrt wrote: | Why not do this:
Code: | {validate name="startDate" criteria="isOnOrAfter" field2="endDate" message="end date must be on or after start date"}
<input type="text" name="startDate">
<input type="hidden" name="endDate" value="+3 months">
|
|
This makes the check almost client side. The precondition can be changed by sending a different endDate field back to the server on submitting a form.
While not a huge security risk in this scenario, I'd still like to keep it entiry server side.
I'm not yet familiar with the new method that is in CVS. Where can I access the CVS repository and are there new docs available? _________________ Spookyfish |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
|
|
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
|