简体   繁体   English

Codeigniter表单验证大于第一个字段,小于第二个字段

[英]Codeigniter form validation greater than field one and less than field two

How to create form validation in Codeigniter base on other field, for example I have two field (field_one, and field_two) where field_one is must be less_than field_two and field_to must be greater_than field_one. 如何在Codeigniter中基于其他字段创建表单验证,例如我有两个字段(field_one和field_two),其中field_one必须小于field_two,field_to必须大于field_one。

$this->form_validation->set_rules('field_one', 'Field One', 'less_than[field_two]');

$this->form_validation->set_rules('field_two', 'Field Two', 'greater_than[field_one]');

my code is doesn't work, the error always displaying 我的代码不起作用,错误始终显示

'Field two must be greater than field one' “第二场必须大于第一场”

but I input the right way, 但我输入正确的方式

field one 1 field two 4 场一1场二4

how to solve this? 如何解决呢? Plz help me! 请帮我!

Try this like 像这样尝试

    $this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural'); 
$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero|callback_check_equal_less['.$this->input->post('first_field').']');

And callback as : 和回调为:

 function check_equal_less($second_field,$first_field) 
{ if ($second_field <= $first_field) { $this->form_validation->set_message('check_equal_less', 'The First &amp;/or Second fields have errors.'); 
return false; }
 else { return true; } 
}

Instead of 代替

'greater_than[field_one]'

use 采用

'greater_than['.$this->input->post('field_one').']'

I just tried it out and it works. 我只是尝试了一下,而且效果很好。 Thanks to Aritra 感谢阿里特拉

Native greater_than method need a numeric input, so we can't use greater_than[field_one] directly. 本机的great_than方法需要数字输入,因此我们不能直接使用Greater_than [field_one]。 But we can make a custom method to achieve the goal. 但是我们可以通过自定义方法来实现目标。

My way are as follows: 我的方法如下:

/* A sub class for validation. */
class MY_Form_validation extends CI_Form_validation {

    /* Method: get value from a field */
    protected function _get_field_value($field)
    {
        return isset($this->_field_data[$field]["postdata"])?$this->_field_data[$field]["postdata"]:null;
    }

    /* Compare Method: $str should >= value of $field */
    public function greater_than_equal_to_field($str, $field)
    {
        $value = $this->_get_field_value($field);
        return is_numeric($str)&&is_numeric($value) ? ($str >= $value) : FALSE;
    }
}

All validation data save in the protected variable $_field_data, and value save in the key "postdata", so we can take the value of the field we want. 所有验证数据都保存在受保护的变量$ _field_data中,值保存在键“ postdata”中,因此我们可以获取所需字段的值。

When we have above method, we can use 'greater_than_equal_to_field[field_one]' to do the validation between two fields. 当我们拥有上述方法时,我们可以使用'greater_than_equal_to_field [field_one]'在两个字段之间进行验证。

  • A good reference - Native form validation methods matches and differs. 一个很好的参考-本机表单验证方法匹配并且有所不同。 You can check it in CI_Form_validation 您可以在CI_Form_validation中进行检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM