2014-02-10 34 views
1

我有一個函數驗證來自數據庫的輸入。 我的問題是如果當前輸入小於數據庫中的值,如何驗證輸入?從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>"; 
     } 
    } 
} 

如果插入VAL UE比當前值少,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

的問題是在你的模型功能:

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

+0

感謝您的想法,我目前的解決方案是調用$ total_bill帶隱藏表單的提交表單,然後檢查值是否小於$ total_bill值。 – ceroberoz