2014-07-25 107 views
0

这是我form_validation扩展类:笨 - 自定义的验证

<?php 
class MY_Form_validation extends CI_Form_validation 
{ 
    function __construct($config = array()) { 
     parent::__construct($config); 
    } 
    function count_errors(){ 
     if (count($this->_error_array) === 0){ 
      return 0; 
     } 
     else 
      return count($this->_error_array); 
    } 

    public function max_number($num, $val) { 
     if($num > $val){ 
      $this->set_message("max_number", "The %s can not be greater then " . $val); 
      return false; 
     } 
    } 

    public function min_number($num, $val) { 
     if($num < $val){ 
      $this->set_message("min_number", "The %s can not be smaller then " . $num); 
      return false; 
     } 
    } 

    public function error_array(){ 
     return $this->_error_array; 
    } 
} 

然后在控制器我设置的规则是这样,例如“编号”字段:

$this->form_validation->set_rules('num', "Number", 'required|numeric|min_number[1]|max_number[99]'); 

例如56遍但也1903.

但如果num字段是0它的作品。

所以,这只适用于min_num,但不适用于max_num。

我在做什么错在这里?

回答

1

笨已经为这个内置的功能,greater_than[1]less_than[99]

但是,让你的工作的功能,你需要返回true(当然,!== FALSE)即

public function max_number($num, $val) { 
    if($num > $val){ 
     $this->set_message("max_number", "The %s can not be greater then " . $val); 
     return false; 
    } 

    return true; 
} 

public function min_number($num, $val) { 
    if($num < $val){ 
     $this->set_message("min_number", "The %s can not be smaller then " . $num); 
     return false; 
    } 

    return true; 
} 

希望这可以帮助!