Git Product home page Git Product logo

spainvalidatorbundle's Introduction

SpainValidatorBundle

Bundle que posibilita la validación de datos específicos de España.

El listado de estos datos es:

  • Teléfono fijo
  • Teléfono móvil
  • Cualquier teléfono
  • Código postal
  • DNI
  • CIF
  • DNI Y CIF

Instalación

Lanzamos instalación mediante Composer

$  php composer.phar require avegao/spain-validator-bundle

Registramos el bundle en nuestra instalación de Symfony:

<?php
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Xinjia\SpainValidatorBundle\XinjiaSpainValidatorBundle(),
        // ...
    );
}

Ejemplo de uso

Uso desde la entidad:

<?php

namespace AppBundle\Entity;

// Validación extra, telefono, DNI/NIF...
use Xinjia\SpainValidatorBundle\Validator as ExtraAssert;

/**
 * MyEntity
 *
 */
class MyEntity {

    /**
     * @var string
     *
     * @ORM\Column(name="telefono", type="string", length=255, nullable=true)
     *
     * @Assert\Length(
     *      max = 9,
     *      maxMessage = "El teléfono debe tener {{ limit }} números"
     * )
     *
     * @ExtraAssert\AllPhone(message="No es un teléfono válido")
     */
    private $telefono;

    /**
     * @var string
     *
     * @ORM\Column(name="telefonoFijo", type="string", length=255, nullable=true)
     *
     * @Assert\Length(
     *      max = 9,
     *      maxMessage = "El teléfono debe tener {{ limit }} números"
     * )
     *
     * @ExtraAssert\Phone(message="No es un teléfono fijo válido")
     */
    private $telefonoFijo;
    
    /**
     * @var string
     *
     * @ORM\Column(name="telefonoMovil", type="string", length=255, nullable=true)
     *
     * @Assert\Length(
     *      max = 9,
     *      maxMessage = "El teléfono debe tener {{ limit }} números"
     * )
     *
     * @ExtraAssert\MobilePhone(message="No es un teléfono móvil válido")
     */
    private $telefonoMovil;
    
    /**
     * @var string
     *
     * @ORM\Column(name="codigoPostal", type="string", length=255, nullable=true)
     *
     * @ExtraAssert\ZipCode(message="No es un código postal válido")
     */
    private $codigoPostal;

    /**
     * @var string
     *
     * @ORM\Column(name="dniCif", type="string", length=255, nullable=true)
     *
     * @ExtraAssert\DniCif(message="No es un DNI o CIF válido")
     */
    private $dniCif;

    /**
     * @var string
     *
     * @ORM\Column(name="dni", type="string", length=255, nullable=true)
     *
     * @ExtraAssert\Dni(message="No es un DNI válido")
     */
    private $dni;

    /**
     * @var string
     *
     * @ORM\Column(name="cif", type="string", length=255, nullable=true)
     *
     * @ExtraAssert\Cif(message="No es un CIF válido")
     */
    private $cif;

}

Uso desde el controlador:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Xinjia\SpainValidatorBundle\Validator\AllPhone;
use Xinjia\SpainValidatorBundle\Validator\Phone;
use Xinjia\SpainValidatorBundle\Validator\MobilePhone;
use Xinjia\SpainValidatorBundle\Validator\ZipCode;
use Xinjia\SpainValidatorBundle\Validator\DniCif;
use Xinjia\SpainValidatorBundle\Validator\Cif;
use Xinjia\SpainValidatorBundle\Validator\Dni;

class DefaultController extends Controller {

    public function indexAction(Request $request) {

        $form = $this->createFormBuilder()
                ->add('telefono', 'text', [
                    'constraints' => new AllPhone(),
                ])
                ->add('telefonoFijo', 'text', [
                    'constraints' => new Phone(),
                ])
                ->add('telefonoMovil', 'text', [
                    'constraints' => new MobilePhone(),
                ])
                ->add('codigoPostal', 'text', [
                    'constraints' => new ZipCode(),
                ])
                ->add('dniCif', 'text', [
                    'constraints' => new DniCif(),
                ])
                ->add('cif', 'text', [
                    'constraints' => new Cif(),
                ])
                ->add('dni', 'text', [
                    'constraints' => new Dni(),
                ])
                ->add('save', 'submit', array('label' => 'Send'))
                ->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            // ...
        }

        // replace this example code with whatever you need
        return $this->render('AppBundle:Default:index.html.twig', [
                    'form' => $form->createView(),
        ]);
    }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.