简体   繁体   English

如何在杂货杂货店中设置自定义验证消息

[英]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. 当我添加盐值小于5的盐添加记录时,将成功插入,但是当我给出大于5的盐盐时,它会显示“插入时发生错误”而不显示我的自定义消息。

What I am doing wrong ?? 我做错了什么??

Your check_salt($str) function should be like this 您的check_salt($str)函数应如下所示

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. 在set_message函数中,应提供回调函数名称“ check_salt”,而不是字段名称“ salt”。这应该可以解决您的问题。

This was the only way that I found to makes this work using: 这是我发现使用以下方法进行这项工作的唯一方法:

CI 3 and Grocery Crud 1.6.1 CI 3和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, 希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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