わかりましたので、ユーザー登録に生年月日を実装しました。私が今やりたいことは、その生年月日を取得して、登録する前に特定の年齢 (13 歳) を超えているかどうかを確認することです。私がやったDOBのやり方はちょっと変ですが、うまくいきます。dob1、dob2、dob3 の 3 つのフィールドがあります。CodeIgniter: Tank Auth, Adding Date of Birth Issues here は、誰かが興味を持っている場合の実装方法です。とにかく、これは私がこれまで試してきたことです: 編集: ユーザーが入力する構文は 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]');
私は近いですか?私は道を外れていますか?誰か助けて?今のところ、このコールバックを作成したことがないふりをして、ユーザーが若すぎる場合でもユーザーを配置するだけです。変数に問題があったため、関数が正しく呼び出されていることはわかっています。ヘルプ?
編集:ブレンダンの答えは私を大いに助けてくれましたが、主な問題は論理エラーでした. だからここに私が今それをどのように機能させているかがあります:
//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[]');