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

[FrameworkBundle] add isSubmittedFormValid method to AbstractController #54743

Closed
wants to merge 4 commits into from

Conversation

Oviglo
Copy link
Contributor

@Oviglo Oviglo commented Apr 26, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

In controllers, we often have to call three functions to validate a form:handleRequest, isSubmitted and isValid. I wrote a new method that calls handlerequest and return the result of the condition isSubmitted() && isValid()

@carsonbot

This comment was marked as resolved.

@carsonbot carsonbot added this to the 7.1 milestone Apr 26, 2024
@Oviglo Oviglo marked this pull request as draft April 26, 2024 08:47
@carsonbot carsonbot changed the title [AbstractController] isSubmittedFormValid function [FrameworkBundle] [AbstractController] isSubmittedFormValid function Apr 26, 2024
@Oviglo Oviglo force-pushed the controller/submitted-form-valid branch from 3a6e34d to 33a79e8 Compare April 26, 2024 11:15
@Oviglo Oviglo marked this pull request as ready for review April 26, 2024 11:19
@Oviglo Oviglo requested a review from derrabus April 26, 2024 11:59
@derrabus derrabus changed the title [FrameworkBundle] [AbstractController] isSubmittedFormValid function [FrameworkBundle] isSubmittedFormValid function Apr 26, 2024
@Oviglo Oviglo requested a review from mttsch April 26, 2024 13:11
@OskarStark OskarStark changed the title [FrameworkBundle] isSubmittedFormValid function [FrameworkBundle] add isSubmittedFormValid method Apr 27, 2024
@OskarStark OskarStark changed the title [FrameworkBundle] add isSubmittedFormValid method [FrameworkBundle] add isSubmittedFormValid method to AbstractController Apr 27, 2024
@smnandre
Copy link
Contributor

I find a bit strange to not differenciate isSubmitted() & isValid() 😐

And when a form is not submitted, any later call to "isValid()" will throw an exception afterwards.

Meaning when this method will return false you will need aditional checks to define next steps / actions :|

@yceruto
Copy link
Member

yceruto commented Apr 27, 2024

For reference, this same feature was implemented on the controller before #24576 and reverted in #29813.

@smnandre
Copy link
Contributor

For reference, this same feature was implemented on the controller before #24576 and reverted in #29813.

Thank you, very interesting reading!

Most convincing arguments (imho):

  • prefix is should not do something
  • it will be harder to learn or explain

@Oviglo
Copy link
Contributor Author

Oviglo commented Apr 29, 2024

I find a bit strange to not differenciate isSubmitted() & isValid() 😐

And when a form is not submitted, any later call to "isValid()" will throw an exception afterwards.

Meaning when this method will return false you will need aditional checks to define next steps / actions :|

You made a good point for the prefix 'is' maybe another name like 'checkSubmitFormValid'.

I think this function can be increase productivity by less writing code, it's optionnal and averyone can use handleRequest() and isSubmitted() && isValid() process or write like this:

public function new(): Response
{
    $entity = new Article();
    $form = $this->createForm(ArticleType::class, $article);

    if ($this->isSubmittedFormValid($form)) {
        // ...    
    }

    return $this->render('@Twig/template.html.twig', [
        'form' => $form, // Generate 422 status code if form is'nt valid (see doRender method)
    ]);
}

And there is less risk of bugs for beginners by forgetting handleRequest

@OskarStark OskarStark changed the title [FrameworkBundle] add isSubmittedFormValid method to AbstractController [FrameworkBundle] add isSubmittedFormValid method to AbstractController Apr 29, 2024
@derrabus
Copy link
Member

prefix is should not do something

I agree, but that's a solvable naming issue.

it will be harder to learn or explain

Yes, that's the danger with all of those utility functions that the abstract controller ships. However, developers can always look into how that method is implemented.


And there is less risk of bugs for beginners by forgetting handleRequest

Okay, but if they do, what's the worst that could happen? The form is not processed at all? 🤷🏻‍♂️

@smnandre
Copy link
Contributor

Maybe renaming the method isFormSubmittedAndValid would better describe what it does ?

(Maybe just me, but i feel isSubmittedForm imply the form is submitted)

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

This is a 👎 on my side.

/**
* Call FormInterface::handleRequest() and return the result of the condition FormInterface::isSubmitted() and FormInterface::isValid().
*/
protected function isSubmittedFormValid(FormInterface $form): bool
Copy link
Member

Choose a reason for hiding this comment

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

Honestly, I don't like this alternative. It makes it less obvious that we're dealing with a request here.

Since all methods involved concern the form only, I don't see why the controller should communicate with the form when the form can communicate directly with the request, making this shortcut helpful for controllers not extending AbstractController.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like my old PR: #54726 ?

@OskarStark
Copy link
Contributor

I agree with @yceruto 👎

@Oviglo
Copy link
Contributor Author

Oviglo commented Apr 30, 2024

Thank you for your interesting feedbacks, now, I don't know what I need to do :)

@derrabus
Copy link
Member

@Oviglo I think, we won't find a solution that everybody's happy with. My personal recommendation would be to create a trait with this utility function in your own codebase and use that.

I'm going to close this proposal because of too many downvotes. Thank you for giving it a try, though.

@derrabus derrabus closed this Apr 30, 2024
@Oviglo
Copy link
Contributor Author

Oviglo commented Apr 30, 2024

I understand, thanks you all, I won personnal XP with your comments.

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

Successfully merging this pull request may close these issues.

None yet

8 participants