EcommerceBundle/Entity/CheckoutAddressable.php
public function validateDeliveryDni(ExecutionContextInterface $context)
    {
        $dni = $this->getDeliveryDni();

        if (is_null($dni)) {
            return;
        }

        // check format
        if (0 === preg_match("/\d{1,8}[a-z]/i", $dni)) {
            $context->addViolationAt('dni', 'Invalid DNI number');

            return;
        }

        // check letter
        $number = substr($dni, 0, -1);
        $letter  = strtoupper(substr($dni, -1));
        if ($letter != substr("TRWAGMYFPDXBNJZSQVHLCKE", strtr($number, "XYZ", "012")%23, 1)) {
            $context->addViolationAt('dni', 'Invalid DNI letter');
        }
    }
public function validateDeliveryDni(ExecutionContextInterface $context)
    {
        $dni = $this->getDeliveryDni();

        if (is_null($dni)) {
            return;
        }

        // check format
        if (0 === preg_match("/\d{1,8}[a-z]/i", $dni)) {
            $context->addViolationAt('dni', 'Invalid DNI number');

            return;
        }

        // check letter
        $number = substr($dni, 0, -1);
        $letter  = strtoupper(substr($dni, -1));
        if ($letter != substr("TRWAGMYFPDXBNJZSQVHLCKE", strtr($number, "XYZ", "012")%23, 1)) {
            $context->addViolationAt('dni', 'Invalid DNI letter');
        }
    }
public function validateDeliveryDni ( ExecutionContextInterface $ context ) { $ dni = $ this -> getDeliveryDni ( ) ; if ( is_null ( $ dni ) ) { return ; } // check format if ( 0 === preg_match ( "/\d{1,8}[a-z]/i" , $ dni ) ) { $ context -> addViolationAt ( 'dni' , 'Invalid DNI number' ) ; return ; } // check letter $ number = substr ( $ dni , 0 , - 1 ) ; $ letter = strtoupper ( substr ( $ dni , - 1 ) ) ; if ( $ letter != substr ( "TRWAGMYFPDXBNJZSQVHLCKE" , strtr ( $ number , "XYZ" , "012" ) % 23 , 1 ) ) { $ context -> addViolationAt ( 'dni' , 'Invalid DNI letter' ) ; } }
Custom validator to check delivery Dni

@param ExecutionContextInterface $context
Custom validator to check delivery Dni