好的,所以我已经在用户注册中实现了出生日期。我现在想做的是,在他们登记之前,取他们的出生日期,检查他们是否超过了一定的年龄(13岁)。他们对道布的方式有点奇怪,但很管用。我有3个字段dob1,dob2,dob3。CodeIgniter: Tank Auth, Adding Date of Birth Issues如果有人感兴趣的话,这里是我如何实现它的。无论如何,这就是我到目前为止一直在尝试的: EDIT:用户输入的语法是mm dd yyyy
function is_old_enough($input, $dob1, $dob2) {
$dob = $dob1.$dob2.$input;
$date = date('md').(date('Y')-13);
if ((int)$dob < (int)$date)
$this->form_validation->set_message('is_old_enough', 'You are not old enough to have an account on this site.');
return $input;
}下面是register()函数中的内容。
$this->form_validation->set_rules('dob1', 'Date of Birth Month', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob2', 'Date of Birth Day', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob3', 'Date of Birth Year', 'trim|required|xss_clean|exact_length[4]|callback_is_old_enough[dob1||dob2]');我说得对吗?我说得太离谱了吗?有人能帮上忙吗?现在,它所做的一切就是假装我从来没有创建过这个回调,并将用户放入其中,即使用户太年轻。我知道它调用函数是正确的,因为我在变量方面有一些问题。帮助?
编辑:Brendan的回答对我帮助很大,但主要问题是逻辑错误。下面是我现在的工作原理:
//Check if user is old enough
function is_old_enough($input) {
$dob = $this->input->post('dob3').$this->input->post('dob1').$this->input->post('dob2');
$date = (date('Y')-13).date('md');
if ((int)$dob > (int)$date) {
$this->form_validation->set_message('is_old_enough', 'You are not old enough to register on this site.');
return FALSE;
}
return TRUE;
}
$this->form_validation->set_rules('dob1', 'Date of Birth Month', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob2', 'Date of Birth Day', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob3', 'Date of Birth Year', 'trim|required|xss_clean|exact_length[4]|callback_is_old_enough[]');发布于 2012-09-15 04:37:16
首先,在回调中最多只能传递两个参数。其次,如果从回调中返回一个非布尔值,则返回的任何值都将替换运行回调的字段的值。
如果你想检查某些东西是否有效,它的工作方式(本质上)是:
function _callback_for_field($input)
{
// check if $input is valid based on your own logic
if($input == YOUR_LOGIC_HERE)
{
return TRUE;
}
return FALSE;
}但是对于你正在做的事情,特别是:
// Birthdate rules
$this->form_validation->set_rules('birthdate-month','Birthdate Month','required|is_natural_no_zero|greater_than[0]|less_than[13]');
$this->form_validation->set_rules('birthdate-day','Birthdate Day','required|is_natural_no_zero|greater_than[0]|less_than[32]');
$this->form_validation->set_rules('birthdate-year','Birthdate Year','required|is_natural_no_zero|greater_than[1930]|less_than['.(date("Y") - 18).']');我故意不会花很大力气去阻止不到18岁的人注册,因为如果他们想注册,他们无论如何都会这样做的。如果一个人下定决心要限制基于年龄的注册是不可能的,而且由于你没有检查一些关于公民的政府数据库,这真的不在你的责任范围之内。只需要一个简单的年龄检查即可。
我有了另一个想法--如果您仍然希望进行准确的检查,那么可以为回调函数中的每个字段引用$this->input->post()。您甚至可以运行不带参数的回调函数,因为您将绕过该限制。
https://stackoverflow.com/questions/12430545
复制相似问题