Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added field entityId to requests when validating unique fields #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Factory/JsFormValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thinks better to pass all class with data. $this->currentElement->getConfig()

}

$result[get_class($item)][] = $item;
Expand Down
12 changes: 11 additions & 1 deletion Form/Constraint/UniqueEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ class UniqueEntity extends BaseUniqueEntity
*/
public $entityName = null;

/**
* @var int
*/
public $entityId = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Store here whole entity object instead of fields.


/**
* @param BaseUniqueEntity $base
* @param string $entityName
* @param mixed $data
*/
public function __construct(BaseUniqueEntity $base, $entityName)
public function __construct(BaseUniqueEntity $base, $entityName, $data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public function __construct(BaseUniqueEntity $base, $entity)

{
$this->entityName = $entityName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->entity = $entity;


if (is_object($data) && method_exists($data, 'getId')) {
$this->entityId = $data->getId();
}

foreach ($base as $prop => $value) {
$this->{$prop} = $value;
}
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/3_9.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $data = array (
'ignoreNull' => '1',
'groups' => array ('Default', 'User'),
'entityName' => 'Acme\DemoBundle\Entity\User',
'entityId' => 15,
'data' => array (
'email' => '[email protected]',
)
Expand Down
3 changes: 3 additions & 0 deletions Resources/public/js/constraints/UniqueEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function FpJsFormValidatorBundleFormConstraintUniqueEntity() {
this.errorPath = null;
this.ignoreNull = true;
this.entityName = null;
this.entityId = null;

this.groups = [];

Expand All @@ -19,6 +20,7 @@ function FpJsFormValidatorBundleFormConstraintUniqueEntity() {
* @param {FpJsFormElement} element
*/
this.validate = function(value, element) {

var self = this;
var route = null;
var config = FpJsFormValidator.config;
Expand All @@ -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)
},
Expand Down