|
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 |
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 2:25 am Post subject: SmartyValidate: so what's this "default form" anyw |
|
|
Hi,
haven't been here for a while and just ran into this (interesting) Add-On.
I'd like to use it but either I'm dumb or there's another huge lack of understanding the "obvious" manual, but for the sake of my life I cannot figure what this mysterical "default form" is supposed to be ...
is this a file? if so what's it name supposed to be?
is it a variable?
the "name" of the <form> element?
the first <form> element in any of the templates rendered?
just a plain ID-string to bind validators to "a <form>"? if so which one, if I have more than one in a page?
I usually have this file structure and variables:
Code: | /templates/page_default.phtml
/templates/page_popup.phtml
/templates/page_redirect.phtml
/templates/... (header, footer, etc.)
/templates/forms/feedback.phtml
/templates/forms/login.phtml
/templates/forms/ ...
/templates/content/de/stuff/somefile1.html
/templates/content/de/stuff/somefile2.html
/templates/content/de/other/stuff/foobar.html |
$Page - my subclassed Smarty instance
page_default.phtml which is passed to $Page>display() and typically containing a zillion nested {include}-statements
Code: | <html>
... bunch of HTML ...
{include file=$ContentFile}
... more HTML
</tml> |
$ContentFile: a filename evaluated at runtime used for the - well - content inside the page template, having any extension (not necessrily the one Smarty uses by default!), usually someplace in a dir below $template_dir, e.g.
Code: | <?php
...
$ContentFile = "/templates/content/de/stuff/somefile1.html";
...
Page->assign_by_ref("ContentFile", $ContentFile);
..
$Page->display();
?> |
The $ContentFile (template) may also contain one or myriad of other {include ..} wth possible one being: a HTML <FORM>.
Because I barely have static filenames for the form includes as well so it's another variable pointing to the actual filename containing a <form> element:
Code: | $Page->assign('feedback_form', 'forms/feedback.phtml'); |
and in the $ContentFile there'd be
Code: | bla bla
{include fle=$feedback_form}
blabla |
Still with me?
Now consider the obvious that "forms/feedback.phtml" has some markup (descriptive text, instructions) AND a <form> element somewhere inside.
I can add {validate ..} all over the place and my $_SESSION has this bizarre 'default' element matching the {validate ...} conditions I added, however: nothing gets validated and I don't get any error messages in place of the {validate ..} functions.
Given the above scenario I believe I must use register_form() or something alike. To me, such plain vanilla names and variables like "myform" or "form.tpl" or $form don't help me to understand the true relationship among them
This is how I used it:
Code: | <?php
// some init code, then ...
session_start();
/* load Monte's SmartyValidate class */
require_once $modules['_CLASS_SMARTYVALIDATE'];
// default form template
$Page->assign('feedback_form', 'forms/feedback.phtml');
// required initialization
SmartyValidate::connect($Page);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( SmartyValidate::is_valid($_POST) ) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$Page->assign('feedback_form', 'forms/feedback_success.phtml');
}
}
// ...
$Page->display("page_default.phtml");
|
This certainly lacks a few lines doing the trick: but where?
Note, that I need ot display("page_default.phtml") (or alike) which works as described above using several other nested templates one containing
{include fle=$feedback_form}
Any hints are greatly appreciated.
t.i.a.
Have fun,
CirTap |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 01, 2005 2:38 am Post subject: |
|
|
If you don't specify a form name, "default" is used implicitly. You only have to supply a form name if you are validating two or more forms simultaneously. |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 4:21 am Post subject: |
|
|
Hi,
thanx for your reply but it doesn't clarify a bit
I bet I miss a stupid basic concept and this is driving me nuts %-/
What do you mean with "form name"? This is exactly what I don't get ..
Do you mean the "name attribute of an HTML form element present in the template being parsed"? And presuming there's one *one* <form> in such a template treat this as the "default" ignoring it's name attribute even if present, or the *first* form present, or ... you see? Me feeling like a DAU.
Where's this mystic "form name" supposed to appear/be declared/defined -- or not? I never ever had a <form name="default"> (which would be a pretty stupid name anyway), but rather something more descriptive in case JS is involved.
Also, I'd never ever even think of having a template file containing a <form> and call this just "default.tpl" - as this would make even less sense in even in the simplest website having just "one [default] form": login.tpl or feedback.tpl or guestbook.tpl
This "default form" concept is totally irritating because such thing doesn't exist for me <gg> I had less trouble reading "Universe in a Nutshell"
Picture some html page having a "login form" on the left and a "search form" on top and a "feedback form" sitting in the middle of the very same page, included in "somepage.phtml" via
Code: | {include file="forms/search.html"}
bla bla
{include file="forms/login.html"}
bla bla
{include file="forms/feedback.html"} |
Not necessarily a very abstract or uncommon structure, right?
This makes three HTML form elements on the page comming from three sub-templates beeing rendered thru the main page template via $Page->display("somepage.phtml").
login.phtml:
<p>Authenticate'cha:</p>
<form name="frmLogin" action="login.php">
<input ...>
</form>
search.phtml:
<form name="frmLogin" action="search.php">
Seach: <input ...>
</form>
feedback.phtml:
<p>Gimme your thoughts:</p>
<form name="frmFeedback" action="feedback.php">
<input ...>
</form>
So what's the "default" here - if any?
One HTML page made of four templates processed by one of three possible .php scripts and at least two to perform validation.
Let's say I'm interested in giving feedback and ignore the "login form" and "search form" - I hit "Send Feedback" and feedback.php runs.
How would I use the Validation class in "feedback.php" which will always display("somepage.phtml") with these three sub-templates to validate the <form name="frmFeedback">?
Pleeeze enlight me!
Thanx!
Have fun,
CirTap |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Feb 01, 2005 5:28 am Post subject: |
|
|
Hi ClrTap.
You deserve a longer response but I'll just chime in to remind that even if you define several forms in a single page only one form is actually submitted back to the server. As you know, information is stored in session and a session can span many requests and hence many form submissions. As a consequence there could arise the need for differentiating submission forms -- for example, for a multi-page/part form that permitted back-tracking. With SmartyValidate, you use unique names for each form so that the variables of the different forms don't collide with each other in Session space. So the naming of the forms is at the application level.
To sum up: 1 request yields 1 form but 1 session may yield many different forms which would necessarily span multiple submissions. Using form names at the application level distinguishes different forms (as required) during the lifetime of the session. That said, this is probably not an everyday sort of feature -- most transactions will probably (should probably) only last for the duration of the form submission/response cycle and the submission will typically (should likely) contain all the data needed to complete the submission. That way, only the most recently posted form (ie. 'default') is needed at any given time.
That's the 15 second spiel on form submission. Hopefully it is enough so that the other details click for you. |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 2:40 pm Post subject: |
|
|
Hi boots,
now this reveals the hidden picture! Thanx!
@Monte: I recommend to add boots' lines to the manual/readme
Talking about forms and "form names" w/o giving a little hint that such "form names" are purely virtual and in no way related to the "physical" <form name="foobar"> and nothing but "application level keywords" in the session variable was the root of my misunderstanding. The term "form name" to me applies exclusively to the name attribute at the HTML level - and here I got stuck.
I should have used
is_valid($_POST, 'frmFeedback')
to have my data bound to the right key.
So this 'default' is nothing but the initial key(word) in the session array, unless I call it something different, right? A print_r() of $_SESSION revealed that this 'default' item would still exists due to the class constructor but being empty)
Next, is_valid($_POST) actually validates the given array argument and assigns the results to this session "keyword", presuming this is the data from the form being submitted.
Assumtion: whatever array is passed to is_valid() is processed and goes to a predefined key (form name) in the session. Generally speaking: SmartyValidate's a more of a generic "array value validator" and could be misused to validate a bunch of values. The fact that submitting a <form> produces a array called $_POST suitable for SmartyValidate is just a handy coincidence
@Monte: I think I can now use this goodie <g>, and from the various threads (and the known quality of your code) I believe this will prevent me from thinking about using the typical overweightned PEAR QuickForm.
As webadept wrote in this 14 pages monster-thread: there's nothing faster than writing
<input class="txt-tiny" type="text" name="foobar" value="" />
to create a form element ...
Ok, I'll go off, digging into form validation...
Have fun
CirTap |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 4:22 pm Post subject: |
|
|
Hi,
cleaned up my code and it now worx like a charme I love it!
I know this thingy would be cool to use
Thanx a lot Monte!! Great stuff!
And again, than you boots to shove me into the right direction
Have fun y'all,
CirTap |
|
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
|
|