Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

SmartyValidate - register_object() bug ?

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
conner_bw
Smarty Rookie


Joined: 21 Dec 2007
Posts: 17

PostPosted: Fri May 09, 2008 10:15 pm    Post subject: SmartyValidate - register_object() bug ? Reply with quote

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. Sad

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


Joined: 21 Dec 2007
Posts: 17

PostPosted: Sat May 10, 2008 4:01 pm    Post subject: Eureka Reply with quote

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.

Confused
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    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