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

Showing form errors

 
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 -> General
View previous topic :: View next topic  
Author Message
inerte
Smarty Rookie


Joined: 24 Sep 2003
Posts: 9

PostPosted: Fri Oct 31, 2003 1:58 pm    Post subject: Showing form errors Reply with quote

Hi!

I need to display information about what errors ocurred when the user filled a form, and I trying to figure out what's the best way (or how do you do it Wink) using Smarty.

I am currently (without Smarty templates), after the form is processed, registering the error message on a session variable, and checking on the form if it's present, and them calling a function to display it. The function is simple:
Code:

function showError($error_message)
{
    return '<span class="' . $this->errorColor . '">' . $error_message . "</span\n">;
}

Where $this-errorColor is a var which holds the css class with just color:red;

I am trying to apply a similar process with Smarty. I begun using a method that I don't know if it's the best, so I am looking for methods and tips or how could I improve the 'logic and presentation' of my code. I made a template for the form (which is used in several places, it's an "upload file" form).

Part of the .tpl looks like this:
Code:

<input type="text" name="date_day" value="{$date_day}" />
{$error_date_day}

Part of the .php which calls $smarty->display() on the template above is:
Code:

$smarty->assign('error_date_day', '');
if (isset($_SESSION['error_date_day'])) {
    $smarty->assign('error_date_day', showError($_SESSION['error_date_day']);
}

But this way doesn't "feel" right. I want to put the css error class on a smarty config file, and instead of using a showError() function, I want something which uses more of a template engine features.

I looked into plug-ins, and I guess I could use a plug-in to substitute showError(), but isn't too much of an overhead? I am kind of a newbie with Smarty, and my problem looks like it is very simple. In essence, I guess it is: How to tell that certain variable, if it's set or not empty, should be displayed, and how it should be displayed?

How do you process your forms and present the errors to the user?

I am sorry if this topic has already been discussed, I did search the forums, Wiki and FAQs, but couldn't find a "tip" on this topic. I guess it's because it's a very personal problem, each developer/ app does/requires things differently.

Also, sorry for the verbose post, and any gramatical or spellings mistakes Smile
Back to top
View user's profile Send private message Visit poster's website
Midas
Smarty n00b


Joined: 30 Oct 2003
Posts: 3

PostPosted: Fri Oct 31, 2003 9:59 pm    Post subject: Reply with quote

What about the following in your template

Code:

{if $error_date_day}
{#errorBegin#}{$error_date_day}{#errorEnd#}
{/if}


this in a config file

Code:

errorBegin = <span class="myerror">
errorEnd = </span>


and this in your script

Code:

$smarty->assign("error_date_day", "My error message");


This works without any piece of HTML in the PHP code. If error_date_day is not set (because there was no error), nothing gets printed. And if you want to change the look of your error messages, you can do it all at once by changing the config file entry.
Back to top
View user's profile Send private message
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 -> General 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