Welcome Guest, Not a member yet? Register   Sign In
custom form validation
#1

[eluser]andieevans[/eluser]
Hi

I have some code which check for a number of records in a database before allowing the form data to be added...

now I have a check box which once ticked on the form and the user submits, the form data needs to be added to a table, but only if there are less than x records...

using the code below, how do i get the userid into the function?



Code:
if ($this->input->post('mycheckbox'))
  {
   $this->form_validation->set_rules('mycheckbox','My Checkbox','callback_count_records');
  }


Code:
public function count_records($uid=0)
{
  $q = $this->db->query("SELECT * FROM mytable WHERE userid = ".$uid );
  $c = $q->num_rows();
  
  if ($c > 19)
   return false;
  else
   return true;
  
}


#2

[eluser]CroNiX[/eluser]
You can pass it in as the optional second parameter using the square brackets.

Code:
public function count_records($fieldvalue, $uid)
{
  $q = $this->db->query("SELECT * FROM mytable WHERE userid = ".$uid );
  $c = $q->num_rows();
  
  if ($c > 19)
   return false;
  else
   return true;
  
}
Then you would:
Code:
$this->form_validation->set_rules('mycheckbox','My Checkbox','callback_count_records[14]');  //14 is uid

Or, you could just create a hidden field on your form with the uid and in your count_records() validation function, you can retrieve it via $input::post('uid').
#3

[eluser]andieevans[/eluser]
hi

thanks for the reply... what is the $fieldvalue for?

and can i put a variable in the square brackets?

eg:

'callback_some_function[$myid]'

or

'callback_some_function['.$myid.']'

;o)
#4

[eluser]CroNiX[/eluser]
The first parameter of a callback function is the value of the field that was submitted. If the form fieldname was "First Name" and they submitted the form after entering "chris" in the first name field, then $fieldvalue would be "chris". It's the thing your validation rule is validating against.

The second, optional, parameter is to pass extra data/parameters to your validation function. To pass it from the rules definition, you just place whatever value you are placing in the square brackets, just like in my example.

It should be self explanatory if you study the code I posted.
#5

[eluser]andieevans[/eluser]
Hi

I have looked at your code, and can't see where you are using $fieldvalue? is the value of $fieldvalue passed to $uid?, the call to the function only has one property whereas your function has 2 properties?

is $fieldvalue a codeigniter 'default' ?


Code:
public function count_records($fieldvalue, $uid)
{
  $q = $this->db->query("SELECT * FROM mytable WHERE userid = ".$uid );
  $c = $q->num_rows();
  
  if ($c > 19)
   return false;
  else
   return true;
  
}
#6

[eluser]CroNiX[/eluser]
Yes, it's a default. Read the "callback" section of the Form Validation docs.

In this particular callback, it isn't actually used, but it still gets automatically passed to the function as the first parameter of the callback function by the validation class when it's executing that rule. How else can you validate values in a callback if the value doesn't get sent?




Theme © iAndrew 2016 - Forum software by © MyBB