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

Conversation

bastos71
Copy link

Resolving issue #143

@@ -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()

/**
* @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.

*/
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)

*/
public function __construct(BaseUniqueEntity $base, $entityName)
public function __construct(BaseUniqueEntity $base, $entityName, $data)
{
$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;

Copy link
Collaborator

@66Ton99 66Ton99 left a comment

Choose a reason for hiding this comment

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

I think you got my idea.

@bastos71
Copy link
Author

Okay no problem I think you're right :)

Just a few minutes and I will commit it

@bastos71
Copy link
Author

bastos71 commented Mar 16, 2018

@66Ton99 The problem is : how do I pass entity object from PHP to Js Model ?
I saw Fp\JsFormValidatorBundle\Model\JsModelAbstract::phpValueToJs() function but it would be dirty to change code here and write a specific case just for "entity" field, no ?

Any idea ?

Edit : because in my case my User entity has a method __toString (from FOSUserBundle), so it goes through the first "if"

//...
public static function phpValueToJs($value)
    {
        // For object which has own __toString method
        if ($value instanceof JsModelAbstract) {
            return $value->toJsString();
        }
        // For object which has own __toString method
        elseif (is_object($value) && method_exists($value, '__toString')) {
            return self::phpValueToJs($value->__toString());
        }
        // For an object or associative array
        elseif (is_object($value) || (is_array($value) && array_values($value) !== $value)) {
            $jsObject = array();
            foreach ($value as $paramName => $paramValue) {
                $paramName = addcslashes($paramName, '\'\\');
                $jsObject[] = "'$paramName':" . self::phpValueToJs($paramValue);
            }

            return sprintf('{%1$s}', implode($jsObject, ','));
        }
        //.....
    }
//...

@66Ton99
Copy link
Collaborator

66Ton99 commented Mar 16, 2018

Leave it as is. Put object as protected (or something like this, which will be ignored by phpValueToJs function) value it will allow extend base class and use all required fields for custom cases.

@bastos71
Copy link
Author

bastos71 commented Mar 16, 2018

You mean by creating a custom Constraint that will extends UniqueEntity constraint ?

@66Ton99
Copy link
Collaborator

66Ton99 commented Mar 16, 2018

Yes

@bastos71
Copy link
Author

bastos71 commented Mar 16, 2018

Couldn't we just add the $entityId as public attribute in the UniqueEntity class ?
That will cover 90% of the cases (I mean where entities have only ID as identifier / primary key)
It is global enough to be implemented directly in the bundle in my opinion

I pushed a new commit with my changes

@66Ton99
Copy link
Collaborator

66Ton99 commented Mar 19, 2018

This bundle used in many different ways, so it must cover at list 99% cases.
In other way you can redefine all classes you need and do everything you want.

@bastos71
Copy link
Author

Sorry but I don't agree with you, the only fact to include the variable id (only, and only if it is accessible) on both JS and PHP sides is global enough in my opinion to be implemented in the bundle.

I will maintain my own fork to get future updates, hope you will change your mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants