Symfony2 表单验证器 - 刷新前比较新旧值

2022-08-30 20:46:48

我想知道是否有一种方法可以在刷新之前比较实体内验证程序中的新旧值。

我有一个实体,它呈现为形式很好。实体与 (N->1) 有关系,当状态从 更改为 时,需要检查对服务器的 SSH 和 FTP 访问。如果未实现访问,则验证程序应失败。ServerstatusUnrackedRacked

我已将验证程序回调映射到实体中的方法,如此处所述,http://symfony.com/doc/current/reference/constraints/Callback.html。我显然可以通过 访问“新”值,但是如何获取原始值?isServerValid()Server$this->status

在伪代码中,如下所示:

public function isAuthorValid(ExecutionContextInterface $context)
{
    $original = ... ; // get old values
    if( $this->status !== $original->status && $this->status === 'Racked' && $original->status === 'Unracked' )
    {
        // check ftp and ssh connection
        // $context->addViolationAt('status', 'Unable to connect etc etc');
    }
}

提前致谢!


答案 1

Symfony 2.5 (http://symfony.com/doc/current/cookbook/validation/custom_constraint.html 的完整示例)

在此示例中,实体“NoDecreasingInteger”的字段“integerField”的新值必须高于存储的值。

创建约束:

// src/Acme/AcmeBundle/Validator/Constraints/IncrementOnly.php;
<?php
namespace Acme\AcmeBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class IncrementOnly extends Constraint
{
  public $message = 'The new value %new% is least than the old %old%';

  public function getTargets()
  {
    return self::CLASS_CONSTRAINT;
  }

  public function validatedBy()
  {
    return 'increment_only';
  }
}

创建约束验证程序:

// src/Acme/AcmeBundle/Validator/Constraints/IncrementOnlyValidator.php
<?php
namespace Acme\AcmeBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

use Doctrine\ORM\EntityManager;

class IncrementOnlyValidator extends ConstraintValidator
{
  protected $em;

  public function __construct(EntityManager $em)
  {
    $this->em = $em;
  }

  public function validate($object, Constraint $constraint)
  {
    $new_value = $object->getIntegerField();

    $old_data = $this->em
      ->getUnitOfWork()
      ->getOriginalEntityData($object);

    // $old_data is empty if we create a new NoDecreasingInteger object.
    if (is_array($old_data) and !empty($old_data))
      {
        $old_value = $old_data['integerField'];

        if ($new_value < $old_value)
          {
            $this->context->buildViolation($constraint->message)
              ->setParameter("%new%", $new_value)
              ->setParameter('%old%', $old_value)
              ->addViolation();
          }
      }
  }
}

将验证程序绑定到实体:

// src/Acme/AcmeBundle/Resources/config/validator.yml
Acme\AcmeBundle\Entity\NoDecreasingInteger:
  constraints:
     - Acme\AcmeBundle\Validator\Constraints\IncrementOnly: ~

将 EntityManager 注入 IncrementOnlyValidator:

// src/Acme/AcmeBundle/Resources/config/services.yml
services:
   validator.increment_only:
        class: Acme\AcmeBundle\Validator\Constraints\IncrementOnlyValidator
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: validator.constraint_validator, alias: increment_only }

答案 2

在 symfony2 中的自定义验证器中访问 EntityManager

您可以在控制器操作中检查以前的值...但这不会是一个干净的解决方案!

正常的表单验证将仅访问绑定到表单的数据...默认情况下无法访问“以前”的数据。

您尝试使用的回调约束无权访问容器或任何其他服务...因此,您无法轻松访问实体管理器(或任何以前的数据提供程序)来检查以前的值。

您需要的是类级别的自定义验证程序。需要类级,因为如果要获取实体,则需要访问整个对象而不仅仅是单个值。

验证程序本身可能如下所示:

namespace Vendor\YourBundle\Validation\Constraints;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class StatusValidator extends ConstraintValidator
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function validate($status, Constraint $constraint)
    {

        $em = $this->container->get('doctrine')->getEntityManager('default');

        $previousStatus = $em->getRepository('YourBundle:Status')->findOneBy(array('id' => $status->getId()));

        // ... do something with the previous status here

        if ( $previousStatus->getValue() != $status->getValue() ) {
            $this->context->addViolationAt('whatever', $constraint->message, array(), null);
        }
    }

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
       return 'previous_value';
    }
}

...然后将验证器注册为服务并将其标记为验证器

services:
    validator.previous_value:
        class: Vendor\YourBundle\Validation\Constraints\StatusValidator

        # example! better inject only the services you need ... 
        # i.e. ... @doctrine.orm.entity_manager

        arguments: [ @service_container ]         
        tags:
            - { name: validator.constraint_validator, alias: previous_value }

最后使用状态实体的约束(即使用注释)

use Vendor\YourBundle\Validation\Constraints as MyValidation;

/**
 * @MyValidation\StatusValidator
 */
class Status 
{

推荐