xinjia / spain-validator-bundle
此包已被废弃,不再维护。作者建议使用 avegao/spain-validator-bundle 包。
最新版本(v1.0.0)的此包没有可用的许可信息。
西班牙邮政编码和个人身份验证的Symfony2验证器
v1.0.0
2016-05-21 17:55 UTC
Requires
- php: >=5.3
- symfony/symfony: >=2.5
- symfony/validator: ~2.6
This package is not auto-updated.
Last update: 2022-02-01 12:44:29 UTC
README
允许验证西班牙特定数据的Bundle。
这些数据的列表包括
- 固定电话
- 移动电话
- 任何电话
- 邮政编码
- DNI
- CIF
- DNI 和 CIF
安装
通过Composer启动安装
$ php composer.phar require avegao/spain-validator-bundle
将Bundle注册到我们的Symfony安装中
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new Xinjia\SpainValidatorBundle\XinjiaSpainValidatorBundle(), // ... ); }
使用示例
从实体使用
<?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; }
从控制器使用
<?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(), ]); } }