swordfish
swordfish

Reputation: 191

execute another function within function with data variables

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);
}

Upvotes: 0

Views: 34

Answers (1)

Rick Calder
Rick Calder

Reputation: 18695

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

Upvotes: 1

Related Questions