Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter 2的自定义表单验证错误消息_Php_Codeigniter_Validation - Fatal编程技术网

Php Codeigniter 2的自定义表单验证错误消息

Php Codeigniter 2的自定义表单验证错误消息,php,codeigniter,validation,Php,Codeigniter,Validation,我有一个名为business\u id的下拉列表 <select name="business_id"> <option value="0">Select Business</option> More options... </select> 问题在于,错误消息显示:业务字段必须包含大于0的数字。不是很直观!我想说你必须选择一家公司 我试过: $this->form_validation->set_message('Bus

我有一个名为business\u id的下拉列表

<select name="business_id"> 
    <option value="0">Select Business</option> More options... 
</select>
问题在于,错误消息显示:业务字段必须包含大于0的数字。不是很直观!我想说你必须选择一家公司

我试过:

$this->form_validation->set_message('Business', 'You must select a business');

但CI complete忽略了这一点。有人对此有解决方案吗?

请尝试不在默认的“选择”菜单上设置“值”属性

<select name="business_id"> 
    <option value>Select Business</option> More options... 
</select>   
我想你也可以尝试编辑你试图设置消息的方式

$this->form_validation->set_message('business_id', 'You must select a business');
instead of
$this->form_validation->set_message('Business', 'You must select a business');

我不完全确定这是否会奏效。

您应该像Anthony所说的那样扩展表单验证库

例如,我在一个名为MY_Form_validation.php的文件中执行类似的操作,该文件应该放在/application/libraries上

在您的情况下,因为您的第一个选项指导选项-请选择。。。如果值为0,则可能需要将条件语句从-1更改为0。然后,从现在开始,您可以使用以下行检查选择值:

$this->form_validation->set_rules('business_id', 'Business', 'has_selection');

希望这有帮助

下面是我使用的一个简单的CI2回调函数。我想要的不仅仅是“必需”作为验证的默认参数。文档有助于:


对于您的情况,您可以将回调更改为检查$str我是否有相同的添加要求,例如,您必须同意我们的条款和条件。当然,覆盖require和morther_的错误消息是错误的,因为它会错误地为表单的其余部分生成消息。我扩展了CI_Form_验证类,并重写了set_rules方法以接受新的“message”参数:

<?php

class MY_Form_validation extends CI_Form_validation
{
    private $_custom_field_errors = array();

    public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
    {
        // Execute the parent method from CI_Form_validation.
        parent::_execute($row, $rules, $postdata, $cycles);

        // Override any error messages for the current field.
        if (isset($this->_error_array[$row['field']])
            && isset($this->_custom_field_errors[$row['field']]))
        {
            $message = str_replace(
                '%s',
                !empty($row['label']) ? $row['label'] : $row['field'],
                $this->_custom_field_errors[$row['field']]);

            $this->_error_array[$row['field']] = $message;
            $this->_field_data[$row['field']]['error'] = $message;
        }
    }

    public function set_rules($field, $label = '', $rules = '', $message = '')
    {
        $rules = parent::set_rules($field, $label, $rules);

        if (!empty($message))
        {
            $this->_custom_field_errors[$field] = $message;
        }

        return $rules;
    }
}

?>

您也可以在自定义消息中使用“%s”,它将自动填充fieldname的标签。

我用一个简单的函数扩展了form\u验证库,该函数确保下拉框没有选择默认值。希望这有帮助

application/libraries/MY_Form_validation.php

如何使用: 1制作您的表单下拉框:

<select name="business_id"> 
    <option value="select">Select Business</option> More options... 
</select>
3设置自定义消息:或跳过此步骤并使用语言文件中的步骤

$this->form_validation->set_message('business_id', 'You must select a business');

一个小黑客可能对你不好,但我这样做是为了一个小小的改变

例如, 我想更改消息“电子邮件字段必须是唯一值”

我做这件事是因为

<?php
$error = form_error('email');
echo str_replace('field must be a unique value', 'is already in use.', $error); 
// str_replace('string to search/compare', 'string to replace with', 'string to search in')
?>

如果找到字符串,则它将打印我们的自定义消息,否则它将显示错误消息,如“电子邮件字段必须是有效电子邮件”等。

如果您要自定义随每个规则显示的错误消息,您可以在以下位置的数组中找到它们:

/system/language/english/form_validation_lang.php

创建方法username\u check回调函数

1 - 2然后将此验证代码放在类上

$this->form_validation->set_rules('number_adults', 'Label Name','Your Message',) 'callback_username_check');

这可能会帮助您

对于那些正在使用CodeIgniter 3的用户,您可以执行以下操作:

$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', array(
'greater_than' => 'You must select a business',
));

如果您使用的是CodeIgniter 2,则需要扩展和覆盖CI_Form_验证类,以获取有关如何使用新的CodeIgniter 3 CI_Form_验证类和使用上述函数的更多信息。

规则的名称是最后一个参数

请尝试:

$this->form_validation->set_message('greater_than[0]', 'You must select a business');
更多信息:

我已经尝试了你的第二个建议,但失败了。你的第一个建议会奏效,但我觉得如果我使用它,我会屈服于代码。在仔细查看文档后,我仍然希望设置自定义错误消息。看起来传递给set_message的第一个参数应该是规则名而不是字段名。尝试将大于或必需的作为第一个参数传递,以根据您决定使用哪个规则来覆盖默认消息来设置消息。成功了$此->表单验证->设置消息“大于”,“您必须选择一个业务”;非$this->form_validation->set_message'better_than[0],'Your must select a business';就像我以前试过的那样。非常感谢。我抢了枪,这导致了并发症。我在表单中有两个下拉列表:$this->form_validation->set_message'bether_than','Your must select a access level';$this->form_validation->set_rules'business_id'、'business'、'trim | required |大于[0];$this->form_validation->set_message'better_than','Your must select a business';。如果未选择访问级别,则错误为您必须选择一个业务。有人对此有补救办法吗。@pigfox您可以使用自己的规则,并在各自的函数中设置每个字段的错误消息,扩展CodeIgniter的,或者使用此线程中提供的信息,并使用所需的规则验证一个输入,而不是验证另一个输入,从而避免消息冲突。我的libraries文件夹中已经有一个MY_Form_验证类,其中包含我编写的自定义有效url函数。我尝试将此代码的其余部分添加到该文件中,但没有效果。我尝试在验证数组中添加带有值的消息键,但新消息没有显示。我在“服务条款”字段中使用了这一点,因此我对这一特定字段的唯一规则是需要了解我可能会做什么 你做错了什么?
<select name="business_id"> 
    <option value="select">Select Business</option> More options... 
</select>
$this->form_validation->set_rules('business_id', 'Business', 'require_dropdown[select]');
$this->form_validation->set_message('business_id', 'You must select a business');
<?php
$error = form_error('email');
echo str_replace('field must be a unique value', 'is already in use.', $error); 
// str_replace('string to search/compare', 'string to replace with', 'string to search in')
?>
/system/language/english/form_validation_lang.php
public function username_check($str)
{
    if ($str=="")
    {
        $this->form_validation->set_message('username_check', 'Merci d’indiquer le nombre d’adultes');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
$this->form_validation->set_rules('number_adults', 'Label Name','Your Message',) 'callback_username_check');
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', array(
'greater_than' => 'You must select a business',
));
$this->form_validation->set_message('greater_than[0]', 'You must select a business');