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

addressing HTML_QuickForm array elements in Smarty

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Sat Aug 14, 2004 5:27 pm    Post subject: addressing HTML_QuickForm array elements in Smarty Reply with quote

hi all,

i've a question of how to refer, in Smarty, to a *specific* error element in a PEAR::HTML_QuickForm array ...

after reading the fairly detailed tutorials on using QuickForm with Smarty:

"PEAR HTML_QuickForm Getting Started Guide"
<http://www.thelinuxconsultancy.co.uk/quickform.html>
"Using Smarty Templates With PEAR HTML_QuickForm"
<http://www.thelinuxconsultancy.co.uk/smarty-guide.html>

as well as the PEAR docs for the module, i'm still unclear ...

towit ... (note: my L & R smarty delims are "<ste:" & ":ste>", respectively)

i've a properly initialized php file:
...
// define form elements & assign attributes
$form->addElement('header', 'frmHeader', 'text');
$form->addElement('text', 'aaa', 'AAA:');
$form->addElement('text', 'bbb', 'BBB:');
$form->addElement('password', 'ccc', 'CCC:');
$form->addElement('submit', 'btnSubmit', 'Submit');
// Create the form renderer object
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smarty);
// build the form-specific HTML
$form->accept($renderer);
// assign form data to a smarty array, display
$smarty->assign('form_data', $renderer->toArray());
$smarty->display('test.tpl');
...

the template, "test.tpl", includes:
<tr><td><ste:$form_data.aaa.label:ste></td><td><ste:$form_data.aaa.html:ste></td></tr>
<tr><td><ste:$form_data.bbb.label:ste></td><td><ste:$form_data.bbb.html:ste></td></tr>
<tr><td><ste:$form_data.ccc.label:ste></td><td><ste:$form_data.ccc.html:ste></td></tr>


which properly displays:
AAA: [field]
BBB: [field]
CCC: [field]


if i add a conditional error template:
// conditionally display an error message
$renderer->setErrorTemplate('
<ste:if $error:ste>
<font color="orange" size="+0"><b><ste:$error:ste></b></font>
<ste:/if:ste>
<ste:$html:ste>
');

prior to "$form->accept($renderer);", it does what it should ... say , in the case of an error in the SECOND element, i see:

AAA: [field]
"(ERROR TEXT for element 'bbb')"
BBB: [field]
CCC: [field]


ok. all clear.

now, the question is -- if i want to MANUALLY place the error message somewhere else, how do i do it?

if i want to see:

AAA: [field]
BBB: [field]
CCC: [field]
"(ERROR TEXT ALWAYS HERE, WHETHER 1 or MORE ERRORS ...)"

in effect, creating an "error display window"

reading through the tutorial and the PEAR docs, i thought mod'ing "test.tpl" like:

<tr><td><ste:$form_data.aaa.label:ste></td><td><ste:$form_data.aaa.html:ste></td></tr>
<tr><td><ste:$form_data.bbb.label:ste></td><td><ste:$form_data.bbb.html:ste></td></tr>
<tr><td><ste:$form_data.ccc.label:ste></td><td><ste:$form_data.ccc.html:ste></td></tr>
+++ <tr><td colspan="2"><ste:$form_data.errors:ste></td></tr>

would do the trick, but all i get is:

AAA: [field]
BBB: [field]
CCC: [field]
"Array"

do i need to use some logic in the .tpl to read through each of the form_data $error array elements, see if its non-zero, then conditionally output? doable, but clunky ... ESPECIALLY ... when there are MANY element. there's gotta be an easier way

thoughts? suggestions? wisdom?

cheers,

richard


Last edited by OpenMacNews on Sun Aug 15, 2004 12:33 am; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Aug 15, 2004 12:26 am    Post subject: Reply with quote

Hi.

I'm not familiar with that HTML_QuickForm, but it appears that <ste:$form_data.errors:ste> is returning an array. That means you need to loop it to get the results you want:

<ste:foreach from=$form_data.errors:ste item=err:ste>
<ste:$err:ste>
<ste:/foreach:ste>

Like I said, I'm not familiar with the class, so the array that is returned may be more complicated than the general case I show above. Modify as appropriate Wink

Cheers.
Back to top
View user's profile Send private message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Sun Aug 15, 2004 12:55 am    Post subject: Reply with quote

hi,

thx! for the reply.

the loop certainly makes good, simple sense to me ... but when i place the loop code you gave in my .tpl file, *and* keeping the
$renderer->setErrorTemplate(' ... stanza in the php code, i would expect to see two things:

(1) the as-described setErrorTemplate-generated "ERROR TEXT" placed next to the offending {label} or {html}, per class def'n,

*AND*,

(2) a list (containing AT LEAST the one error generated) displayed where i defined the output loop you suggested.

again, makes sense 2 me.

unfortunately, the output of (1) is there, but *nothing* (blank, nada) is shown where i include your code snippet ...

hmmm ... am i missing something blindingly simple or subtle?

richard
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Aug 15, 2004 7:11 am    Post subject: Reply with quote

Sorry that I can't give you a better answer... I suggest you put a {debug} into your template so that you can see the actual structure of the error array that is passed back. Perhaps a Pear_QuickForm user will respond with a better answer.

BTW, no need to cross-post--we read you Smile
Back to top
View user's profile Send private message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Sun Aug 15, 2004 4:46 pm    Post subject: Reply with quote

thx!

richard
Back to top
View user's profile Send private message
OpenMacNews
Smarty Rookie


Joined: 03 Aug 2004
Posts: 34
Location: Floating on Io's Methane Seas ...

PostPosted: Sun Aug 15, 2004 6:13 pm    Post subject: Reply with quote

ok, so i'm blind as a bat!

your suggested code snippet:

<ste:foreach from=$form_data.errors:ste item=err:ste>

has a simple, 'extra' right delim typo .... and should, instead, be:

<ste:foreach from=$form_data.errors item=err:ste>

i must stared at that 20 times! ... Shocked

thx! i'm set ...

richard
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Aug 15, 2004 7:35 pm    Post subject: Reply with quote

[quote="OpenMacNews"]ok, so i'm blind as a bat!

your suggested code snippet:

<ste:foreach from=$form_data.errors:ste item=err:ste>

has a simple, 'extra' right delim typo .... quote]

You and me both! Sorry about that. I was cut-and-pasting your delimiters. My lesson is to stick to the standard delimiters when answering posts!

Cheers!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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