drwj4061 2015-11-11 19:59
浏览 40

too long

I need to display an error message for a custom validation rule, but I can't get to do it.

This is the validation rule:

    $config = array(
             ....,
            array(
                    'field' => 'general_sales_subaccount',
                    'label' => 'General Sales Subaccount',
                    'rules' => array(
                            'required',
                            'numeric',
                            array(
                                    $this->subaccounts_model,
                                    'is_valid'
                            )
                    ),
            )
    );

    $this->form_validation->set_rules($config);

And now this is the referenced model method:

    public function is_valid($subaccount)
    {
        $subaccount_num_digits = $this->preferences->get('subaccount_num_digits');

        if (strlen($subaccount) != $subaccount_num_digits ) {
            $this->form_validation->set_message('is_valid', "The number of digits in %s doesn't match the length set to " . $subaccount_num_digits);
            return false;
        }       

        return true;
    }

The rule seems to work, but it displays this error message:

Unable to access an error message corresponding to your field name (Anonymous function).

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongqiangou5724 2016-08-30 10:32
    关注

    You can't get an error message because you don't setup functions name. You may change your rule function like below:

    $config = array(
             ....,
            array(
                    'field' => 'general_sales_subaccount',
                    'label' => 'General Sales Subaccount',
                    'rules' => array(
                            'required',
                            'numeric',
                            array( //you may get all in another array
                              'is_valid', // and tell codeigniter your functions name
                              array(
                                    $this->subaccounts_model,
                                    'is_valid'
                            )
                        )
                    ),
            )
    );
    
    $this->form_validation->set_rules($config);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动