0

need help on this one:

here's a my sample code,
i would like to add a validation message if a preg_match occurs:
pls. see inline comments for more details..

public function supplier_entry()
{
    if (preg_match("/[\'^£@&*...etc.../", $this->input->post('supplier')))
    {  

    //add or pass validation message, ex. $msg = 'Invalid Supplier Name';  
    // i tried $this->supplier_entry_form($msg); but its not working.  

        $this->supplier_entry_form();

    }else{
        $post_data = array(
        'supplier_name' =>$this->input->post('supplier'),
        'user' => $this->input->post('user'),       
        'trx_id' =>$this->input->post('trx_id'),
        );  

        $this->load->model('user_model');
        $this->load->model('product_model');

        $this->product_model->add_new_supplier($post_data);
        $user_data['trx'] = 'Supplier Entry';
        $user_data['username'] = $this->user_model->user_info();
        $trx_data['supplier'] = $this->product_model->get_supplier_list();  
        $trx_data['msg'] = 'Supplier Posted.';  


        $this->load->view('header',$user_data);     
        $this->load->view('item_supplier', $trx_data);              
    }
}  

thanks in advance..

    public function supplier_entry_form()
{
    $this->load->model('user_model');
    $this->load->model('product_model');

    $user_data['username'] = $this->user_model->user_info();
    $user_data['trx'] = 'Supplier Entry';   
    $trx_data['supplier'] = $this->product_model->get_supplier_list();

    $this->load->view('header', $user_data);                      
    $this->load->view('item_supplier', $trx_data);
}
2
  • Can you post the code for the method supplier_entry_form()? Commented Jan 5, 2013 at 15:01
  • Use the form_validation and a callback function, it's laid out pretty clearly in the user guide. You just make the preg match it's own function then call that function in the form validation. Commented Jan 5, 2013 at 15:27

1 Answer 1

1

Use the built in form validation.

$this->load->library('form_validation');
$this->form_validation->set_rules($this->input->post('supplier'), 'Supplier', 'trim|callback_pregMatchSupplier');
if($this->form_validation->run()==FALSE)
{
    $this->supplier_entry_from();
}
// continue code here if validation passes.


function pregMatchSupplier()
{
    if (preg_match("/[\'^£@&*...etc.../", $this->input->post('supplier')))
    {
       return FALSE;
    } else {
       return TRUE;
    }

Then in the view you echo out the validation errors:

<?php echo validation_errors(); ?>

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.