【问题标题】:Symfony Form Callback is not returning the object as the first argumentSymfony 表单回调未将对象作为第一个参数返回
【发布时间】:2014-09-16 08:34:04
【问题描述】:

我正在尝试向 Symfony 2.4 中的复选框添加回调约束。这个想法是检查对象上的其他值并决定天气或不允许验证通过。

我已经让回调工作,但返回的第一个参数不是实体,而是复选框的值。 Symfony 文档声明第一个参数将是对象。 http://symfony.com/doc/current/reference/constraints/Callback.html#the-callback-method。我不确定我错过了什么

表格代码如下:

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContextInterface;

//...

public function buildForm(FormBuilderInterface $builder, array $options) {
    parent::buildForm($builder, $options);
    $builder->add('enabled', 'checkbox', array(
        'required' => false,
        'constraints' => array(
            new Assert\Callback(array(
                'callback' => array(
                    $this,
                    'validateisReady'),
                'groups' => $this->validationGroups))
        ),
    ))
    ;
}

public static function validateisReady($object, ExecutionContextInterface $context) {

    //..
    if($object->getItems()->count() < 1){
        $context->addViolationAt('enabled', 'items.missing');
    }
    //..
}

$object 保存复选框的布尔值。我希望它成为实体。有什么想法吗?

【问题讨论】:

  • 尝试将此约束添加到您的实体,而不是表单
  • 谢谢你完美的工作。如果我可以接受您的评论作为答案,那么我会的。

标签: validation symfony callback


【解决方案1】:

您需要将此约束添加到您的实体对象,而不是表单。

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2016-03-04
    相关资源
    最近更新 更多