【问题标题】:symfony2 fails with error "This value should not be null"symfony2 失败并出现错误“此值不应为空”
【发布时间】:2012-11-25 19:37:07
【问题描述】:

我正在使用 symfony2 表单,现在我收到消息“此值不应为空。”.. 现在“这个值”..这个值是多少?以及如何解决?

通过转储错误: ($form->getErrors()) 我得到

array (size=3)
  0 => 
    object(Symfony\Component\Form\FormError)[2418]
      protected 'messageTemplate' => string 'This value should not be null.' (length=30)
      protected 'messageParameters' => 
        array (size=0)
          empty
      protected 'messagePluralization' => null
  1 => 
    object(Symfony\Component\Form\FormError)[2420]
      protected 'messageTemplate' => string 'This value should not be null.' (length=30)
      protected 'messageParameters' => 
        array (size=0)
          empty
      protected 'messagePluralization' => null
  2 => 
    object(Symfony\Component\Form\FormError)[2421]
      protected 'messageTemplate' => string 'This value should not be null.' (length=30)
      protected 'messageParameters' => 
        array (size=0)
          empty
      protected 'messagePluralization' => null

在切换一些断言后,一个 formError 消失了。那是什么:

 /**
  * @ORM\Column(type="integer")
  * @Assert\NotNull()
  */
 protected $price;

var_dump( $reservation->getPrice(), is_null( $reservation->getPrice() ) );

结果:

float 733

boolean false

但是在断言中这是一个错误..

实体失败:

/**
 * @ORM\Column(type="integer")
 * @Assert\NotNull()
 */
protected $price;

/**
* @ORM\ManyToOne(targetEntity="Caravan", inversedBy="caravan")
* @ORM\JoinColumn(name="caravan_id", referencedColumnName="id")
* @Assert\NotNull()
*/
protected $caravan;

/**
 * @ORM\Column(type="datetime")
 * @Assert\NotNull()
 */
protected $created;

这些是我在 isValid 检查之前在控制器中设置的 3 个属性。如果我这样做:

$form->getData();

它有正确的数据。

【问题讨论】:

  • 请提供表单的转储以及任何相关的过滤器。

标签: symfony assert


【解决方案1】:

问题是:

不会工作:

$form->bind( $request );

$reservation->setCreated( new \DateTime() );

if( $form->isValid() ){

}

有效:

$reservation->setCreated( new \DateTime() );

$form->bind( $request );

if( $form->isValid() ){

}

当表单绑定时,他会检查他是否有效。 所以以后他就不会再查了。

所以一点建议,在isValid检查前的最后一刻绑定它=)

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2018-02-24
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多