Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Great question, and one which took me a long time to figure out the best way to solve.</p> <p>The CodeIgniter <a href="http://codeigniter.com/user_guide/libraries/form_validation.html">Form Validation library</a> is great, but can only be used with proper views and controllers, so it doesn't work out of the box when you are developing a front end tag.</p> <p>Normally, the preferred way of submitting a front-end form is to register an 'Action' in your upd.addon.php file (I'm guessing you have done this for your <code>submit_form()</code> function). This is then assigned a number, which you can post with the url <code>/index.php?ACT=37</code> or something similar. This is a good system, because it means we know the form submission came from our module. However, with input forms, this is a hindrance, because it means we can't repopulate input fields. You therefore need to configure the input form to post back to the current URL, and wait until the template engine tries to render your tag before handling the form submission.</p> <p>The easiest, and visually ugliest way to achieve this, is to use <code>$this-&gt;EE-&gt;output-&gt;show_user_error(FALSE, array_of_errors)</code>. You can actually use this from either an action, or within your module code. It displays the standard grey EE message page we have all grown to know and hate.</p> <p>With that sort of intro, you probably knew the solution wasn't going to be quite that simple, right? Here's a skeleton of a tag function which implements inline error checking:</p> <pre><code>function my_form() { // load default tag variables $tag_vars = array(); $tag_vars[0] = array( 'first_name' =&gt; '', 'error:first_name' =&gt; '', 'last_name' =&gt; '', 'error:last_name' =&gt; '' ); // handle a form submission if ($this-&gt;EE-&gt;input-&gt;post('my_form_hidden') == '1')) { // load POST data into tag $tag_vars[0]['first_name'] = $this-&gt;EE-&gt;input-&gt;post('first_name', TRUE); $tag_vars[0]['last_name'] = $this-&gt;EE-&gt;input-&gt;post('last_name', TRUE); // use CI validation library to check submission $this-&gt;EE-&gt;load-&gt;helper('form'); $this-&gt;EE-&gt;load-&gt;library('form_validation'); $this-&gt;EE-&gt;form_validation-&gt;set_rules('first_name', 'lang:first_name', 'required'); $this-&gt;EE-&gt;form_validation-&gt;set_rules('last_name', 'lang:first_name', 'required'); $valid_form = $this-&gt;EE-&gt;form_validation-&gt;run(); if ($valid_form) { // probably save something to database, then redirect } else { $form_errors = array(); foreach (array('first_name', 'last_name') as $field_name) { $field_error = form_error($field_name); if ($field_error) { $form_errors[] = $field_error; $tag_vars[0]['error:'.$field_name] = $field_error; } } if ($this-&gt;EE-&gt;TMPL-&gt;fetch_param('error_handling') != 'inline') { // show default EE error page return $this-&gt;EE-&gt;output-&gt;show_user_error(FALSE, $form_errors); } } } // parse and output tagdata $out = $this-&gt;EE-&gt;functions-&gt;form_declaration(array( 'action' =&gt; $this-&gt;EE-&gt;functions-&gt;fetch_current_uri(), 'hidden_fields' =&gt; array('my_form_hidden'))); $out .= $this-&gt;EE-&gt;TMPL-&gt;parse_variables($tagdata, $tag_vars); return $out.'&lt;/form&gt;'; } </code></pre> <p>This way, the designer can specify <code>error_handling="inline"</code> in the tag if they want inline errors, otherwise they will just be redirected to the standard error form. If they do request inline error handling, they will simply need to make sure their inputs look like this:</p> <pre><code>&lt;input type="text" name="first_name" value="{first_name}" /&gt; {error:first_name} </code></pre> <p>Note the hidden field we submit along with the form, this allows us to ensure we only handle submission of <em>this</em> form, and not any other forms on the page, such as a login form or something. If you have more than one instance of your form on a page (say inside a channel entries loop or something), you will need to implement some trickery to make sure that you only handle the form instance which was submitted, for example by submitting along entry_id as a hidden field too.</p> <p>Glad to get that all documented, hopefully this will be useful to other EE developers too!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload