Symfony2指定表单验证

时间:2016-06-06 12:59:46

标签: php forms symfony

我有一个简单的Symfony表单。

RIGHT

我可以在哪里更改此处接受的验证类型?

当输入错误时,我总是得到默认的浏览器错误消息,我想更改验证标准。

更新

现在找到模式:

  {{ form_rest(form) }}

现在我想更改协议,所以如果有http://,https://或者两者都没有,它会匹配....协议的类型存储在:

  const PATTERN = '~^
        (%s://)                               # protocol
        (([\pL\pN-]+:)?([\pL\pN-]+)@)?          # basic auth
        (
            ([\pL\pN\pS-\.])+(\.?([\pL]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
                |                                              # or
            \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}                 # a IP address
                |                                              # or
            \[
                (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
            \]  # a IPv6 address
        )
        (:[0-9]+)?                              # a port (optional)
        (/?|/\S+)                               # a /, nothing or a / with something
    $~ixu';

我该如何改变?

1 个答案:

答案 0 :(得分:1)

Symfony的验证服务根据与其关联的实体的字段注释来验证字段。

要处理错误,应该写

$validator = $this->get('validator');
$errors = $validator->validate($entity);

要自定义打印错误的方式,您可以渲染自己的视图,例如:

if (count($errors) > 0) 
    return $this->render('my_entity/validation.html.twig', array(
    'errors' => $errors,
));

要更改错误消息,您应该按以下模式编辑实体字段:

// src/AppBundle/Entity/M.php

// The following is required to use validator service for that entity.
use Symfony\Component\Validator\Constraints as Assert;

class MyEntity
{
    /**
     * @Assert\NotBlank(message="This field is mandatory.")
     * @Assert\Length(min=3, message="This field should be 3 characters at least.")
     */
    private $field;
}

有关如何在Symfony中处理验证的约束和简要回忆的详尽列表,请参见cheat sheet

阅读Symfony's validator service documentation可能对更高级的练习和细节有用。

更新

要以不同的方式过滤网址,您应该创建自定义约束。

  1. 创建自定义约束类

        // src/AppBundle/Validator/Constraints/AnyURL.php
        namespace AppBundle\Validator\Constraints;
    
        use Symfony\Component\Validator\Constraint;
    
        /**
         * @Annotation
         */
        class AnyURL extends Constraint
        {
            public $message = '%url% is not a valid URL.';
        }
    
  2. 创建约束验证器(检查here是否有更方便的URL PHP正则表达式,尽管我使用的那个应该可以完成这项工作)

        // src/AppBundle/Validator/Constraints/AnyURLValidator.php
        namespace AppBundle\Validator\Constraints;
    
        use Symfony\Component\Validator\Constraint;
        use Symfony\Component\Validator\ConstraintValidator;
    
        class AnyURLValidator extends ConstraintValidator
        {
            public function validate($value, Constraint $constraint)
            {
                if (!preg_match('#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i',
    $value, $matches)) {
                    $this->context->buildViolation($constraint->message)
                        ->setParameter('%url%', $value)
                        ->addViolation();
                }
            }
        }
    
  3. 在目标实体

    中有效使用验证器
    // src/AppBundle/Entity/MyEntity.php
    use Symfony\Component\Validator\Constraints as Assert;
    use AppBundle\Validator\Constraints as MyAssert;
    
    class MyEntity
    {    
        /**
         * @MyAssert\AnyURL
         */
        protected $url;