View previous topic :: View next topic |
Author |
Message |
foof Smarty n00b
Joined: 21 Dec 2004 Posts: 1
|
Posted: Tue Dec 21, 2004 10:15 am Post subject: Error messages in SmartyValidate |
|
|
I want my errormessages to be listed under each other, it seems like I can't do this with <br /> in the message:
{validate field="name" criteria="notEmpty" message="Missing name<br />"}
{validate field="email" criteria="notEmpty" message="Missing email<br />"}
How can I do this if I don't want the <br /> to show if there is no errormessages?
Also, I want the text in the color red, how do I do this the easiest way? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Dec 21, 2004 2:37 pm Post subject: |
|
|
Something like this:
{validate field="name" criteria="notEmpty" message="Missing name" append="errors"}
{validate field="email" criteria="notEmpty" message="Missing email" append="errors"}
Then loop over $errors array and display them however you wish. |
|
Back to top |
|
gestionti Smarty Rookie
Joined: 02 Mar 2005 Posts: 15 Location: Gatineau, Qc, Canada
|
Posted: Wed Mar 02, 2005 11:19 pm Post subject: |
|
|
I am learning Smarty and I like it so far... Been trying SmartyValidate and I want to display the error messages at the bottom of the form. Error messages will not appear. Here is what I have:
<form method="post" action="validate.php">
{validate field="FullName"
criteria="notEmpty"
transform="trim"
message="Full Name Cannot Be Empty" append="errors"}
Full Name: <input type="text"
name="FullName" value="{$FullName|escape}"><br />
{validate field="Phone"
criteria="isNumber"
empty="yes" transform="trim"
message="Phone Number Must be a Number" append="errors"}
Phone :<input type="text"
name="Phone" value="{$Phone|escape}" empty="yes"><br />
etc.... and finally :
{if !empty($errors)}
<table border="1">
<TR><TD COLSPAN="3"><FONT COLOR=#ff0000>
{section name=msg loop=$errors}
error: {$errors[msg]}<br>
{/section}
</FONT></TD></TR></table>
{/if}
All I get listed is error:
please advise. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 6:58 am Post subject: |
|
|
does {$errors|@print_r} show anything? |
|
Back to top |
|
gestionti Smarty Rookie
Joined: 02 Mar 2005 Posts: 15 Location: Gatineau, Qc, Canada
|
Posted: Thu Mar 03, 2005 1:16 pm Post subject: |
|
|
Thanks for reply!
I place the {$errors|@print_r} within the section tag, and here is the output.
error: E
Array ( [FullName] => Enter full name [Phone] => Phone Number Must be a Number [CCExpDate] => Exp Date not valid ) 1
error: x
Array ( [FullName] => Enter full name [Phone] => Phone Number Must be a Number [CCExpDate] => Exp Date not valid ) 1
error: p
Array ( [FullName] => Enter full name [Phone] => Phone Number Must be a Number [CCExpDate] => Exp Date not valid ) 1
In the means time, I tried with {foreach} tag like so :
{if !empty($errors)}
<table border="1">
<TR><TD COLSPAN="3"><FONT COLOR=#ff0000>
{foreach from=$errors item=error}
erreur: {$error}<br>
<br>
{/foreach}
</FONT></TD></TR></table>
{/if}
And so far this seems to be working... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 2:38 pm Post subject: |
|
|
SmartyValidate is appending $key => $val to the array each time, with $key being the name of the field with the error. Using a {section} loop won't work because it requires zero-index keys. Foreach will work fine, as you are doing. |
|
Back to top |
|
gestionti Smarty Rookie
Joined: 02 Mar 2005 Posts: 15 Location: Gatineau, Qc, Canada
|
Posted: Thu Mar 03, 2005 10:36 pm Post subject: |
|
|
Outstanding!!
Now my other problem is with Radio button. User must select male of female. Not sure how I validate this. It cannot be empty. I tried this and it does not work :
{validate field="sexe"
criteria="notEmpty"
message="Sélectionner un genre"
append="errors"}
<input type="radio" name="sexe" value="{$homme|escape}" tabindex="4">Homme<br />
<input type="radio" name="sexe" value="{$femme|escape}" tabindex="5">Femme<br />
Plus if I make a selection, when it returns to the form after validation, nothing is selected. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 10:50 pm Post subject: |
|
|
You submit the form with nothing selected and it doesn't come up with an empty error message? Or something else?
As for not being selected on redraw, that is because you need to identify which one is selected (or more properly "checked" with radios);
<input type=radio name=sexe value=foo {if $sexe eq "foo"}checked="checked"{/if}>
Look at the {html_radios} function that comes with Smarty, it will ease the pain with managing radio button generation/selection. Same with {html_options} and {html_checkboxes} |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 10:52 pm Post subject: |
|
|
btw, any time you change validation criteria in the template during development, clear the validation session data! |
|
Back to top |
|
|