【问题标题】:Unable to access an error message corresponding to your field name in codeigniter 4无法访问与您在 codeigniter 4 中的字段名称对应的错误消息
【发布时间】:2022-01-05 15:36:48
【问题描述】:

我正在尝试设置表单验证规则。它说无法访问与您的字段名称客户 (validate_customer) 对应的错误消息。对此的任何帮助将不胜感激。

$this->form_validation->set_rules('customer_id', 'Customer' ,'required|callback_validate_customer');

这是我的验证方法。

function validate_customer() {
if((double)$this->input->post('paid_amount') == 0)
{
   $this->form_validation->set_message('validate_customer' , 'Can not sale.');
   return FALSE;
} else {
   return TRUE;
}
} 

【问题讨论】:

    标签: forms codeigniter validation


    【解决方案1】:

    这些方法在 Codeigniter 4 中不再是 set_messageform_validationcallback_。为此,您需要使用 custom rule

    例子:

    <?php
    namespace App\Validation;
    
    class CustomRules{
        public function customerValidation( ... , array $data)){
    
        }  
    }
    

    并像使用它

    "customer_id" => "required|customerValidation[customer_id]"
    

    检查这些

    1. Creating Custom Rules
    2. How to Create Custom Validation Rule in CodeIgniter 4

    【讨论】:

    • 谢谢阿卜杜拉。我得到了 CI 4。但我在 CI 3.1.4 上得到了相同的错误消息。你能帮我解决这个问题吗?
    【解决方案2】:

    最后这段代码对我有用。

    $this->form_validation->set_rules(
                    'customer_id', 'Customer',
                    'required|min_length[1]|max_length[12]|customer_due_check',
                    array(
                            'required'      => 'You have not provided %s.',
                            'customer_due_check'     => 'This %s already exists.'
                    )
            );
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多