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}
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?
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.