Welcome Guest, Not a member yet? Register   Sign In
Need help validating a special field
#1

[eluser]php_princess[/eluser]
With my signup form, my users enter the day, month, and year of their birthdate separately. PHP is what puts these three strings together into one string called $birthdate. I wrote a validation check that looks at a birthdate and sees if the person is at least 13 years old. However, I can't use it with $this->form_validation->set_rules() because that method only looks in $this->input->post(). How can I get it to look in $birthdate? Here is my relevant code:

Where the three strings are put together in my controller:
Code:
$birthdate = $this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_day');

The validation check in MY_form_validation.php
Code:
public function check_age($age, $minimum) //returns false if personis under the minimum specified age
{  
  $cur_date = new DateTime(date('Y-m-d', mktime()));
  $birth_date= new DateTime($age);
  $interval = $cur_date->diff($birth_date);
  
      return ( $interval->y >= $minimum) ? FALSE : TRUE;
}

check_age is going to accept input like this:
Code:
$this->form_validation->set_rules('birthdate', 'Birth Date', 'check_age[13]')
**Actually it also checks a little more than this but I wanted to keep my post simple and to the point.
#2

[eluser]PhilTem[/eluser]
Code:
public function check_age($minimum) //returns false if personis under the minimum specified age
{
  $birthdate = $this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_day');  
  $cur_date = new DateTime(date('Y-m-d', mktime()));
  $birth_date = new DateTime(strtotime($birthdate));
  $interval = $cur_date->diff($birth_date);
  
  return ( $interval->y >= $minimum) ? FALSE : TRUE;
}

Note: I haven't yet worked with DateTime, so I'm not sure whether my code around $birth_date =... is correct.

But anyway, you would just check the following:
Create the users birthday from the post-inputs, create a DateTime of today, and then just compare that these have a difference of more than 13. Should be that simple.
#3

[eluser]php_princess[/eluser]
How do I get the form validator to run that code? It won't run it because I don't have a field called birthdate.
#4

[eluser]PhilTem[/eluser]
You can have the form-validation assigned to any post variable that will be coming from your form. That said, you can have it assigned to any of the three fields (but then you want to put it last in the 'rules' part of the form-validation config) or some other form input field.

E.g.
Code:
$this->form_validation->set_rules('birth_year', 'Birth Date', 'required|callback_check_age[13]')
#5

[eluser]php_princess[/eluser]
Changed the birth_year line to this:
Code:
$this->form_validation->set_rules('birth_year', 'Birth Year', 'required|check_age[13]|one_of_these['.$acceptable['birth_year'].']');

(one_of_these accepts an array and checks to make sure the entered value is in the array. In this case of birth year the array contains a list of all years between now and 1900.)

Well, now I'm getting this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: MY_Form_validation::$input

Filename: libraries/MY_Form_validation.php

Line Number: 106

Along with this one:
Quote:Fatal error: Call to a member function post() on a non-object in /home/mysitenamehere/application/libraries/MY_Form_validation.php on line 106

Showing you line 106:
Code:
public function check_age($age, $minimum)
{  
  $cur_date = new DateTime(date('Y-m-d', mktime()));
  $birthdate = $this->input->post('birth_year').'-'.$this->input->post('birth_month').'-'.$this->input->post('birth_day'); //line 106
  $birthdate= new DateTime($birthdate);
  $interval = $cur_date->diff($birthdate);
  
      return ( $interval->y >= $minimum) ? FALSE : TRUE;
}
#6

[eluser]PhilTem[/eluser]
Does your MY_Form_validation extend CI_Form_validation? And do you call the __construct() method in MY_Form_validation? Is so, do you also call parent::__construct()?
#7

[eluser]php_princess[/eluser]
Here's the top few lines of the class:

Code:
class MY_Form_validation extends CI_Form_validation {

public function __construct()
{
  parent::__construct();
}

Is it right?
#8

[eluser]PhilTem[/eluser]
Oh, my bad. Form_validation is a library not a controller or model. That's why it does not support calling any CI object directly via $this.

You need to change
Code:
$this->input

to
Code:
$this->CI->input

and then it'll work Wink
#9

[eluser]php_princess[/eluser]
Is this right:
Code:
$birthdate = $this->CI->input('birth_year').'-'.$this->CI->input('birth_month').'-'.$this->CI->input('birth_day');

Because it's saying:
Quote:Fatal error: Call to undefined method Auth::input() in /home/mysitenamehere/application/libraries/MY_Form_validation.php on line 106

Also tried this:
Code:
$birthdate = $this->CI->post('birth_year').'-'.$this->CI->post('birth_month').'-'.$this->CI->post('birth_day');

Should I just use $_POST['birth_year'] etc. The input values get checked by this stuff:
Code:
$this->form_validation->set_rules('birth_month', 'Birth Month', 'required|one_of_these['.$acceptable['birth_month'].']');
  $this->form_validation->set_rules('birth_year', 'Birth Year', 'required|check_age[13]|one_of_these['.$acceptable['birth_year'].']');
  $this->form_validation->set_rules('birth_day', 'Birth Day', 'required|one_of_these['.$acceptable['birth_day'].']');

This is one_of_these():
Code:
/**
  * One of These : Sees if entered value is in a list of acceptable values.
  *
  * @access public
  * @params string, array or string
  * @return bool
  */
public function one_of_these($current_str, $acceptable)
{  
  if ( ! is_array($acceptable)) $acceptable = explode(',', $acceptable);
  
      return ( ! in_array($current_str, $acceptable)) ? FALSE : TRUE;
}

Just for your reference, here is one_of_these() getting sent a string input for $acceptable:
Code:
$this->form_validation->set_rules('gender', 'Gender', 'required|one_of_these[male,female]');
#10

[eluser]PhilTem[/eluser]
It must be

Code:
$birthdate = $this->CI->input->post('birth_year').'-'.$this->CI->input->post('birth_month').'-'.$this->CI->input->post('birth_day');

note the CI->input->post()




Theme © iAndrew 2016 - Forum software by © MyBB