Laravel5-自定义验证器-未定义的setPresenceVerifier()

pcezar91

我正在尝试在Laravel 5中创建一个自定义验证器,但是在创建解析器后卡住了。调用验证后,它将返回:

Factory.php第97行中的FatalErrorException:调用未定义的方法dpvnice \ Validators \ CustomValidator :: setPresenceVerifier()

开机功能:

use Validator;
use Route;
use dpvnice\Validators\CustomValidator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */    
public function boot(){
            $messages = ['cnpj' => 'CNPJ inválido', 'cpf' => 'CPF inválido'];
            Validator::resolver(function($translator, $data, $rules, $messages){
                return new CustomValidator($translator, $data, $rules, $messages);
            });
    } ...

customValidator类:

<?php namespace dpvnice\Validators;
    use Validator;

class CustomValidator extends Validator {

    public function validateCpf ($attribute, $cpf, $parameters){
        // Verifica se um número foi informado
        if(empty($cpf)) {return false;}

        // Elimina possivel mascara
        $cpf = str_replace(array(".","/","-"),"",$cpf);

        // Verifica se o numero de digitos informados é igual a 11 
        if (strlen($cpf) != 11) {
            return false;
        }
        // Verifica se nenhuma das sequências invalidas abaixo 
        // foi digitada. Caso afirmativo, retorna falso
        else if ($cpf == '00000000000' || 
            $cpf == '11111111111' || 
            $cpf == '22222222222' || 
            $cpf == '33333333333' || 
            $cpf == '44444444444' || 
            $cpf == '55555555555' || 
            $cpf == '66666666666' || 
            $cpf == '77777777777' || 
            $cpf == '88888888888' || 
            $cpf == '99999999999') {
            return false;
         // Calcula os digitos verificadores para verificar se o
         // CPF é válido
        } else {   
            for ($t = 9; $t < 11; $t++) {
                for ($d = 0, $c = 0; $c < $t; $c++) {
                    $d += $cpf{$c} * (($t + 1) - $c);
                }
                $d = ((10 * $d) % 11) % 10;
                if ($cpf{$c} != $d) {
                    return false;
                }
            }
            return true;
        }
    }

    function validateCnpj ($attribute, $cnpj, $parameters){
        ...
    }


}

在代码上的用法:

public function postAdicionarConvidado(Request $request){
        $input = Input::all();

        $v = Validator::make($request->all(), $this->convidadoRules, $this->convidadoRulesMessage);
        $v->sometimes('senha', 'required|same:confirmar-senha|min:5', function($input){
            return is_null(User::where(['email' => $input['email']])->first());
        });
 ...
爱国者

您的自定义验证器类需要扩展Illuminate\Validation\Validator,而不是ValidatorFacade的别名

<?php namespace dpvnice\Validators;

class CustomValidator extends Illuminate\Validation\Validator {
    // code
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章