mardi 1 mars 2016

Validate field with another rule if there is a condition

In Italy the Tax Code is a set of 16 alpha-numeric characters and the Vat Number is a number of 11 digits, but the the Tax Code can be equal to the Vat in certain conditions. I created two custom validation rules in Laravel 5.1 to check these two data types, but I don't know to tell the Framework "If the tax code is a number of 11 digits, validate as Vat Number!".

Here is the code of app/Providers/AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        //validate tax_code
        Validator::extend('tax_code', function($attribute, $value, $parameters, $validator) {
            if($value=='')
                return false;

            if ((strlen($value) == 11) && is_numeric($value))
            {
                //validate as vat number
            }

            if(strlen($value)!= 16)
                return false;

            $value=strtoupper($value);

            if(!preg_match("/[A-Z0-9]+$/", $value))
                return false;

            $s = 0;
            for($i=1; $i<=13; $i+=2){
                $c=$value[$i];
                if('0'<=$c and $c<='9')
                    $s+=ord($c)-ord('0');
                else
                    $s+=ord($c)-ord('A');
            }

            for($i=0; $i<=14; $i+=2){
                $c=$value[$i];
                switch($c){
                    case '0':  $s += 1;  break;
                    case '1':  $s += 0;  break;
                    case '2':  $s += 5;  break;
                    case '3':  $s += 7;  break;
                    case '4':  $s += 9;  break;
                    case '5':  $s += 13;  break;
                    case '6':  $s += 15;  break;
                    case '7':  $s += 17;  break;
                    case '8':  $s += 19;  break;
                    case '9':  $s += 21;  break;
                    case 'A':  $s += 1;  break;
                    case 'B':  $s += 0;  break;
                    case 'C':  $s += 5;  break;
                    case 'D':  $s += 7;  break;
                    case 'E':  $s += 9;  break;
                    case 'F':  $s += 13;  break;
                    case 'G':  $s += 15;  break;
                    case 'H':  $s += 17;  break;
                    case 'I':  $s += 19;  break;
                    case 'J':  $s += 21;  break;
                    case 'K':  $s += 2;  break;
                    case 'L':  $s += 4;  break;
                    case 'M':  $s += 18;  break;
                    case 'N':  $s += 20;  break;
                    case 'O':  $s += 11;  break;
                    case 'P':  $s += 3;  break;
                    case 'Q':  $s += 6;  break;
                    case 'R':  $s += 8;  break;
                    case 'S':  $s += 12;  break;
                    case 'T':  $s += 14;  break;
                    case 'U':  $s += 16;  break;
                    case 'V':  $s += 10;  break;
                    case 'W':  $s += 22;  break;
                    case 'X':  $s += 25;  break;
                    case 'Y':  $s += 24;  break;
                    case 'Z':  $s += 23;  break;
                }
            }

            if( chr($s%26+ord('A'))!=$value[15] )
                return false;

            return true;

        });

        //validate vat number
        Validator::extend('vat_number', function($attribute, $value, $parameters, $validator) {
            if($value=='')
                return false;

            //la p.iva deve essere lunga 11 caratteri
            if(strlen($value)!=11)
                return false;

            //la p.iva deve avere solo cifre
            if(!@ereg("^[0-9]+$", $value))
                return false;

            $primo=0;
            for($i=0; $i<=9; $i+=2)
                $primo+= ord($value[$i])-ord('0');

            for($i=1; $i<=9; $i+=2 ){
                $secondo=2*( ord($value[$i])-ord('0') );

            if($secondo>9)
                $secondo=$secondo-9;
            $primo+=$secondo;

            }
            if( (10-$primo%10)%10 != ord($value[10])-ord('0') )
                return false;

            return true;        
        });
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire