dousi6405 2015-03-17 19:23
浏览 115
已采纳

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

I'm trying to create a custom validator in Laravel 5 but got stuck after creating the Resolver. When the validation is called it returns:

FatalErrorException in Factory.php line 97: Call to undefined method dpvnice\Validators\CustomValidator::setPresenceVerifier()

boot function:

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 class:

<?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){
        ...
    }


}

Usage on code:

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());
        });
 ...
  • 写回答

1条回答 默认 最新

  • duanbiaojin8860 2015-03-17 20:02
    关注

    Your custom validator class needs to extend Illuminate\Validation\Validator, not the alias to the Validator facade.

    <?php namespace dpvnice\Validators;
    
    class CustomValidator extends Illuminate\Validation\Validator {
        // code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码