php – Symfony2表单验证器 – 在刷新之前比较旧值和新值

前端之家收集整理的这篇文章主要介绍了php – Symfony2表单验证器 – 在刷新之前比较旧值和新值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道是否有一种方式来比较一个实体之前的验证器中的旧值和新值.

我有一个服务器实体呈现一个表格的罚款.实体与状态(N→1)有关系,当状态从Unracked更改为Racked时,需要检查SSH和FTP对服务器的访问.如果未实现访问,则验证程序将失败.

我已经将验证器回调映射到服务器实体中的方法isServerValid(),如这里所述
http://symfony.com/doc/current/reference/constraints/Callback.html.我显然可以通过$this->状态访问“新”值,但是如何获得原始值?

在伪代码中,这样的东西:

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');
    }
}

提前致谢!

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 }

猜你在找的PHP相关文章