[eluser]alucard[/eluser]
Woohoo I got it working including the custom rule that wasn't working before.
Thanks guys for all the help, everything is working now.
This is how it looks now:
Code:
class MY_form_validation extends CI_Form_validation{
public $my_form;
public $field_labels = array();
public function __construct($field_labels = array()) {
$this->my_form =& get_instance();
//$this->my_form->load->library('form_validation');
$this->field_labels = $field_labels;
parent::__construct();
}
public function do_validation(){
if (isset($_POST["submit"])) {
$this->my_form->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->set_rules_();
/*
If validation passed return true else return false
*/
return $this->my_form->form_validation->run()? true: false;
}
}
public function set_rules_(){
//set validation rules
$this->my_form->form_validation->set_rules('category1', $this->field_labels['category1'], "trim|required|xss_clean");
$this->my_form->form_validation->set_rules('category2', $this->field_labels['category2'], "trim|compare_cat|xss_clean");
$this->my_form->form_validation->set_rules('title', $this->field_labels['title'], "trim|required|xss_clean");
$this->my_form->form_validation->set_rules('description', $this->field_labels['description'], "trim|valid_description|xss_clean");
$this->my_form->form_validation->set_rules('phone', $this->field_labels['phone'], "trim|required|numeric|min_length[10]|max_length[11]");
$this->my_form->form_validation->set_rules('link', $this->field_labels['link'], "trim|required|valid_url|xss_clean");
if(!(isset($_POST['online_only_business'])) && isset($_POST['address_line1'])){
$this->my_form->form_validation->set_rules('address_line1', $this->field_labels['address_line1'], "trim|required|xss_clean");
}
$this->my_form->form_validation->set_rules('address_line2', $this->field_labels['address_line2'], "trim|xss_clean");
if(!(isset($_POST['online_only_business'])) && isset($_POST['my_formty'])){
$this->my_form->form_validation->set_rules('my_formty', $this->field_labels['my_formty'], "trim|required|xss_clean");
}
if(!(isset($_POST['online_only_business'])) && isset($_POST['state'])){
$this->my_form->form_validation->set_rules('state', $this->field_labels['state'], "trim|required|xss_clean");
}
if(!(isset($_POST['online_only_business'])) && isset($_POST['post_code'])){
$this->my_form->form_validation->set_rules('post_code', $this->field_labels['post_code'], "trim|required|numeric|max_length[4]|min_length[4]|xss_clean");
}
$this->my_form->form_validation->set_rules('listing_plan_id', '', "trim|required|xss_clean");
$this->my_form->form_validation->set_rules('email', $this->field_labels['email'], "trim|required|valid_email|xss_clean");
}
function compare_cat(){//compare categories - callback function for form validation
if( !$_POST['category1']=="" &&($_POST['category1'] == $_POST['category2'])){
$this->my_form->form_validation->set_message('compare_cat','Category 1 and category 2 cannot be the same');
return false;
}
else{
return true;
}
}
function valid_url(){//check if url supplied is valid - callback function for form validation
if(!preg_match('/^((http:\/\/www\.)|(www\.)|(http:\/\/))[a-zA-Z0-9._-]+\.[a-zA-Z.]{2,5}$/', $_POST['link'])){
$this->my_form->form_validation->set_message('valid_url',"Enter a valid url, make sure to enter 'http://' first.");
return false;
}
else{
return true;
}
}
function valid_email(){
if(!preg_match( '/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $_POST['email'])){
$this->my_form->form_validation->set_message('valid_email',"Enter a valid email");
return false;
}
else{
return true;
}
}
function valid_description(){
if((isset($_POST['listing_plan_id']) && $_POST['listing_plan_id'] == 3) && str_word_count($_POST['description']) > 100){
$this->my_form->form_validation->set_message('valid_description', "For the plan you have chosen, description word count cannot be more than 100");
//echo 'more than 50';
//exit;
return false;
}
else{
return true;
}
}
}