Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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 在Laravel 4中显示自定义验证的错误消息_Php_Validation_Laravel_Laravel 4 - Fatal编程技术网

Php 在Laravel 4中显示自定义验证的错误消息

Php 在Laravel 4中显示自定义验证的错误消息,php,validation,laravel,laravel-4,Php,Validation,Laravel,Laravel 4,我通过创建一个类创建了一个自定义错误函数 <?php class CoreValidator extends Illuminate\Validation\Validator { public function validatePostcode($attribute, $value, $parameters = null) { $regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-

我通过创建一个类创建了一个自定义错误函数

<?php

class CoreValidator extends Illuminate\Validation\Validator
{
    public function validatePostcode($attribute, $value, $parameters = null)
    {
        $regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
        if(preg_match($regex ,$value)) {
            return true;
        }
        return false;
    }
}
它已在myglobal.php中注册

Validator::resolver(function($translator, $data, $rules, $messages) {
        return new CoreValidator($translator, $data, $rules, $messages);
    });
这一切都很好,但它返回的错误消息是

验证。邮政编码

如何/在何处为此设置自定义错误消息?
我试过用设置app/lang/en/validation.php


另外,我知道已经有了一种正则表达式验证方法,但这个问题对我来说更一般。

我想我已经破解了它

我将消息添加到app/lang/en/validation.php中的主数组中,而不是添加到自定义子数组中

return array(
    ...
    "url" => "The :attribute format is invalid.",
    "postcode" => "my error message 2",
    ...
)

如果这不是正确的方法,那么有人可以自由地纠正我。

也许这段代码更好:

// for example I am using sub-array custom at default validation file, but you can do it in other file as you wishes.
..other..
'custom' => array(
        'email' => array(
            'required' => 'We need to know your e-mail address!',
        ),
        "required"         => "Hey!!! don't forget at :attribute field is required.",
    ),
..other..

// you can determine your custom languages at your wishes file
$messages = \Lang::get('validation.custom');

Validator::make($input, $rules, $messages);
发件人:

在某些情况下,您可能希望在语言文件中指定自定义消息,而不是将它们直接传递给验证器。为此,请将消息添加到app/lang/xx/validation.php语言文件中的自定义数组中

这意味着,在你的情况下

'custom' => array(
    'postcode' => array(
        'PostCode' => 'error message for PostCode rule',
        'required' => 'error message for required rule',
    ),
),

如果要使用app/lang/xx/validation.php中的自定义验证消息数组,正确的方法如下:

'custom' => array(
    'formFieldName' => array(
        'postcode' => 'error message for PostCode rule',
        'iamalwayslowercase' => 'error message for this rule'
    ),
),

请注意,您使用表单字段的名称,然后在数组中使用规则的小写名称。

您可以使用
setCustomMessages()
方法分配自定义消息,如下面的代码

<?php

class CoreValidator extends Illuminate\Validation\Validator
{

    private $custom_messages = array(
        "customvalidation" => "my error message.",
    );

    public function __construct($translator, $data, $rules, $messages = array(), $customAttributes = array())
    {
        parent::__construct($translator, $data, $rules, $messages, $customAttributes);
        $this->setCustomMessages($this->custom_messages);
    }

    public function validateCustomvalidation($attribute, $value, $parameters = null)
    {
        // validation code here
    }

}

下面的代码也可以很好地工作,请注意
$customValidatorMessages
数组索引上的下划线。希望它能帮助别人:-)


可能是@Abishersrikaanth的重复答案是错误消息中的自定义占位符,而不是自定义方法的实际消息我也是这样做的,并且认为这也是正确的方法。我非常想知道这是否正确,它看起来很粗略。@uɐɥʇɐᴎ 我刚刚遇到了同样的问题-请参阅我发布的关于如何正确使用自定义验证数组的答案。对于第三方自定义规则,这仍然不适用:-(文件在v5中的位置略有不同:如果您正在进行包开发,这是唯一的好选项。\Lang::get('package::validation.custom'));如果您希望使用$规则保留$消息,您也可以在模型中使用以下内容(这样您就不会弄乱语言文件):
public static$messages=array('formFieldName.postcode'=>'error message for postcode rule');
对于第三方自定义规则,这仍然不适用于我:-(@groovenectar)我无法查看您的代码,但我可以肯定地说,上面的代码很有效,并且已经使用了一段时间。
'custom' => array(
    'postcode' => array(
        'PostCode' => 'error message for PostCode rule',
        'required' => 'error message for required rule',
    ),
),
'custom' => array(
    'formFieldName' => array(
        'postcode' => 'error message for PostCode rule',
        'iamalwayslowercase' => 'error message for this rule'
    ),
),
<?php

class CoreValidator extends Illuminate\Validation\Validator
{

    private $custom_messages = array(
        "customvalidation" => "my error message.",
    );

    public function __construct($translator, $data, $rules, $messages = array(), $customAttributes = array())
    {
        parent::__construct($translator, $data, $rules, $messages, $customAttributes);
        $this->setCustomMessages($this->custom_messages);
    }

    public function validateCustomvalidation($attribute, $value, $parameters = null)
    {
        // validation code here
    }

}
class CoreValidator extends Illuminate\Validation\Validator
{
    /**
     * The array of custom validator error messages.
     *
     * @var array
     */
    protected $customValidatorMessages = array(); 

    public function validatePostcode($attribute, $value, $parameters = null)
    {
        $regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
        if(preg_match($regex ,$value)) {
            return true;
        }

        $this->customValidatorMessages['post_code'] = 'Postcode error message.';

        $this->setCustomMessages($this->customValidatorMessages);

        return false;
    }
}