I am trying to add a form on the front end so my users can use crud functionality and allowing me to add responsiveness to the form.

I have been pointed in the direction of an article and examples over at ee-recipes and this have given me a good starting point. But I need to add some validation to the process. Firstname and lastname are required as well as checks to so if the player being added is already in the system.

I have added a form and action and I am able to save the data into the database.

public function player_form() {

    // Build an array to hold the form's hidden fields
    $hidden_fields = array(
        "ACT" => $this->EE->functions->fetch_action_id( 'Clubmanager', 'create_club_player' )

    $action_id = ee()->functions->fetch_action_id('Clubmanager', 'create_club_player');

    // Build an array with the form data
    $form_data = array(
        "action" => "{site_url}?ACT=$action_id",
        "id" => $this->EE->TMPL->form_id,
        "class" => $this->EE->TMPL->form_class,
        "hidden_fields" => $hidden_fields

    // Fetch contents of the tag pair, ie, the form contents
    $tagdata = $this->EE->TMPL->tagdata;

    $form = $this->EE->functions->form_declaration($form_data) . 
    $tagdata . "</form>";

    return $form;

public function create_club_player() {

    $firstname = $this->EE->input->post('firstname');
    $lastname = $this->EE->input->post('lastname');
    $knownas = $this->EE->input->post('knownas');
    //$club = $this->EE->input->post('club');
    $position = $this->EE->input->post('position');
    $dob = $this->EE->input->post('dob');
    $url_title = strtolower(str_replace("'", "",$firstname)).'-'. strtolower(str_replace("'", "",$lastname));

    $data = array(
                    'firstname' => $firstname,
                    'lastname' => $lastname,
                    'knownas' => $knownas,
                    'url_title' => $url_title,
                    'position' => $position,
                    'dob' => $dob

    $this->EE->db->insert('tm_players', $data);

    $ret = $this->EE->functions->fetch_site_index();

    $data = array(
        'title' => 'Add Player',
        'content' => "Player added successfully",
        'link' => array($ret, "Back to site")


    //print_r( $_POST );

I know that I need to use form validation for the name etc and have what I need already as part of the CP module but not sure how to put it all together especially when reloading the page if there are errors. I have seen a post like this one here. page reload after postback answer by @Adrian Macneil that references ee()->core->generate_page(). Are there any simple examples for me to try and follow

BACKGROUND Some more background information for you, should answer some questions. My site was originally built on CI and added bits here and there. I had a vision of integrating the site with EE but unfortunately it wasn't straight forward or so I was told.

I paid to get my football stats into EE and with that I lost a of functionality and didn't have the knowledge or money to fix that. I ran with EE for two years but it just couldn't cope the stats and how it pulled it all together. I got lucky and teammanager module was then available and I was able to change this slightly and just plugin my database tables from my CI site.

With EE there are many ways to build websites and I am sure there are better ways to use EE for me but I do not have the expertise and just have to try to get there myself somehow.

I have control panel access for updating and adding all information including a separate one for Club Admins to add player information. As most of my users use mobiles to visit the site and I want to try and push members to use the club admin feature I thought best way would be to have responsive design rather than mess with the CP CSS or try and build a mobile app.

Unless I am wrong the CP process for forms are different from building a form for front and this is the reason why I am asking this question. My form will also require a validation routine to check whether this player exists and also allow an override for different players with the same name. I have this code already but as I said the CP form and front end form are processed differently. Hopefully this clears some questions up.

And thanks for all comments.

  • 1
    You probably have a reason here, but: have you considered just using a Channel Form and storing these submissions as channel entries? Apr 28, 2015 at 14:27
  • My custom module is for football/soccer statistics and this is just a sub part of it. Apr 28, 2015 at 14:51
  • I can't imagine how complex your stats can get, but the only time I've made an EE app with modules as an ORM, I had a frickin' 20+ page ERD to implement. Are you sure you can't just use matrix and playa and built in channels?
    – jrothafer
    Apr 28, 2015 at 21:32
  • yes i am sure i cant use matrix and playa Apr 28, 2015 at 23:00
  • If the validation fails, redirect them back using: $this->EE->functions->redirect($this->EE->functions->form_backtrack());
    – Brad
    Apr 29, 2015 at 17:38

2 Answers 2


Take a look at the Code Ignitor Validation Helper. You can use this within PHP on an EE template, it really is as easy as this (example from the CI Docs), just replacing $this with a reference to EE :

    function gmail_only($value){
        return ( stripos(strtolower($value), '@gmail.com') !== false );

    ee()->load->helper(array('form', 'url'));


    ee()->form_validation->set_rules('username', 'Username', 'required');
    ee()->form_validation->set_rules('password', 'Password', 'required');
    ee()->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
    ee()->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_gmail_only');

    if (ee()->form_validation->run() == FALSE)
        //validation failed, back to form, show error messages!
        //GREAT SUCCESS, do what you need to do!

That is literally it, in the exampe there's a form with 4 fields, username, passwrod, password confirm and email. All are required and email needs to be a valid email too. You can even use custom validation callbacks for complex things (see the docs linked above for this too).

Edit: You also get the error's array when you run form validation, so you can use that to populate inline errors ect. as for routing what I tend to do is run the submission code on the same template as the form itself and post the form back to the current URL.

This way you can create your error array if validation fails, or a success message if it all worked out, then on the template where the form is displayed you can do an if ( count($errors) ) or something to detect if you're siaplying errors, if ( !empty($success) ) or similar to display a success message and just display the form if it's not a submission (errors and success are empty). No need for redirects.

PS> When it comes to EE, I am very much a 'roll your own' kind of guy. I find the extra work of creating modules, tag parsers and routing quite erronious especially if the function your developing has very little re-usability, i.e. a specifically crafted form for a specific client/website/page.

  • Cheers for that, I know that I need to use form validation for the name etc and have what I need already. Part of my problem is reloading the page after validation. But I think I have part of this solved already and will post when I have tested. I am also looking into using jquery validation for this and have tested the first part but need to use the remote function. Apr 29, 2015 at 15:09
  • Might I suggest jquery.unobtrusive.validation? I've used this before on a different project and I recall it drops in nicely. Ass for the routing I tend to post forms back tothe page they are on, that way you can do your validation above the form, if it fails you can create an error array and process the rest of the template normally, if everything works out you can redirect to a success page / display a success message on the form. Or are you meaning the CP routing specifically?
    – Blatant
    Apr 29, 2015 at 17:18
  • Also, if you're looking for a hard and fast example of redirecting and routing for forms from modules/plug ins, I'd checkout the source code that displays and handles the channel entries form in EE, it will provide a solid example for you to use.
    – Blatant
    Apr 29, 2015 at 17:27
  • I am not sure the best way to achieve it and probably the easiest for me to do. It might be to complicated to use the jquery and have it using a remote function to check players if they exist. I have posted an example that I have been working with Andrew Weaver on and will look into all these. Apr 29, 2015 at 18:10

This is one solution I am working on, I had been looking over ee-recipes and Andrew Weaver has been giving me some code as well

Main Form

public function test_form() {

// Build an array to hold the form's hidden fields
$hidden_fields = array(
    "ACT" => ee()->functions->fetch_action_id( 'Clubmanager', 'action_form' ),
    "return" => ee()->TMPL->fetch_param('return')

// Build an array with the form data
$form_data = array(
    // Send form back to this url - the action must be the same as the form
    "action" => ee()->functions->create_url( ee()->uri->uri_string ), 
    "id" => ee()->TMPL->form_id,
    "class" => ee()->TMPL->form_class,
    "hidden_fields" => $hidden_fields

// Make sure errors is actually an array
if( ! is_array( self::$errors ) ) {
    self::$errors = array();

// Set up array to parse tagdata
$variables = array(
    "errors" => array( self::$errors ), // list of error messages
    // repopulate form with these variables
    "firstname" => isset( self::$formdata["firstname"] ) ? self::$formdata["firstname"] : "",
    "lastname" => isset( self::$formdata["lastname"] ) ?  self::$formdata["lastname"] : ""

// Fetch contents of the tag pair, ie, the form contents
$tagdata = ee()->TMPL->tagdata;

// Parse tagdata for errors/submitted values
$tagdata = ee()->TMPL->parse_variables_row( $tagdata, $variables );

// Create the form
$form = ee()->functions->form_declaration($form_data) . 
    $tagdata . 

return $form;}

Action Form

 public function action_form() {

// use CI validation library to check submission
ee()->form_validation->set_rules('firstname', 'lang:firstname', 'required');
ee()->form_validation->set_rules('lastname', 'lang:lastname', 'required');

// Fetch data from the form
$firstname = ee()->input->post("firstname");
$lastname = ee()->input->post("lastname");

    self::$errors["error"] = ee()->form_validation->error_string();

// If there are no errors, redirect to the "return" url
if( empty( self::$errors ) ) {
    ee()->functions->redirect( ee()->functions->create_url( ee()->input->get_post('return') ) );

// Store submitted data to static formdata array
self::$formdata = array(
    "firstname" => $firstname,
    "lastname" => $lastname

// Continue loading this page

Hopefully this with what has been suggested I will get there.

  • Added the CI validation and tested and works as expected. Just need to add my own validation routine. If thinks there is a better way to write this please let me know. Thanks Apr 29, 2015 at 21:17
  • I've always used these style of callbacks here : ellislab.com/codeigniter/user-guide/libraries/… . You can write a function near the validation code that returns the result of a preg_match or something, then its just adding callback_FUNCTIONNAME inside your setRules statements. I'll add a simple example above for you.
    – Blatant
    Apr 30, 2015 at 9:15
  • @blatant cheers I have used them in the past in ci but wasn't able to figure it out on ee. And will be something stupid I will be doing. Apr 30, 2015 at 9:22
  • Can you add your custom validation to your example on your action form above? it should just work with a function above action_form() (lets say 'validate_name' and adding required|callback_validate_name to your set_rules... Is it not finding the validation function properly??
    – Blatant
    Apr 30, 2015 at 9:26
  • I have added my own validation and tested and it works fine. So I am happy this works for me now. Apr 30, 2015 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.