Php Codeigniter表单验证大于字段1且小于字段2
如何在Codeigniter中基于其他字段创建表单验证,例如,我有两个字段字段字段,其中字段字段必须小于字段,字段必须大于字段Php Codeigniter表单验证大于字段1且小于字段2,php,codeigniter,Php,Codeigniter,如何在Codeigniter中基于其他字段创建表单验证,例如,我有两个字段字段字段,其中字段字段必须小于字段,字段必须大于字段 $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]');
$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]');
我的代码不工作,错误总是显示出来
“字段2必须大于字段1”
但我输入正确的方式
第一场1
第二场4
如何解决这个问题?请帮帮我 像这样试试看
$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').']');
和回调为:
function check_equal_less($second_field,$first_field)
{ if ($second_field <= $first_field) { $this->form_validation->set_message('check_equal_less', 'The First &/or Second fields have errors.');
return false; }
else { return true; }
}
像这样试试
$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').']');
和回调为:
function check_equal_less($second_field,$first_field)
{ if ($second_field <= $first_field) { $this->form_validation->set_message('check_equal_less', 'The First &/or Second fields have errors.');
return false; }
else { return true; }
}
而不是
'greater_than[field_one]'
使用
我只是试了一下,效果不错。多亏了Aritra而不是
'greater_than[field_one]'
使用
我只是试了一下,效果不错。由于Aritra本机大于方法需要数字输入,因此我们不能直接使用大于[field\u one]。但是我们可以定制一种方法来实现这个目标 我的方法如下:
/* 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;
}
}
所有验证数据都保存在受保护的变量$字段\数据中,值保存在key postdata中,因此我们可以获取所需字段的值
当我们使用上述方法时,我们可以使用“大于等于等于字段[field\u one]”在两个字段之间进行验证
一个很好的参考-本机表单验证方法匹配和不同。你可以登记
本机的大于方法需要数字输入,因此我们不能直接使用大于[field\u one]。但是我们可以定制一种方法来实现这个目标 我的方法如下:
/* 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;
}
}
所有验证数据都保存在受保护的变量$字段\数据中,值保存在key postdata中,因此我们可以获取所需字段的值
当我们使用上述方法时,我们可以使用“大于等于等于字段[field\u one]”在两个字段之间进行验证
一个很好的参考-本机表单验证方法匹配和不同。你可以登记
你已经在实际中设置了你的规则,或者这是一个打字错误_than@pradeep现在更正了,对不起,我没有注意到,你能帮我吗?大于或小于接受int作为其参数,而不是字段项,只需在其中提供int值。您实际设置了您的规则,或者这是一个更大的打字错误_than@pradeep现在更正,抱歉我没注意到,你能帮我吗?大于或小于接受int作为参数而不是字段项只提供其中的int值