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: a simple yet powerful form validation plugin
Goto page Previous  1, 2, 3, 4 ... 16, 17, 18  Next
 
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
xces
Smarty Regular


Joined: 09 Apr 2004
Posts: 77

PostPosted: Wed Jun 02, 2004 7:34 am    Post subject: Reply with quote

mohrt wrote:
SmartyValidate isn't tightly coupled with the Smarty object at the time is_valid() is called, so if you want a template variable for this, you'll have to assign one yourself after is_valid() fails.
Not exactly, you are right about the class not being able to talk to the smarty object, but the {validate ..} function can, (it even has the smarty variable to talk to in the param list).. So my idea is possible but i don't know if it could be usefull...

Typo in documentation, must be 'isCustom', you wrote:
Registers a function to use with "inCustom" criteria type. All functions must be registered before they can be used. You can optinally pass a form name in the case you are validating more than one form at a time. Call this after an init() call.


p.s. Sombody can see what i do wrong? I am having problems with the Append function, so i filed a bug about it because i do everything right in my opinion...
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Jun 02, 2004 2:16 pm    Post subject: Reply with quote

xces wrote:
Not exactly, you are right about the class not being able to talk to the smarty object, but the {validate ..} function can, (it even has the smarty variable to talk to in the param list).. So my idea is possible but i don't know if it could be usefull...


I looked at that, but the problem is {validate ...} is only useful from within the context of the template. I suppose a hacked custom criteria could grab the validation state and assign it, but I'd rather just assign it myself prior to displaying the template.

Maybe a new method in the class:

SmartyValidate::assign_info($smarty, 'validate');

This would would assign useful information like {$validate.formname.valid}
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 Jun 02, 2004 4:09 pm    Post subject: Reply with quote

Another option would be to change SmartyValidate from a stand alone class to an extention of the Smarty class.

This will change the syntax from:

SmartyValidate::init()

to:

$smarty->validate_init()

etc.

Thoughts?
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 Jun 02, 2004 4:34 pm    Post subject: Reply with quote

mohrt, sorry to say I haven't been following this, but as to your last suggestion, why not pass the Smarty instance to SmartyValidate::init() ?

There's no trick to saving values in a static class, it's just a simple trick! Here's an example from one of my static classes:

[php:1:394424dc27]function &_set_config_dir($config_dir = '')
{
static $conf = array();
if (!empty($config_dir)) {
$dir = $config_dir;
}

if (empty($conf['dir'])) {
$dir = BOS_DIR . 'configs';
} else {
$dir = $conf['dir'];
}

if (!array_key_exists($dir, $conf)) {
require_once BOS_DIR . 'BOS_HCnfig_File.class.inc';
$conf[$dir] = new BOS_HCnfig_File($dir);
$conf[$dir]->booleanize = true;
$conf[$dir]->overwrite = false;
}
$conf['dir'] = $dir;
$out = &$conf[$dir];
return $out;
} [/php:1:394424dc27]

Shouldn't be too hard to modify it to save references instead. (Note the use of an array structure on the static--particularly important if you are saving a reference, I do believe)

You can also go the otherway around and have SmartyValidate::init() create and return a Smarty instance (but as I said, I haven't looked at SmartyValidate, so excuse me if I am talking out of my ass)
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Jun 02, 2004 4:41 pm    Post subject: Reply with quote

I know I could require the smarty object as the first parameter to all the SmartyValidate class functions, but that seems a bit clumsy to me. I was thinking that making this an extention to the Smarty class might be a better solution for the future, and making the use of SmartyValidate a little easier.

I'm just looking for pros and cons to this:

SmartyValidate::init($smarty, 'myform')

vs. this:

$smarty->validate_init('myform')
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 Jun 02, 2004 5:07 pm    Post subject: Reply with quote

mohrt wrote:
I know I could require the smarty object as the first parameter to all the SmartyValidate class functions, but that seems a bit clumsy to me. I was thinking that making this an extention to the Smarty class might be a better solution for the future, and making the use of SmartyValidate a little easier.

I'm just looking for pros and cons to this:

SmartyValidate::init($smarty, 'myform')

vs. this:

$smarty->validate_init('myform')


Ahh, but that is the beauty of using a method to store a static var in a static class--you only need to pass the Smarty reference once (say via ::init)--internally, you acquire the reference from your internal method that is used to store it.

The only problems I see subclassing Smarty is that it limits the function names you can use and it means that users have to change how they instantiate their Smarty objects (or perhaps extended Smarty classes) instead of being a separate feature altogether. Perhaps most importantly, it also means that you always load more code where you could potentially want to avoid that, say on cached calls.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Jun 02, 2004 6:34 pm    Post subject: Reply with quote

SmartyValidate runs over several instances of the Smarty Object.. each time you submit the form you have a new instance, so I don't see how saving a reference is going to help any (?)

I see your point with keeping it separate from the Smarty instance though... I guess another option is to force creation of an instance of SmartyValidate instead of using it statically, then pass a reference to the smarty object:

$smarty =& new Smarty();
$validate =& new SmartyValidate($smarty);
$validate->init();
...
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 Jun 02, 2004 7:03 pm    Post subject: Reply with quote

mohrt wrote:
SmartyValidate runs over several instances of the Smarty Object.. each time you submit the form you have a new instance, so I don't see how saving a reference is going to help any (?)


I don't see the issue -- every call to the server is going to force a new instance of Smarty to be created regardless of whether you are using SmartyValidate, not? Unless you mean that you have to deal with several running instances of Smarty during one server call--that is also doable but a little more obtuse.

mohrt wrote:
I see your point with keeping it separate from the Smarty instance though... I guess another option is to force creation of an instance of SmartyValidate instead of using it statically, then pass a reference to the smarty object:

$smarty =& new Smarty();
$validate =& new SmartyValidate($smarty);
$validate->init();
...


Sure, if you don't mind keeping an instance of SmartyValidate, then this makes a lot of sense to me. As an aside, I recently did some testing on various forms of method/function calling and if IIR, I found that static methods were slightly faster than instance methods--but it was fairly marginal with a difference of about .02s over 50000 iterations in my test Smile

Anyhow, as far as I can see, keeping a static instance reference in a static class is equivalent to instancing a separate class that maintains the reference instance--at least functionally. So if there is indeed an issue with the static storage procedure then you will likely have the same issue with a regular instance.
Back to top
View user's profile Send private message
pt2002
Smarty Regular


Joined: 05 May 2003
Posts: 89
Location: Porto, Portugal

PostPosted: Fri Jun 04, 2004 5:22 pm    Post subject: Two questions about this plugin Reply with quote

Hello

I would like to display error messages at the top of the form instead next to the input . I set the error messages {validate... append='error message'}. How can I know there's an error or errors and loop the array with the messages?

Is it possible to assign error messages from config vars in a config file instead coding them in the template itself?

Greetings
pt2002
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Jun 04, 2004 6:41 pm    Post subject: Re: Two questions about this plugin Reply with quote

pt2002 wrote:
I would like to display error messages at the top of the form instead next to the input . I set the error messages {validate... append='error message'}. How can I know there's an error or errors and loop the array with the messages?


I'm working on the next release, you will have {$validate.formname.is_valid} available to use. You could also test the variable being appended for values {if $errors|@count gt 0}...{/if}

The validate tags must be placed in front of where you want them displayed, so just line them all up at the top of the template, then display them.

pt2002 wrote:
Is it possible to assign error messages from config vars in a config file instead coding them in the template itself?


Yes, just use them like any other config variable:
{validate ... message=#foo#}
Back to top
View user's profile Send private message Visit poster's website
hristov
Smarty Rookie


Joined: 04 Jun 2004
Posts: 24

PostPosted: Fri Jun 04, 2004 11:28 pm    Post subject: Notices when refreshing the form before submitting the form Reply with quote

If I refresh the form several time I get Notices that the varibles are not existent. Also since the session is already set, I get the error messages, even though I have not submitted the form, just refreshed the page.

Here is a simple modification of the is_valid(...) function that seems to solve the problem.

Code:

    function is_valid(&$formvars, $form = 'default') {
       
        if(!SmartyValidate::is_init($form)) {
            trigger_error("SmartyValidate: [is_valid] form '$form' is not initialized.");
            return false;
        }
      
      if( empty($formvars) )
         return false;

                ...
       

________
Ch250


Last edited by hristov on Sat Feb 12, 2011 7:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
hristov
Smarty Rookie


Joined: 04 Jun 2004
Posts: 24

PostPosted: Fri Jun 04, 2004 11:45 pm    Post subject: Reply with quote

Quote:

I'm working on the next release, you will have {$validate.formname.is_valid} available to use.


What else should we expect in the next release
________
Herbalaire vaporizer


Last edited by hristov on Sat Feb 12, 2011 7:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
hristov
Smarty Rookie


Joined: 04 Jun 2004
Posts: 24

PostPosted: Sat Jun 05, 2004 8:59 pm    Post subject: Reply with quote

On several occaisons I would have the
Code:

$_SESSION['SmartyValidate'][$form]

initialized; however,
Code:

$_SESSION['SmartyValidate'][$form]['registered_criteria']
$_SESSION['SmartyValidate'][$form]['registered_transform']

were missing. In result of which I was getting some errors.
The code bellow seems to fix the problems ..

Code:

    function is_init($form = 'default') {
      if ( isset($_SESSION['SmartyValidate'][$form]) )
         $return_val = true;
      else
         return false;
         
      if( !isset($_SESSION['SmartyValidate'][$form]['registered_criteria']) )
         $_SESSION['SmartyValidate'][$form]['registered_criteria'] = array();
      if( !isset($_SESSION['SmartyValidate'][$form]['registered_transform']) )
           $_SESSION['SmartyValidate'][$form]['registered_transform'] = array('trim');
   
        return $return_val;
    }   

________
BUY VOLCANO VAPORIZER


Last edited by hristov on Sat Feb 12, 2011 7:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
pt2002
Smarty Regular


Joined: 05 May 2003
Posts: 89
Location: Porto, Portugal

PostPosted: Mon Jun 07, 2004 11:17 am    Post subject: Reply with quote

Thank you very much

Greetings
pt2002
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jun 07, 2004 2:55 pm    Post subject: Reply with quote

2.0RC1 has been released.

I moved on to 2.0 since there have been some changes that break backward compatability. These changes make things a little easier to understand and also give SmartyValidate access to the $smarty object.

The most notable changes are the following:

init() and is_init() were removed. connect($smarty) must always be called prior to using SmartyValidate, passing your smarty object as the parameter. forms are now registered separately with the register_form() method. {$validate.formname.is_error} is now assigned to smarty automatically. See the README for full details and examples.

http://www.phpinsider.com/php/code/SmartyValidate/

hristov, I didn't get those warnings in 2.0 RC1, but let me know if you find any, or anything else wrong before 2.0 is made official.
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
Goto page Previous  1, 2, 3, 4 ... 16, 17, 18  Next
Page 3 of 18

 
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