View previous topic :: View next topic |
Author |
Message |
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 12:54 pm Post subject: Problem with SmartyValidate & its errormessages |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 2:37 pm Post subject: |
|
|
Quote: | if(SmartyValidate::is_valid($_POST)) |
if(SmartyValidate::is_valid($_POST, 'addAnswer')) |
|
Back to top |
|
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 3:18 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 4:16 pm Post subject: |
|
|
You only want to call disconnect() after the form is validated. |
|
Back to top |
|
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 4:25 pm Post subject: |
|
|
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 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 4:34 pm Post subject: |
|
|
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 |
|
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 4:49 pm Post subject: |
|
|
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!
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 4:50 pm Post subject: |
|
|
Correct. disconnect() wipes out the session data, which you need to retain until the validation is complete. |
|
Back to top |
|
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 4:59 pm Post subject: |
|
|
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
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 5:07 pm Post subject: |
|
|
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 |
|
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 5:14 pm Post subject: |
|
|
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
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 )
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 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 5:22 pm Post subject: |
|
|
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 ) |
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 5:33 pm Post subject: |
|
|
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 |
|
gopher Smarty Rookie
Joined: 04 Mar 2005 Posts: 23
|
Posted: Tue Mar 15, 2005 6:04 pm Post subject: |
|
|
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
I found another "workaround" for that problem with "notEmpty" and spaces though: criteria="notEmpty" transform="trim"
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 15, 2005 7:08 pm Post subject: |
|
|
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 I thought you wanted something else. |
|
Back to top |
|
|