|
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 |
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Sat Jul 07, 2007 2:59 pm Post subject: [SmartyValidate] Custom validator bug? |
|
|
Why does this code not work? The validator "errCredentials" is always shown, no matter how i define the custom function. Either via the plugin naming inside thesame PHP file, in a seperate file, or usting the register_criteria call. Since smartyvalidate doesn't complain, i know it has the function, but it just doesn't work. Is this a bug
Mohrt, if i am doing something wrong, please include a complete working example in the docs of a custom validator which passes 2 form fields, since that part of the docs is a bit vague....
Code: |
session_start();
require('Smarty.class.php');
require('SmartyValidate.class.php');
function smarty_validate_criteria_checkCredentials($value, $empty, &$params, &$formvars) {
if (($params['FullName'] == '') && ($params['Password'] == '')) {
return false;
} else {
return true;
}
}
$smarty =& new Smarty;
SmartyValidate::connect($smarty, (empty($_POST)));
if (empty($_POST)) {
SmartyValidate::register_validator('fuser', 'FullName', 'notEmpty', false, true);
SmartyValidate::register_validator('fpass', 'Password', 'notEmpty', false, true);
SmartyValidate::register_validator('errCredentials', 'FullName:Password', 'checkCredentials', false, true);
$smarty->display('form.tpl');
} else {
// validate after a POST
if (SmartyValidate::is_valid($_POST)) {
SmartyValidate::disconnect();
$smarty->display('success.tpl');
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('form.tpl');
}
}
----------------
form.tpl
----------------
<form method="POST" action="index.php">
{validate id="errCredentials" message="..."}
{validate id="fuser" message="Full Name cannot be empty"}
Full Name: <input type="text" name="FullName">
{validate id="fpass" message="Password cannot be empty"}
Password: <input type="text" name="Password">
<input type="submit">
</form>
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jul 11, 2007 10:00 pm Post subject: |
|
|
The fields names are passed as $params['field1'] and $params['field2'] in the validator, not the actual values. See the isEqual validator plugin source for an example:
Code: | function smarty_validate_criteria_isEqual($value, $empty, &$params, &$formvars) {
if(!isset($params['field2'])) {
trigger_error("SmartyValidate: [isEqual] parameter 'field2' is missing.");
return false;
}
if(strlen($value) == 0)
return $empty;
return $value == $formvars[$params['field2']];
}
|
|
|
Back to top |
|
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Thu Jul 12, 2007 6:08 pm Post subject: |
|
|
Mohrt, as promised, a demo page showing the error.
It now works, using the code below.
But.... if i change the validators to use $params instead of formvars, both fail. If i change them to formvars, both work. So, is there an error in passing $params?
PHP code:
Code: | <?php
session_start();
require('inc/Smarty.class.php');
require('inc/SmartyValidate.class.php');
function smarty_validate_criteria_checkOneField($value, $empty, &$params, &$formvars) {
return ($formvars['FullName'] === 'piet');
}
function smarty_validate_criteria_checkTwoFields($value, $empty, &$params, &$formvars) {
return (($formvars['FullName'] === 'piet') && ($formvars['LastName'] === 'snot'));
}
$smarty =& new Smarty;
if (empty($_POST)) {
SmartyValidate::connect($smarty, true);
SmartyValidate::register_validator('fname', 'FullName', 'notEmpty');
SmartyValidate::register_validator('fnameTwo', 'FullName', 'checkOneField');
SmartyValidate::register_validator('lname', 'LastName', 'notEmpty');
SmartyValidate::register_validator('lnameTwo', 'FullName:LastName', 'checkTwoFields');
$smarty->display('form.tpl');
} else {
SmartyValidate::connect($smarty);
if(SmartyValidate::is_valid($_POST)) {
SmartyValidate::disconnect();
echo 'succes!';
die();
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('form.tpl');
}
}
?> |
Template:
Code: | <form method="POST" action="index.php">
{validate id="fname" message="Full Name cannot be empty<br />"}
{validate id="fnameTwo" message="Full Name must be 'piet'<br />"}
Full Name: <input type="text" name="FullName"><br />
{validate id="lname" message="Last Name cannot be empty<br />"}
{validate id="lnameTwo" message="Full Name must be 'piet' and Last Name must be 'snot'<br />"}
Last Name: <input type="text" name="LastName"><br />
<input type="submit">
</form>
|
p.s. Both Smarty and SmartyValidate are under the inc dir. Smarty 2.6.18 and latest SmartyValidate. Smartyvalidate validators are in the plugins dir, except the custom ones off course.... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jul 12, 2007 7:49 pm Post subject: |
|
|
No, because as I stated, the $params only passes the names of the fields separated by colons, not the values. If you look at the isEqual example above, it should be clear.
Do something like:
Code: | // return false if either value is empty
return (empty($value) && empty($formvars[$params['field2']]))
|
|
|
Back to top |
|
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Fri Jul 13, 2007 7:49 am Post subject: |
|
|
Ah, i can see clearly now (the rain is gone) hehe
Did you perhaps also had some time to look at the email i sent you at your ohrt domain? It was about the fact that SmartyValidate currently processes the validators as they appear in the template, not in the order you register them, and if that could be changed.
Since i want a custom message to appear above my form, stating that the credentials are wrong. I only want this to appear however, if all other validators fail. Currently, since it is the first validator SmartyValidate finds, it is processed first. |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Fri Jul 13, 2007 8:23 am Post subject: |
|
|
I usually work around this by putting my {validate} calls all on top of my template and assigning their output to variables. In fact I hacked {validate} a little so it appends it's output to an array my header.tpl can process. _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Fri Jul 13, 2007 8:32 am Post subject: |
|
|
Hi Celeb, well that certainly helps me for now, but it would be more logical to process the validators, the way they are assigned. I mean, that way, you only have to worry about the order in the PHP file, and not in the template file (especially when you use the halt parameter).
Oh and another thing i now remember from the mail:
Further more, i don't know how the "halt" parameter works, but given this example; you have 2 differently named fields, of which one fails. Both fields have the halt parameter set to true. A third validation call is made with both fields passed e.g. "field1:field2" Does that validator fire, or is it skipped? |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Fri Jul 13, 2007 9:56 am Post subject: |
|
|
When a validator with halt set fails to validate it's field(s) the whole form is invalid and no more validator are processed. _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Fri Jul 13, 2007 11:48 am Post subject: |
|
|
Oke, nvrmind, now i am reading your reply, i realised that this was also one of my questions to mohrt, if he could add an option to only skip the "failed" fields, instead of the entire form.
For instance;
you have 4 fields on the form. To make it simple; each field has 3 validators.
You want all 4 fields to be validated, but only those validators that pass. This means that you could get 4 errors, if all fields fail. Instead of 12. Get it? |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Fri Jul 13, 2007 1:01 pm Post subject: |
|
|
Ok, I know what you want to achieve. I don't think SmartyValidate "knows" what field it is validating right now and if this field was (successfully) validated by another validator so far.
I've got two suggestion, how you could approach this:
Define a SmartyValidate form for each field you want to validate. So you can validate the form seperately.
Define your own custom validators, that check all the constraints of one field at once. Then you can put as much logic as you want in there. _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
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
|
|