From 441a844844277a9f8027e0823a0fcc02b1c48239 Mon Sep 17 00:00:00 2001 From: Bastien Serprix Date: Fri, 16 Mar 2018 15:57:59 +0100 Subject: [PATCH 1/2] Added field entityId to requests when validating unique fields --- Factory/JsFormValidatorFactory.php | 2 +- Form/Constraint/UniqueEntity.php | 12 +++++++++++- Resources/doc/3_9.md | 1 + Resources/public/js/constraints/UniqueEntity.js | 3 +++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Factory/JsFormValidatorFactory.php b/Factory/JsFormValidatorFactory.php index 79c78d8..8fc8d1e 100644 --- a/Factory/JsFormValidatorFactory.php +++ b/Factory/JsFormValidatorFactory.php @@ -565,7 +565,7 @@ protected function parseConstraints(array $constraints) } if ($item instanceof \Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity) { - $item = new UniqueEntity($item, $this->currentElement->getConfig()->getDataClass()); + $item = new UniqueEntity($item, $this->currentElement->getConfig()->getDataClass(), $this->currentElement->getConfig()->getData()); } $result[get_class($item)][] = $item; diff --git a/Form/Constraint/UniqueEntity.php b/Form/Constraint/UniqueEntity.php index e19958c..8170243 100644 --- a/Form/Constraint/UniqueEntity.php +++ b/Form/Constraint/UniqueEntity.php @@ -14,14 +14,24 @@ class UniqueEntity extends BaseUniqueEntity */ public $entityName = null; + /** + * @var int + */ + public $entityId = null; + /** * @param BaseUniqueEntity $base * @param string $entityName + * @param mixed $data */ - public function __construct(BaseUniqueEntity $base, $entityName) + public function __construct(BaseUniqueEntity $base, $entityName, $data) { $this->entityName = $entityName; + if (is_object($data) && method_exists($data, 'getId')) { + $this->entityId = $data->getId(); + } + foreach ($base as $prop => $value) { $this->{$prop} = $value; } diff --git a/Resources/doc/3_9.md b/Resources/doc/3_9.md index d3907f7..ec757e7 100644 --- a/Resources/doc/3_9.md +++ b/Resources/doc/3_9.md @@ -40,6 +40,7 @@ $data = array ( 'ignoreNull' => '1', 'groups' => array ('Default', 'User'), 'entityName' => 'Acme\DemoBundle\Entity\User', + 'entityId' => 15, 'data' => array ( 'email' => 'john_doe@example.com', ) diff --git a/Resources/public/js/constraints/UniqueEntity.js b/Resources/public/js/constraints/UniqueEntity.js index 7096e32..3a63cb4 100644 --- a/Resources/public/js/constraints/UniqueEntity.js +++ b/Resources/public/js/constraints/UniqueEntity.js @@ -11,6 +11,7 @@ function FpJsFormValidatorBundleFormConstraintUniqueEntity() { this.errorPath = null; this.ignoreNull = true; this.entityName = null; + this.entityId = null; this.groups = []; @@ -19,6 +20,7 @@ function FpJsFormValidatorBundleFormConstraintUniqueEntity() { * @param {FpJsFormElement} element */ this.validate = function(value, element) { + var self = this; var route = null; var config = FpJsFormValidator.config; @@ -44,6 +46,7 @@ function FpJsFormValidatorBundleFormConstraintUniqueEntity() { ignoreNull: this.ignoreNull ? 1 : 0, groups: this.groups, + entityId: this.entityId, entityName: this.entityName, data: this.getValues(element, this.fields) }, From a8ed6ed8e1eda102b455d4a3b77f98aa800ab46e Mon Sep 17 00:00:00 2001 From: Bastien Serprix Date: Fri, 16 Mar 2018 17:25:26 +0100 Subject: [PATCH 2/2] Saving whole entity in UniqueEntity constraint --- Form/Constraint/UniqueEntity.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Form/Constraint/UniqueEntity.php b/Form/Constraint/UniqueEntity.php index 8170243..990beae 100644 --- a/Form/Constraint/UniqueEntity.php +++ b/Form/Constraint/UniqueEntity.php @@ -14,6 +14,11 @@ class UniqueEntity extends BaseUniqueEntity */ public $entityName = null; + /** + * @var mixed + */ + protected $entity = null; + /** * @var int */ @@ -22,14 +27,18 @@ class UniqueEntity extends BaseUniqueEntity /** * @param BaseUniqueEntity $base * @param string $entityName - * @param mixed $data + * @param mixed $entity */ - public function __construct(BaseUniqueEntity $base, $entityName, $data) + public function __construct(BaseUniqueEntity $base, $entityName, $entity) { $this->entityName = $entityName; - if (is_object($data) && method_exists($data, 'getId')) { - $this->entityId = $data->getId(); + if (is_object($entity)) { + $this->entity = $entity; + + if (method_exists($this->entity, 'getId')) { + $this->entityId = $this->entity->getId(); + } } foreach ($base as $prop => $value) {