【问题标题】:Assert\Type not working in Symfony2 form validationAssert\Type 在 Symfony2 表单验证中不起作用
【发布时间】:2015-06-28 16:15:53
【问题描述】:

我有一个表单,其中包含一个需要整数的字段。这是表单类型实体定义中的字段定义:

/**
 * @ORM\Column(type="integer", nullable=true)
 * @Assert\Type(type="integer", message="Number of pieces must be a number.")
 * @Assert\GreaterThanOrEqual(value=1, message="Number of pieces cannot be lower than 1.")
 */
protected $numberOfPiecesSent;

相关的表单构建器如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('numberOfPiecesSent', 'integer', array('label' => 'Number of pieces sent:', 'required' => false));
}

当我在字段中提交带有非数字值的表单(例如,'aaa')时,它只是保存表单并将字段 numberOfPiecesSent 保留在数据库中,而不是验证失败。我还尝试使该字段为非NULL,但这没有帮助。请告诉我为什么这不起作用?

【问题讨论】:

  • 你可以试试regex
  • 令人惊讶的是,即使是正则表达式也不起作用。这很奇怪,因为 GreaterThanOrEqual() 断言有效,所以总的来说,我的断言有效。
  • 查看下面的答案。

标签: validation symfony


【解决方案1】:

我刚刚测试过,效果很好。您可以在下面的属性中添加约束。您也可以删除 NotBlank。随意修改。

实体

/**
 * @ORM\Column(type="integer", nullable=true)
 */
protected $numberOfPiecesSent;

表格

use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;

->add(
    'numberOfPiecesSent',
    'integer',
    [
        'constraints' => [
            new NotBlank(
                [
                    'message' => 'The numberOfPiecesSent is required.'
                ]
            ),
            new Range(
                [
                    'min' => 1,
                    'minMessage' => "The numberOfPiecesSent must contain at least {{ limit }}"
                ]
            )
        ]
    ]
)

更新

use Symfony\Component\Validator\Constraints\Regex;

->add(
    'name',
    'text',
    [
        'constraints' => [
            new Regex(
                [
                    'pattern' => "/^[0-9]+$/"
                ]
            )
        ]
    ]
)

use Symfony\Component\Validator\Constraints\Regex;

->add(
    'name',
    'integer',
    [
        'constraints' => [
            new Regex(
                [
                    'pattern' => "/^[0-9]+$/"
                ]
            )
        ]
    ]
)

【讨论】:

  • 谢谢!我刚试过这个。它仅在我使用 NotBlank 约束时才有效,我不想使用该约束,因为我需要该字段是可选的,因此这种解决方法不是我的解决方案。我真的很想知道为什么 Assert\Type 东西不起作用。我可能最终会编写自己的验证器,但我更喜欢使用内置功能。
  • 谢谢。所以这真的对你有用吗?我只是试过这个,它没有做任何事情。当我提交“aaa”作为字段的值时,它只是保存为 NULL。该字段还设置了'required' => false。我在 Symfony 2.7 上。
  • 啊,它适用于设置为“文本”而不​​是“整数”的小部件类型。说得通。我希望它与 Assert\Type 解决方案一起使用。我很高兴这有效。我要对此表示赞成,因为这是一个很好的解决方法,但我会等几天再接受它,以防有人知道如何让 Assert\Type 工作,这是原始问题的主题。非常感谢您的帮助!
  • 没关系。很高兴它提供了某种解决方案。
【解决方案2】:

您是否尝试过使字段不为空: @ORM\Column(type="integer", nullable=false) 还是您也尝试过断言 NotNull ? http://symfony.com/fr/doc/current/reference/constraints/NotNull.html

【讨论】:

  • 我试过@ORM\Column(type="integer", nullable=false) - 没有帮助。我没有尝试 NotNull 的事情,因为我实际上需要该字段是可选的。我只需要确保如果在其中输入任何值,它必须是一个数字。
  • Peut-on voir ton controller également ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
相关资源
最近更新 更多