• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form validation callback problem

#1
[eluser]Unknown[/eluser]
The below code doesn't work. I'm trying to prevent the user from deleting more than 25% of a block of text at one time.

However, for some reason if I pass the $id as the 2nd parameter to my_min_length() and then get the old text within the function, it works fine.

I don't want to do that though because I need to add 2 more custom rules which will also require the old text so I would need to make additional db calls.

Thanks in advance for any help.

Code:
function my_min_length($new_text, $old_text)
{
// SET MAX DELETE PERCENT:
$max_delete = .25;

// Test and return
if (strlen($new_text) < strlen($old_text)*(1-$max_delete))
{
  $this->form_validation->set_message('my_min_length', 'You can not delete
  more than 25% of the text at one time.');
  return FALSE;
}
else
{
  return TRUE;
}
}

function validate_new_text($id) {

// Get old text
$old_text = $this->Maintenance_model->get_old_text($id);
  
// Set rules
$this->form_validation->set_rules('new_text', 'New Text', "trim|required|callback_my_min_length[$old_text]");

return $this->form_validation->run();
}

#2
[eluser]Aken[/eluser]
Add your old text as a property in the class, so you can then access it from any callback function as $this->old_text or something similar.

#3
[eluser]Unknown[/eluser]
Thanks! That's the kind of advice that best comes from another person.

I sat there staring at the 2 methods without ever zooming out and thinking of the class as a whole.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.