JsFormValidatorBundle icon indicating copy to clipboard operation
JsFormValidatorBundle copied to clipboard

Added field entityId to requests when validating unique fields

Open bastos71 opened this issue 7 years ago • 8 comments

Resolving issue #143

bastos71 avatar Mar 16 '18 15:03 bastos71

Okay no problem I think you're right :)

Just a few minutes and I will commit it

bastos71 avatar Mar 16 '18 15:03 bastos71

@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, ','));
        }
        //.....
    }
//...

bastos71 avatar Mar 16 '18 15:03 bastos71

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.

66Ton99 avatar Mar 16 '18 15:03 66Ton99

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

bastos71 avatar Mar 16 '18 15:03 bastos71

Yes

66Ton99 avatar Mar 16 '18 16:03 66Ton99

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

bastos71 avatar Mar 16 '18 16:03 bastos71

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.

66Ton99 avatar Mar 19 '18 15:03 66Ton99

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

bastos71 avatar Mar 19 '18 17:03 bastos71