|
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 |
conner_bw Smarty Rookie
Joined: 21 Dec 2007 Posts: 17
|
Posted: Fri May 09, 2008 10:15 pm Post subject: SmartyValidate - register_object() bug ? |
|
|
Greetings.
I have been struggling with SmartValidate all day and can't get register_object() or register_criteria(static::method) to work.
I keep getting: SmartyValidate: [is_valid] method 'someMethod' is not valid for object 'someObject'.
Below is a test case. Can someone provide me with pointers as to what is going on and why it would fail? I have spent hours trying to fix it myself to no avail.
Optionally, a way to call myFilter, *any way at all*, would be awesome.
Thank you for your time.
Code: |
--- testcase2.php ---
<?php
class testCase {
public $smarty;
function __construct() {
$this->smarty = new Smarty();
}
function myFilter($value, $empty, &$params, &$formvars) {
return true;
}
static function myStaticFilter($value, $empty, &$params, &$formvars) {
return true;
}
function go() {
if(empty($_POST)) {
SmartyValidate::connect($this->smarty, true);
//
// BUG: Doesn't work
//
SmartyValidate::register_object('myObj' , $this);
SmartyValidate::register_criteria('myFilter', 'myObj->myFilter');
//
// BUG: Also Doesn't work
//
SmartyValidate::register_criteria('myStaticFilter', 'self::myStaticFilter');
SmartyValidate::register_validator('fname','FullName','myFilter');
SmartyValidate::register_validator('fdate','Date','myStaticFilter');
$this->smarty->display('test.tpl');
} else {
SmartyValidate::connect($this->smarty);
// validate after a POST
if(SmartyValidate::is_valid($_POST)) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
echo 'Success!';
} else {
// error, redraw the form
$this->smarty->assign($_POST);
$this->smarty->display('test.tpl');
}
}
}
}
session_start();
require('Smarty.class.php');
require('SmartyValidate.class.php');
$testcase = new testCase();
$testcase->go();
?>
--- test.tpl ---
<form method="POST" action="testcase2.php">
{validate id="fname" message="[ Full Name cannot be empty ]"}
Full Name: <input type="text" name="FullName">
<p />
{validate id="fdate" message="[ Date is not valid ]"}
Date: <input type="text" name="Date">
<p />
<input type="submit">
</form>
|
|
|
Back to top |
|
conner_bw Smarty Rookie
Joined: 21 Dec 2007 Posts: 17
|
Posted: Sat May 10, 2008 4:01 pm Post subject: Eureka |
|
|
Ok, I figured it out.
It works if I register the object before connecting. In the aforementioned, moving SmartyValidate::register_object('myObj' , $this); into the constructor solved my problem.
|
|
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
|