Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Problem with SmartyValidate & its errormessages
Goto page 1, 2  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
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 12:54 pm    Post subject: Problem with SmartyValidate & its errormessages Reply with quote

view.html
<form name="addAnswer" action="?page=threads&view={$threadID}" method="post">

{validate form="addAnswer" field="answer" criteria="notEmpty" message="fooo!" append="Errors"}

{foreach from=$Errors item=Error}
Error? {$Error}!
{/foreach}

<textarea name="answer" class="normal-input" style="width: 500px; height: 150px"></textarea>

<input type="submit" name="add" value="Antworten" class="normal-button">


That's basically my form with all the other html crap left out.

view.php[php:1:0210624c1c]<?
SmartyValidate::connect($smarty);
SmartyValidate::register_form('addAnswer');
[ ... ]
if(SmartyValidate::is_valid($_POST))
{
$Thread->addAnswer($threadID, $_POST);
}
[ ... ]
SmartyValidate::disconnect();?>[/php:1:0210624c1c]

The problem is, that $Errors is always empty, even though it gets that the form is not filled out correctly, and thus doesn't add the answer ..

Any help is greatly appreciated :>
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 15, 2005 2:37 pm    Post subject: Reply with quote

Quote:
if(SmartyValidate::is_valid($_POST))


if(SmartyValidate::is_valid($_POST, 'addAnswer'))
Back to top
View user's profile Send private message Visit poster's website
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 3:18 pm    Post subject: Reply with quote

mohrt wrote:
if(SmartyValidate::is_valid($_POST, 'addAnswer'))


Oh, missed that :/

Thanks!

[edit] Hm, didn't change anything at all, I'm still not getting the error message, and even a simple {validate form="addAnswer" field="answer" criteria="notEmpty" message="fooo!"} won't display anything. The {validate} part doesn't have to be directly above or beneath the <input> field, right?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 15, 2005 4:16 pm    Post subject: Reply with quote

You only want to call disconnect() after the form is validated.
Back to top
View user's profile Send private message Visit poster's website
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 4:25 pm    Post subject: Reply with quote

mohrt wrote:
You only want to call disconnect() after the form is validated.


Well disconnect() is called at the end of the file, after which only ob_end_flush(); mysql_close($con); follow, so my guess would be the form has been already validated at that point Wink
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 15, 2005 4:34 pm    Post subject: Reply with quote

The problem is calling disconnect() before they fill out the form.

You have to:

* connect()
* draw the form
* do the validate/redraw cycle
* disconnect()

Do do NOT want to:

* connect()
* draw the form
* disconnect()
* do the validate/redraw cycle
* disconnect()

See the difference? Calling connect() on each invocation is fine, calling disconnect() on each invocation is not, only do that after the form is validated.
Back to top
View user's profile Send private message Visit poster's website
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 4:49 pm    Post subject: Reply with quote

mohrt wrote:
The problem is calling disconnect() before they fill out the form.

[ ... ]

See the difference? Calling connect() on each invocation is fine, calling disconnect() on each invocation is not, only do that after the form is validated.


Ah! Smile

So I would want to call disconnect only after for example I found out the form was filled out correctly and inserted the data into the table, correct?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 15, 2005 4:50 pm    Post subject: Reply with quote

Correct. disconnect() wipes out the session data, which you need to retain until the validation is complete.
Back to top
View user's profile Send private message Visit poster's website
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 4:59 pm    Post subject: Reply with quote

mohrt wrote:
Correct. disconnect() wipes out the session data, which you need to retain until the validation is complete.


Got confused with Formsess' way of doing it Wink

Now my only problem is that when I change anything that deals with the form that I validate, the changes aren't being displayed; instead I have to kill the session. Is there a function for that, that I missed, or is there a "workaround" for my problem? Something like SmartyValidate:rehash('form'); that would keep the entered data, but would apply the new "code"
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 15, 2005 5:07 pm    Post subject: Reply with quote

If you are developing and changing things on the form, you must clear the session to pick up the changes. If you use:

SmartyValidate::register_form('myform', true);

That will clear the session, so do that each time you first come into the form.
Back to top
View user's profile Send private message Visit poster's website
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 5:14 pm    Post subject: Reply with quote

mohrt wrote:
If you are developing and changing things on the form, you must clear the session to pick up the changes. If you use:

SmartyValidate::register_form('myform', true);

That will clear the session, so do that each time you first come into the form.


I guess that will do, of course it would have been neat do actually keep the entered variable and still be able to use the new form Wink

I only got two more things:

1) Is there a check similiar to isLength that does *not* require a max argument? Or do I need to "write" my own function (more like copy isLength and change it slightly Wink)

2) Is there a buildin check that checks if the entered value actually is anything besides a space (notEmpty won't do that) or would I need to modify notEmpty ?

Thanks for keeping up with my questions Wink
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 15, 2005 5:22 pm    Post subject: Reply with quote

gopher wrote:

1) Is there a check similiar to isLength that does *not* require a max argument? Or do I need to "write" my own function (more like copy isLength and change it slightly Wink)


Yes, you can write your own custum plugin to do what you want.

gopher wrote:

2) Is there a buildin check that checks if the entered value actually is anything besides a space (notEmpty won't do that) or would I need to modify notEmpty ?


You can write your own custom plugin, maybe call it notSpace or something.
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: Tue Mar 15, 2005 5:33 pm    Post subject: Reply with quote

See if this does what you want. If it works, I'll commit to the CVS tree. This allows min or max to be left empty, meaning there is no restriction.

[php:1:329e971465]
function smarty_validate_criteria_isLength($value, $empty, &$params, &$formvars) {

$_min = isset($params['min']) ? $params['min'] : -1;
$_max = isset($params['max']) ? $params['max'] : -1;

$_length = strlen($value);

if(($_min == -1 || $_length >= $_min) && ($_max == -1 || $_length <= $_max))
return true;
elseif($_length == 0)
return $empty;
else
return false;
}
[/php:1:329e971465]
Back to top
View user's profile Send private message Visit poster's website
gopher
Smarty Rookie


Joined: 04 Mar 2005
Posts: 23

PostPosted: Tue Mar 15, 2005 6:04 pm    Post subject: Reply with quote

mohrt wrote:
See if this does what you want. If it works, I'll commit to the CVS tree. This allows min or max to be left empty, meaning there is no restriction.


Seems to be working fine Smile

I found another "workaround" for that problem with "notEmpty" and spaces though: criteria="notEmpty" transform="trim"

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


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

PostPosted: Tue Mar 15, 2005 7:08 pm    Post subject: Reply with quote

gopher wrote:

I found another "workaround" for that problem with "notEmpty" and spaces though: criteria="notEmpty" transform="trim"


Yes, that is what trim is for Wink I thought you wanted something else.
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 1, 2  Next
Page 1 of 2

 
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