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

Batch action $allItems needs to be an boolean #6251

Merged

Conversation

nieuwenhuisen
Copy link
Contributor

Batch action $allItems needs to be an boolean

Exception:

Argument 4 passed to Sonata\AdminBundle\Admin\AbstractAdmin::preBatchAction() must be of the type bool, string given, called in /projects/sonata-sandbox/vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php on line 452

I am targeting this branch, because there is the typehint added.

Changelog

### Changed
- Cast $allItems to an boolean to prevent typehint error.

$request->request->replace(array_merge($request->request->all(), $data));
} else {
$request->request->set('idx', $request->get('idx', []));
$request->request->set('all_elements', $request->get('all_elements', false));

$action = $request->get('action');
$idx = $request->get('idx');
$allElements = $request->get('all_elements');
$allElements = (bool) $request->get('all_elements');
Copy link
Member

Choose a reason for hiding this comment

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

What about using $request->request->getBoolean('all_elements')? There is other get just above that could be changed too.

This code is a bit strange BTW, it first get the all_elements, then sets it to the request with the same value, and then get it again to assign it to a var. 🤔

@nieuwenhuisen nieuwenhuisen force-pushed the batch_action_all_items_bool branch from 3f3d14b to 4449997 Compare August 3, 2020 14:30
$request->request->set('all_elements', $request->get('all_elements', false));

$allElements = $request->request->getBoolean('all_elements');
$request->request->set('all_elements', $allElements);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$request->request->set('all_elements', $allElements); Is this needed?

Copy link
Member

@jordisala1991 jordisala1991 Aug 3, 2020

Choose a reason for hiding this comment

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

Maybe is in another parameter bag? That is the only explanation I can find for it.

Copy link
Member

Choose a reason for hiding this comment

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

Taking a look at 9949121#diff-b1f1981b1ed9490b32c9bc83360655c3R396 where all this started, looks like it started using Request::get() method and it has been like that since then, but taking into account that some lines above there is this check:

if (Request::METHOD_POST !== $restMethod)

I would say we can use in this block $request->request safely (batch actions are sent through POST).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oké is this meaning that the line $request->request->set('all_elements', $allElements); (line 377) can be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this is used to allow a combination of POST and GET parameters?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe this is used to allow a combination of POST and GET parameters?

https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Controller/CRUDController.php#L410, apparently only POST.

Copy link
Contributor Author

@nieuwenhuisen nieuwenhuisen Aug 4, 2020

Choose a reason for hiding this comment

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

But you can POST to a url for example /delete?all_elements=1

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but in this case:

I don't think they are later modified by JavaScript and seeing the <form> tag: https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Resources/views/CRUD/base_list.html.twig#L37 I would say that the URL only contains filter parameters.

And looking at how the request is built in CRUDControllerTest when using idx or all_elements.

So I think we can change this to just use $request->request.

@nieuwenhuisen nieuwenhuisen force-pushed the batch_action_all_items_bool branch 4 times, most recently from 8f10580 to 66ca282 Compare August 4, 2020 13:01
@nieuwenhuisen nieuwenhuisen requested a review from franmomu August 4, 2020 13:03
$action = $request->get('action');
$idx = $request->get('idx');
$allElements = $request->get('all_elements');
$action = $request->request->getAlnum('action');
Copy link
Member

Choose a reason for hiding this comment

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

what does the getAlnum?

Copy link
Member

Choose a reason for hiding this comment

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

Alpha numeric ?

Copy link
Member

Choose a reason for hiding this comment

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

    /**
     * Returns the alphabetic characters and digits of the parameter value.
     *
     * @param string $key     The parameter key
     * @param string $default The default value if the parameter key does not exist
     *
     * @return string The filtered value
     */
    public function getAlnum($key, $default = '')
    {
        return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
    }

Returns the alphabetic characters and digits of the parameter value.

and returns a string value. Ref: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/HttpFoundation/ParameterBag.php#L127

@franmomu
Copy link
Member

franmomu commented Aug 4, 2020

I think we can do this on 3.x branch.

@nieuwenhuisen nieuwenhuisen force-pushed the batch_action_all_items_bool branch from 66ca282 to 906a8b1 Compare August 5, 2020 07:36
@nieuwenhuisen nieuwenhuisen changed the base branch from master to 3.x August 5, 2020 07:37
@jordisala1991 jordisala1991 merged commit f04bca8 into sonata-project:3.x Aug 6, 2020
@jordisala1991
Copy link
Member

Thank you @nieuwenhuisen

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.

4 participants