|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
loremipsum3891 Smarty n00b
Joined: 30 Apr 2010 Posts: 2
|
Posted: Fri Apr 30, 2010 7:17 pm Post subject: smartyValidate problem |
|
|
I'm using smarty validate for my contact form. The form has 4 fields but only 3 are required: name, email, and comments. The problem is that if someone fills in the form and only one of the fields has an error, I get 3 error messages anyways. What do I have to do if I only want error from that specific field printed and not the others. Thanks in advance, the code is as follows:
contact.php
Code: |
<?php
session_start();
require("libs/Smarty.class.php");
require('libs/SmartyValidate.class.php');
$smarty = new Smarty;
if(empty($_POST) || !SmartyValidate::is_registered_form('default')) {
// new form, we (re)set the session data
SmartyValidate::connect($smarty, true);
// register our validators
SmartyValidate::register_validator('name', 'cfname', 'notEmpty', false, false, 'trim');
SmartyValidate::register_validator('email', 'cfemail', 'isEmail', false, false, 'trim');
SmartyValidate::register_validator('comments', 'cfcomments', 'notEmpty', false, false, 'trim');
// display form
$smarty->display('contact.tpl');
} else {
// validate after a POST
SmartyValidate::connect($smarty);
if(SmartyValidate::is_valid($_POST)) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty->display('success.tpl');
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('contact.tpl');
}
}
?>
|
contact.tpl
Code: |
<form name="contact-form" id="contact-form" action="contact.php" method="post" onsubmit="return validateContactForm();">
<p style="float: right; margin-top: 0px;">
{validate id="name" message="Name Cannot Be Empty"} <br />
{validate id="email" message="Email not valid"} <br />
{validate id="comments" message="Message Cannot Be Empty"}
</p>
<!--messages go here-->
<h3 id="msg"> </h3>
<p>
<label for="cfname">Name:*</label>
<input name="cfname" id="cfname" type="text" value="{$cfname|escape}"/>
</p>
<p>
<label for="cfemail">Email:*</label>
<input name="cfemail" id="cfemail" type="text" value="{$cfemail|escape}"/>
</p>
<p>
<label for="cfwebsite">Website:</label>
<input name="cfwebsite" id="cfwebsite" type="text" />
</p>
<p>
<label for="cfcomments">Message:*</label>
<textarea name="cfcomments" id="cfcomments" >{$cfcomments|escape}</textarea>
</p>
<p>
<input type="submit" name="cfsubmit" id="cfsubmit" value="submit" />
<input type="reset" name="cfclear" id="cfclear" value="clear" />
</p>
</form>
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Apr 30, 2010 8:00 pm Post subject: |
|
|
You should only see validation error messages for the fields that are in error. Did you try testing all three fields with different sets of errors and see if you get any different results? Maybe there is a typo in your code, and they all error no matter what? |
|
Back to top |
|
|
|
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
|