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?