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

SmartyValidate doesn't clear messages

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


Joined: 29 Sep 2004
Posts: 3

PostPosted: Wed Sep 29, 2004 5:48 pm    Post subject: SmartyValidate doesn't clear messages Reply with quote

Hi,

Great plug-in!

I've set up a couple "notEmpty" validators and when I fill the input field the message does not clear on re-display of the form.

What am I doing wrong?

Thanks.
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


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

PostPosted: Wed Sep 29, 2004 6:30 pm    Post subject: Reply with quote

What does your template and relevant PHP code look like?
Back to top
View user's profile Send private message Visit poster's website
hsal
Smarty n00b


Joined: 29 Sep 2004
Posts: 3

PostPosted: Wed Sep 29, 2004 6:53 pm    Post subject: Reply with quote

I got it working with some experimenting. The problem seems to be in using a form="formname" instead of the default form. When I removed all references to "RegistrationForm" it worked correctly.

Maybe this is a bug you are aware of? It would be useful to be able to use the form name.

Anyway the code that did not work was as follows:

Template (just part because it is so large):

Code:
<div style="margin-left: 15px;">
<form method="post" action="doregform.php" name="RegistrationForm">
  <h1 class='formtitle'>Registration</h1>
      <h2>{$reference_num_desc}</h2>
  <!-- validation errors here -->
  <div class='regerr'>
  {section name=valerror loop=$RegValErrors}
  {$RegValErrors[valerror]}
  {/section}
  </div>
  <!-- --------------------- -->
  <input id="form_name" type="hidden" name="form_name" value="{$form_name}"/>
  <input id="reference_num" type="hidden" name="reference_num" value="{$reference_num}"/>
  <table width="950" border="0">
    <tr bgcolor="ffcc00">
      <td valign="baseline" colspan="4" class="sectionhdr">Distributor/OEM Information</td>
    </tr>
    <tr>
      <td><label for="distrib_code">Code:</label></td>
      <td>{validate form="RegistrationForm" field="distrib_code" criteria="notEmpty" message="• Distributor Code is required.<br />" append="RegValErrors"}
.
.
.

PHP:

Code:
<?php
session_start();
require ('./Smarty/libs/Smarty.class.php');
require ('./Smarty/libs/SmartyValidate.class.php');
require ('registration.inc.php') ;

$Reg = new Registration;

$FindRefNum = 100001;
//$Reg->GetRegistration($FindRefNum);

$smarty = new Smarty;

$smarty->compile_check = true;
//$smarty->debugging = true;

// Init Validator
SmartyValidate::connect($smarty);
SmartyValidate::register_form('RegistrationForm', true);

if(empty($_POST)) {
   $smarty->assign("form_name", "Challenger");
   $smarty->assign("reference_num_desc", "");
   $smarty->assign("reference_num", 0);
                 .
                 .
                 .
   $smarty->display('registration_v.tpl');
}
else {
   // validate after POST
   if (SmartyValidate::is_valid($_POST)) {
      // no errors--done with SmartyValidate
      SmartyValidate::disconnect();
      $smarty->assign("alert_msg", "All Validation Passed.");
         $smarty->display('index_alert.tpl');
   }
   else {
   $smarty->assign($_POST);
   $smarty->display('registration_v.tpl');   
   }   
}   



Thanks.
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


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

PostPosted: Wed Sep 29, 2004 7:26 pm    Post subject: Reply with quote

Your problem is this line:

[php:1:3f7ba0d194]SmartyValidate::register_form('RegistrationForm', true); [/php:1:3f7ba0d194]

The "true" parameter tells SmartyValidate to "reset" the form, so everytime you post your form the validation criteria gets reset. You only want that to happen when you initially draw the form! You can remove that parameter so the reset only happens initially, but then it will not get reset when might want it to. One quick fix is to do this (only reset form when $_POST is empty):

[php:1:3f7ba0d194]SmartyValidate::register_form('RegistrationForm', empty($_POST));[/php:1:3f7ba0d194]

Or do an explicit reset by other means.

When you removed the form="RegistrationForm" parameter from your template, you are then using the "default" form, so your register_form('RegisterationForm') call is not being used at that point.
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 Sep 29, 2004 7:50 pm    Post subject: Reply with quote

@monte: this is just an idea...

how about a new method to explicitly reset forms [say: reset_form()] and that register_form() is modified to automatically do an empty check on the passed form data array and then act accordingly. That way there is more auto-supplied magic while still retaining api control.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Sep 29, 2004 8:26 pm    Post subject: Reply with quote

I suppose reset_form('formname') is a little shorter than register_form('formname', true), but I'm not sure about automatically checking submitted data... that needs to be explicitly passed, you don't know if it lies in $_POST, $_GET, or some other variable controlled by the programmer.
Back to top
View user's profile Send private message Visit poster's website
hsal
Smarty n00b


Joined: 29 Sep 2004
Posts: 3

PostPosted: Wed Sep 29, 2004 9:01 pm    Post subject: Reply with quote

Thanks, I'll try that.

The other point I didn't make clear was that it seemed to work fine on just the first field, but when I added others they were the ones that didn't clear after input into the field. The first field message did clear.

Anyway, I have two possible solutions now.

Thanks.
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


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

PostPosted: Wed Sep 29, 2004 9:45 pm    Post subject: Reply with quote

When the form is first displayed, the validators get written to the session. If you add validators, you need to clear the session data so the new validators get picked up (or reset the form.)

SmartyValidate::disconnect();

or

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

This only happens during development, you obviously don't add/subtract validators between runs in production.
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: Thu Sep 30, 2004 12:49 am    Post subject: Reply with quote

mohrt wrote:
I suppose reset_form('formname') is a little shorter than register_form('formname', true), but I'm not sure about automatically checking submitted data... that needs to be explicitly passed, you don't know if it lies in $_POST, $_GET, or some other variable controlled by the programmer.


I should have made clear that the assumption was that the second parameter to register_form() would be the array to test against (say $_POST, $_GET or anything else they were using as a bucket) instead of the current boolean that the function uses. The idea is that register_form() would check the array and decide that if it was empty, it should reset the form and otherwise assume that the program will call form_reset() as required.

I was just thinking out-load there, but it was probably a half-baked idea.
Back to top
View user's profile Send private message
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