简体   繁体   中英

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

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.

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM