Skip to content

feat: [Validation] add method to get the validated data - #7420

Merged
kenjis merged 6 commits into
codeigniter4:4.4from
kenjis:feat-Validation-getValidated
Apr 12, 2023
Merged

kenjis merged 6 commits into
codeigniter4:4.4from
kenjis:feat-Validation-getValidated

Conversation

@kenjis

@kenjis kenjis commented Apr 11, 2023

Copy link
Copy Markdown
Member

Description

  • add Validation::getValidated()

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added enhancement PRs that improve existing functionalities 4.4 labels Apr 11, 2023
@kenjis kenjis changed the title feat: add method to get the validated data feat: [Validation] add method to get the validated data Apr 11, 2023
@iRedds

iRedds commented Apr 11, 2023

Copy link
Copy Markdown
Collaborator

I'd like to hear the reason for adding this method.
What is the real benefit of it?

@kenjis

kenjis commented Apr 11, 2023

Copy link
Copy Markdown
Member Author

Security enhancement.
This method returns only validated data, so developers can be sure to use validated data.

@iRedds

iRedds commented Apr 11, 2023

Copy link
Copy Markdown
Collaborator

Let's say the script receives data, some of which do not need to be validated.
It turns out that after validation, the getValidated() method will return only part of the data, which will add an extra operation to manipulate the data further.
For me, this feature looks dubious.

@kenjis

kenjis commented Apr 11, 2023

Copy link
Copy Markdown
Member Author

Let's say the script receives data, some of which do not need to be validated.

This method is intended for input validation like validation for HTML form.
What data don't you need to validate?

@ddevsr

ddevsr commented Apr 11, 2023

Copy link
Copy Markdown
Collaborator

For me, currently develop upload batch by excel and will return callback failed excel with marker like alreadyExists in column description.

Upload:

1|John Doe|2023-02-01|
2|Maria|2024-04-14|

Callback:

1|John Doe|2023-02-01|DONE
2|Maria|2024-04-14|alreadyExists

I think this function can use for this case and combine failed validation

@kenjis

kenjis commented Apr 12, 2023

Copy link
Copy Markdown
Member Author

Added the docs.

];

if ($validation->run($data)) {
$validatedData = $validation->getValidated();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This example is fine, but I would like to ask you to add an example that will cover controller validation (with the $this->validate() method). This is the place where this method is the most valuable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have a plan to add a method to get validated data in Controller, and update all sample code with $this->validate().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think need a separate method in the controller for this, though.
Since we can get the errors via $this->validator->getErrors(), I see no problem with getting validated data the same way, via $this->validator->getValidated().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, I see. Thank you for opinion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See #7425

@kenjis
kenjis merged commit 5be9023 into codeigniter4:4.4 Apr 12, 2023
@kenjis
kenjis deleted the feat-Validation-getValidated branch April 12, 2023 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants