We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Front end template form validation using modue

Development and Programming

hbsinternet's avatar
hbsinternet
24 posts
10 years ago
hbsinternet's avatar hbsinternet

My requirement is how we validate and display message in front end template form using module? Below is my code

I have created a module “abc” and added function “my_form” in mod.abc.php

function my_form(){
  
  $data['hidden_fields'] = array(
   'ACT' => ee()->functions->fetch_action_id('abc', 'my_form_action'),
   'RES' => ee()->TMPL->fetch_param('results'),
  );
     
  $data['id'] = ee()->TMPL->form_id;
  $data['class'] = ee()->TMPL->form_class;

  $res  = ee()->functions->form_declaration($data);

  $res .= stripslashes(ee()->TMPL->tagdata);

  $res .= "</form>";

  return $res;
 }
 <pre><code>function my_form_action(){

ee()->load->library(‘form_validation’); ee()->form_validation->set_rules(‘firstname’,’First Name’,’trim|required|max_length[150]’); ee()->form_validation->set_rules(‘lastname’,’Last Name’,’trim|required|max_length[150]’);

ee()->view = new stdClass;
ee()->view->error = ‘ ’;

if (ee()->form_validation->run() == FALSE) {

} else {

}

ee()->functions->redirect(‘/abctemplateform’);
} In my template file “abctemplateform”

{exp:abc:my_form}
  First Name<input type="text" name="firstname" id="firstname" /></p>
  Last Name<input type="text" name="lastname" id="lastname" /></p>
  <input type="submit" name="submit" id="submit" value="Add" /></p>
 {/exp:abc:my_form}
       
Kevin Cupp's avatar
Kevin Cupp
791 posts
10 years ago
Kevin Cupp's avatar Kevin Cupp

Hi there. You basically need to get the errors out of the form validation library some how. Here’s one way. Call error_string(), like this:

$error_string = ee()->form_validation->error_string();

That will give you a string of all the error messages the form generated. You can even optionally add a prefix and suffix for each error, for instance if you wanted to display the errors in a list:

$error_string = ee()->form_validation->error_string('<li>', '</li>');

Then, just pass along that string to the template however you like. Either append it to the form declaration or create a separate tag for it.

Does that do what you need?

       
hbsinternet's avatar
hbsinternet
24 posts
10 years ago
hbsinternet's avatar hbsinternet

Thanks Kevin

Then, just pass along that string to the template however you like. Either append it to the form declaration or create a separate tag for it.

>> Please can you give a example code of both way. Thanks

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
10 years ago
Kevin Cupp's avatar Kevin Cupp

Sure, so in your first code example where you’re calling form declaration, you can do this:

$error_string = ee()->form_validation->error_string(); 

$res = ee()->functions->form_declaration($data).$error_string;

Then it will just simply appear under the form tag in the HTML.

Or, when you’re setting up the tagdata for return, send it as a variable to the Template class:

$error_string = ee()->form_validation->error_string(); 

$res .= stripslashes(ee()->TMPL->parse_variables_row(ee()->TMPL->tagdata, array('errors' => $error_string));

Then render it on the front-end as {errors} wherever you like within your tag pair.

       
hbsinternet's avatar
hbsinternet
24 posts
10 years ago
hbsinternet's avatar hbsinternet

Thanks Kevin, Now we have achieve this.

Just one thing: How we apply “set_flashdata” in module function and after redirect on template file how we get this message?

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
10 years ago
Kevin Cupp's avatar Kevin Cupp

Great!

To get the flashdata, call flashdata() instead. So if you set a message like this:

ee()->session->set_flashdata('my_message', 'Here is my message');

You can then get your stored message after a redirect like this:

$my_message = ee()->session->flashdata('my_message');
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.