I have a form with two fields
<input type="text" name="total_plots" value="" placeholder="Enter Total plots" />
<input type="text" name="available_plots" value="" placeholder="Enter Available Plots " />
Available plot "available_plots" field value should be less than total plots "total_plots" field value
I don't want to write callbacks. I want to extend the form validation rule.
How to ?
MY_Form_validation
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
$this->CI =& get_instance();
}
public function check_avail($str)
{
$this->CI->form_validation->set_message('check_avail', 'Available plot should be less than Total plot.');
$total_plots = $this->CI->input->post('total_plots');
//echo '------'.$total_plots;
//echo '------'.$str;
if($str > $total_plots){
return false;
}
}
} // class
I have written rules in config
<?php
$config['plot_settings'] = array(
array(
'field' => 'total_plots',
'label' => 'Total Plots',
'rules' => 'trim|xss_clean'
),
array(
'field' => 'available_plots',
'label' => 'Available Plots',
'rules' => 'trim|xss_clean|check_avail'
)
);
?>
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Plot extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('Admin_layout');
$this->load->model('admin/plot_model');
$this->config->load('plot_rules');
$this->output->enable_profiler(TRUE);
$this->new_name='';
}
public function add(){
$this->form_validation->set_rules($this->config->item('plot_settings'));
$this->form_validation->set_error_delimiters('<p><b>', '</b></p>');
if ($this->form_validation->run('submit') == FALSE )
{
$this->admin_layout->set_title('Post Plot');
$this->admin_layout->view('admin/post_plot');
}
}//add
}
I think you can do this without writing a callback or extending the validation rule.
CI already provides a validation rule to check for less_than value.
$total_plots = $this->input->post('total_plots');
$this->form_validation->set_rules('available_plots', 'Available Plots', "less_than[$total_plots]");
It should work.