SonataAdminBundle icon indicating copy to clipboard operation
SonataAdminBundle copied to clipboard

Improve error handling in Ajax

Open jordisala1991 opened this issue 3 years ago • 4 comments
trafficstars

Subject

I am targeting this branch, because {reason}.

Closes #{put_issue_number_here}.

Changelog

### Added
- Added some `Class::newMethod()` to do great stuff.

### Changed

### Deprecated

### Removed

### Fixed

### Security

jordisala1991 avatar Sep 05 '22 07:09 jordisala1991

To be able to properly fix this: https://github.com/sonata-project/SonataPageBundle/issues/1495#issuecomment-1235626107

We will need to properly work with the errors. Currently there is no context on what field is generating the error.

On 3.x branch of AdminBundle, due to this return null: https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Controller/CRUDController.php#L1712-L1722 we were relying on html generated for the error page.

So, in 3.x of PageBundle:

If you are calling the createAction via ajax and the response is Success, it will arrive in the form of json, BUT if the response was failure, it was arriving in the form of HTML with the errors already in place.

It looks very bad, but now, in 4.x always the response is get via json, so without this PR it is impossible to know where to place each error. This PR gives each error a key which is the ID of the form field. (Also each field can have multiple errors, so we need to take into account that too.

jordisala1991 avatar Sep 05 '22 07:09 jordisala1991

wdyt @VincentLanglet ? Do you know any other solution for this?

jordisala1991 avatar Sep 05 '22 07:09 jordisala1991

I know that this needs probably to move some logic outside crud controller and test it and all, but does the code look good or should I find another way? @VincentLanglet

jordisala1991 avatar Sep 19 '22 07:09 jordisala1991

I know that this needs probably to move some logic outside crud controller and test it and all, but does the code look good or should I find another way? @VincentLanglet

Seems an OK strategy to me if it works for you. I assume you plan to move the private method to a service.

VincentLanglet avatar Sep 19 '22 07:09 VincentLanglet

This should be ready, have added tests, changelog and upgrade note.

Also took care of all comments and tried the compat with page bundle.

In the changelog and in the upgrade note is mentioned the possible (but unlikely) bc break.

Unlikely because I dont think anyone is using sonata as an api (and if they do, they probably got the same problems as we did on the page bundle: error output was broken)

jordisala1991 avatar Sep 26 '22 09:09 jordisala1991