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 support for editable association fields from type choice in Lis… #4449
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
811dfb5
Added support for editable association fields from type choice in Lis…
christingruber ea45a19
Fix store original value for editables from request before transforming
christingruber 9d4cc2b
Fix http status handling for json responses on editables
christingruber d0abfe3
Added documentation for editable association from type choice
christingruber dd56081
Update spelling for error messages
christingruber daaeb14
Tiny changes for cs
christingruber 7fce916
Update spelling for error messages
christingruber 712afaf
Updated existing tests for new json responses
christingruber File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,47 +227,44 @@ public function setObjectFieldValueAction(Request $request) | |
$field = $request->get('field'); | ||
$code = $request->get('code'); | ||
$objectId = $request->get('objectId'); | ||
$value = $request->get('value'); | ||
$value = $originalValue = $request->get('value'); | ||
$context = $request->get('context'); | ||
|
||
$admin = $this->pool->getInstance($code); | ||
$admin->setRequest($request); | ||
|
||
// alter should be done by using a post method | ||
if (!$request->isXmlHttpRequest()) { | ||
return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a XmlHttpRequest request header')); | ||
return new JsonResponse('Expected an XmlHttpRequest request header', 405); | ||
} | ||
|
||
if ($request->getMethod() != 'POST') { | ||
return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a POST Request')); | ||
return new JsonResponse('Expected an POST Request', 405); | ||
} | ||
|
||
$rootObject = $object = $admin->getObject($objectId); | ||
|
||
if (!$object) { | ||
return new JsonResponse(array('status' => 'KO', 'message' => 'Object does not exist')); | ||
return new JsonResponse('Object does not exist', 404); | ||
} | ||
|
||
// check user permission | ||
if (false === $admin->hasAccess('edit', $object)) { | ||
return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid permissions')); | ||
return new JsonResponse('Invalid permissions', 403); | ||
} | ||
|
||
if ($context == 'list') { | ||
$fieldDescription = $admin->getListFieldDescription($field); | ||
} else { | ||
return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid context')); | ||
return new JsonResponse('Invalid context', 400); | ||
} | ||
|
||
if (!$fieldDescription) { | ||
return new JsonResponse(array('status' => 'KO', 'message' => 'The field does not exist')); | ||
return new JsonResponse('The field does not exist', 400); | ||
} | ||
|
||
if (!$fieldDescription->getOption('editable')) { | ||
return new JsonResponse(array( | ||
'status' => 'KO', | ||
'message' => 'The field cannot be edit, editable option must be set to true', | ||
)); | ||
return new JsonResponse('The field cannot be edited, editable option must be set to true', 400); | ||
} | ||
|
||
$propertyPath = new PropertyPath($field); | ||
|
@@ -291,6 +288,28 @@ public function setObjectFieldValueAction(Request $request) | |
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN); | ||
} | ||
|
||
// Handle entity choice association type, transforming the value into entity | ||
if ('' !== $value && $fieldDescription->getType() == 'choice' && $fieldDescription->getOption('class')) { | ||
// Get existing associations for current object | ||
$associations = $admin->getModelManager() | ||
->getEntityManager($admin->getClass())->getClassMetadata($admin->getClass()) | ||
->getAssociationNames(); | ||
|
||
if (!in_array($field, $associations)) { | ||
return new JsonResponse(sprintf('Unknown association "%s", association does not exist in entity "%s", available associations are "%s".', | ||
$field, $this->admin->getClass(), implode(', ', $associations)), 404); | ||
} | ||
|
||
$value = $admin->getConfigurationPool()->getContainer()->get('doctrine')->getManager() | ||
->getRepository($fieldDescription->getOption('class')) | ||
->find($value); | ||
|
||
if (!$value) { | ||
return new JsonResponse(sprintf('Edit failed, object with id "%s" not found in association "%s".', | ||
$originalValue, $field), 404); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not PSR-compliant, although not detected yet. Please put each |
||
} | ||
} | ||
|
||
$this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null); | ||
|
||
$violations = $this->validator->validate($object); | ||
|
@@ -302,7 +321,7 @@ public function setObjectFieldValueAction(Request $request) | |
$messages[] = $violation->getMessage(); | ||
} | ||
|
||
return new JsonResponse(array('status' => 'KO', 'message' => implode("\n", $messages))); | ||
return new JsonResponse(implode("\n", $messages), 400); | ||
} | ||
|
||
$admin->update($object); | ||
|
@@ -313,7 +332,7 @@ public function setObjectFieldValueAction(Request $request) | |
|
||
$content = $extension->renderListElement($this->twig, $rootObject, $fieldDescription); | ||
|
||
return new JsonResponse(array('status' => 'OK', 'content' => $content)); | ||
return new JsonResponse($content, 200); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Post starts with a consonant sound, so "a"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops!