0
$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 ??

Ishrak
  • 71
  • 6
  • 18

2 Answers2

0

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.

Ariful Haque
  • 3,662
  • 5
  • 37
  • 59
  • I tried by updating my check_salt function i.e. added the else condition but I get the following message. "Unable to access an error message corresponding to your field name." @Ariful Haque – Ishrak Dec 31 '14 at 10:00
  • Did you try 'check_salt' instead of 'salt' in your set_message function? I just found this [reference](http://stackoverflow.com/a/10613125/2627842) @jishan – Ariful Haque Dec 31 '14 at 10:08
  • worked ok after using check_salt in the set_message function @Ariful Haque – Ishrak Dec 31 '14 at 10:20
  • Good to know it worked. Accepting the answer formally will be appreciated which will help other users too. @jishan – Ariful Haque Dec 31 '14 at 10:35
0

This was the only way that I found to makes this work using:

CI 3 and Grocery Crud 1.6.1

$crud->set_rules('name', 'Name', array(
                'required',
                array(
                    'company_check',
                    function ($str) {
                        $company = $this->Company_model->searchCompanyByName($str);
                        if (count($company) > 0) {
                            $this->form_validation->set_message('company_check', 'Error, The company already exist.');
                            return false;
                        } else {
                            return true;
                        }
                    }
                )
            ));

Hope this help,

masmerino
  • 957
  • 9
  • 8