1

I am building a module for EE2 and I want to extend CI's form validation library. I also want a separate language file too, kept within my module's directory so it's separate from future EE updates.

I created a libraries folder within my module's folder within the third_party folder.

I named the file: My_Form_validation.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    /**
     * Constructor
     *
     * @access  public
     */ 
    function __construct($rules = array())
    {   
        parent::__construct($rules);
    }

    public function is_valid($value)
    {
        $this->form_validation->set_message('is_valid', 'The %s field can not contain any spaces');
        return FALSE;
    }
}

$this->EE->form_validation->set_rules('short_name', 'Short name', 'required|max_length[30]|is_valid');

I set the rule in the mcp.module_name.php file, I also swapped out $this->EE->form_validation... with $this->CI->form_validation and nothing.

it is not erroring out, it just doesn't seem like it is loading it correctly.

I tried loading the my form validation library in the mcp.module_name.php constructor and it returns this error: Fatal error: Class 'CI_Form_validation' not found

I shouldn't have to load it since it should load along with the default form validation library.

Below is my constructor for the mcp.module_name.php file

public function __construct()
{
    $this->EE =& get_instance();
    $this->CI =& get_instance();
    $this->EE->config->load('config');
    $this->conf = $this->EE->config->item('restaurant_menu_defaults');
    $this->CI->load->helper('file');
    $this->EE->load->helper('html');
    $this->EE->load->model('restaurant_menu_model');
    $this->base = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=restaurant_menu';
    $this->base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=restaurant_menu';
    $this->EE->cp->add_to_head('<link rel="stylesheet" href="/themes/third_party/restaurant_menu/css/restaurant_menu.css" type="text/css" media="print, projection, screen" />');
    $this->base_short = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=restaurant_menu';
    $this->data['base_url_short'] = $this->base_short;
    $this->data['base_url'] = $this->base;
    $this->site_id = $this->EE->config->item('site_id');
    $this->EE->cp->set_breadcrumb($this->base, $this->EE->lang->line('Restaurant Menu'));

    // setup navigation
    $this->EE->cp->set_right_nav(array(
        'module_nav_home' => $this->base_url.AMP.'method=index',
        //'module_nav_settings' => $this->base_url.AMP.'method=settings',
    ));
}

UPDATE:

added this to my construct on mcp.module_name

$this->EE->load->library('form_validation'); $this->EE->load->library('MY_Form_validation');

then in my methods, I run the form validation using: if($this->EE->my_form_validation->run() === FALSE)

Then I set my form field rules:

$this->EE->my_form_validation->set_rules('title', 'title', 'required|max_length[75]'); $this->EE->my_form_validation->set_rules('short_name', 'Short name', 'required|is_valid|max_length[30]'); $this->EE->my_form_validation->set_error_delimiters('', '');

MY_Form_validation.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Form_validation extends EE_Form_validation {

    /**
     * Constructor
     *
     * @access  public
     */ 
    function __construct($rules = array())
    {   
        parent::__construct($rules);
        $this->EE =& get_instance();
    }

    public function is_valid($value)
    {
        $this->EE->form_validation->set_message('is_valid', 'The %s field can not contain any spaces');
        return false;
    }

}

It won't post the form unless all the rules are met, but it won't display the form error messages, why do you think that is?

1 Answer 1

5

First, no need to ever load directly from CI - you can always use EE. (So, $this->EE->load->helper('file').)

Same with your validation class - I'd extend Form_validation, not CI_Form_validation.

(Generally, EE should serve as your path to anything CI-related to avoid any conflicts.)

You'll need to load your custom validation library, and call it by its unique name as well. You may also have to load the form validation class before loading your custom library.

$this->EE->load->library('form_validation');
$this->EE->load->library('my_form_validation');
// $this->EE->my_form_validation->run();
4
  • Very close to getting this, it is not outputting the error messages, posted an update to my original post.
    – Brad
    Sep 10, 2013 at 19:26
  • You need to show the errors manually - echo $this->EE->my_form_validation->error_string() or echo $this->EE->my_form_validation->show_errors(). Sep 10, 2013 at 19:44
  • within my view? like <?php echo form_error('field_name'); ?>
    – Brad
    Sep 10, 2013 at 19:45
  • 1
    If you want to show individual, per-field errors, sure. My examples were for a global error message display (say, at the top of your view). Sep 10, 2013 at 20:10

Your Answer

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

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