Ishrak Ishrak - 2 months ago 49
PHP Question

how to set custom validation message in grocery crud

$crud = new grocery_CRUD();
$crud->set_table('generate_eblskyid');
$crud->set_rules('salt', 'Salt Code','callback_check_salt');
$output = $crud->render();


then in the call back function i did the following

function check_salt($str)
{
$salt = $_POST['salt'];
if($salt > 5)
{
$this->get_form_validation()->set_message('salt',"Salt value must be less then FIVE");
return FALSE;
}
}


When I go to add record if I give a salt value below five the is inserted successfully but when I give a value greater then five it says "An error has occurred in insert" without displaying my custom message.

What I am doing wrong ??

Answer

Your check_salt($str) function should be like this

function check_salt($str)
{

   if($str > 5)
   {
      $this->form_validation->set_message('check_salt',"Salt value must be less then FIVE");

      return false;
   }else{
      return true;
   }
}

In set_message function, the callback function name 'check_salt' should be given, not the field name 'salt' This should solve your problem.

Comments