0

在这方面需要帮助:

这是我的示例代码,
如果发生 preg_match,我想添加一条验证消息:
请。有关更多详细信息,请参阅内联评论..

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

提前致谢..

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

1 回答 1

1

使用内置的表单验证。

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

然后在视图中显示验证错误:

<?php echo validation_errors(); ?>

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

于 2013-01-05T15:32:19.557 回答