dimanche 22 novembre 2015

laravel register custom validator

for laravel 5 i want to create and define custom validator, but i can't find whitch file and folder i must be register that, i can not find that in laravel documents, for example after create Extension\Validation folders into app folder, i'm create this class:

<?php
namespace App\Extension\Validation;
use Illuminate\Validation\Validator;
class CustomValidator extends Validator {

    public function validateNationalCode ($attribute, $value, $parameters)
    {
        if (!preg_match('/^[0-9]{10}$/', $value)) {
            return false;
        }
        for ($i = 0; $i < 10; $i++) {
            if (preg_match('/^' . $i . '{10}$/', $value)) {
                return false;
            }
        }

        for ($i = 0, $sum = 0; $i < 9; $i++) {
            $sum += ((10 - $i) * intval(substr($value, $i, 1)));
        }
        $ret = $sum % 11;
        $parity = intval(substr($value, 9, 1));

        if (($ret < 2 && $ret == $parity) || ($ret >= 2 && $ret == 11 - $parity)) {
            return true;
        }

        return false;
    }
}

now i must be use below code to register that :

Validator::resolver(function($translator, $data, $rules, $messages)
{
    return new CustomValidator($translator, $data, $rules, $messages);
});

Laravel Document:

Registering A Custom Validator Resolver Next, you need to register your custom Validator extension:

which file i must be add or append?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire