使用codeigniter从模型输入验证

时间:2014-02-10 09:21:41

标签: php codeigniter

我有一个验证数据库输入的函数。 我的问题是如果当前输入小于数据库中的值,如何验证输入?

视图

<?php echo form_open('order/confirm');?>
    <div class="input-control text">
       <input type="text" name="paid_value" value="" placeholder=""/>
       <button class="btn-clear"></button>
    </div>
<?php echo form_close;?>

模型

function payment_validate(){
    $oid = $this->uri->segment(3);
    $paid = $this->input->post('paid_value');

    $this->db->select('*')
    ->from('order')
    ->join('payment','payment.order_id = order.order_id')
    ->where('order.order_id',$oid)
    ->where('order.total_bill >', $paid);

    $query = $this->db->get();

    if($query->num_rows() > 0){
    return $this->db->last_query(); //$query->result();
    }
    else{
    return array();
    }   
}

控制器

function confirm(){ // submit form payment
    if (!$this->ion_auth->logged_in()){ 
    redirect('auth/login');
    }else{
        if($this->nekogear->payment_validate()){ // set error message
            $data['bill'] = $this->nekogear->payment_validate();

            echo "<pre>";
            die(print_r($data, TRUE));
        //$this->session->set_userdata('refered_from', $_SERVER['HTTP_REFERER']);
        //echo "<script language='javascript'>alert('Insufficient fund.');
        //window.location='http://localhost/nekogear/index.php/cart/redirects'</script>";   
        }else{
        $data['goto'] = $this->nekogear->confirm_payment();

            echo "<pre>";
            die(print_r($data, TRUE));
        //echo "<script language='javascript'>alert('Accepted.');
        //window.location='http://localhost/nekogear'</script>";
        }
    }
}

如果插入的值小于当前值,则print_r结果正确查询 低于当前值

    Array
(
    [bill] => SELECT *
FROM (`order`)
WHERE `order`.`order_id` =  '52F89602E6E'
AND `order`.`total_bill` > 10000
)

如果 超过当前值

Array
(
    [bill] => Array
        (
        )

)

所以我的猜测必须在我的if - else控制器逻辑中关闭,但我无法弄清楚为什么它总是跳过这个

if($this->nekogear->payment_validate()){
  // some error message here
}

任何想法为什么?感谢。


解决方案 我将隐藏在总帐单中的表单添加到视图中,然后在我的控制器中调用它。

$total = $this->input->post('total_bill');
$paid = $this->input->post('paid_value');
if($paid < $total){
// error message
}else{
// success message
}

1 个答案:

答案 0 :(得分:1)

问题在于您的模型功能:

if($query->num_rows() > 0){
    return $this->db->last_query(); //$query->result();
    }
    else{
    return array(); <--- you are returning an empty array here.
    }  

此外,如果要验证表单,可以使用控制器中的表单验证库。说到你的情况,你可以使用回调函数。

在您的控制器中:

function confirm(){
    $this->load->library('form_validation');
     $this->form_validation->set_rules('paid_value', 'Paid value', 'required|callback_validate_payment');
     if ($this->form_validation->run() == TRUE){ 
         // yes validation is true do something
        ......
      }
}

并在控制器中指定您的回调函数,如:

public function validate_payment($str){ //$str holds the input post value
        //query here and check the value,return TRUE OR FALSE based on your condition. 
        // Also if you need you can set a custom error message here
 }

查看更多信息here