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

[5.6] Provide access to validated dataset #23385

Closed
wants to merge 1 commit into from
Closed

[5.6] Provide access to validated dataset #23385

wants to merge 1 commit into from

Conversation

adam1010
Copy link
Contributor

@adam1010 adam1010 commented Mar 4, 2018

Currently in your controller if you're using:
$data = $request->validate([...]);

If you need to switch to using advanced rules, there is no firect method available for accessing the validated data:

$validator = Validator::make($request->all(), [...]);
$validator->sometimes('phone', 'required', function(){ ... });
$validator->validate();
// Now how do you get your $data variable?

$data = $validator->getDataForRules(); // my new method
// OR
$data = $validator->getDataValidated(); // this takes care of calling validate()

@GrahamCampbell GrahamCampbell changed the title Provide access to validated dataset [5.6] Provide access to validated dataset Mar 4, 2018
@GrahamCampbell GrahamCampbell changed the title [5.6] Provide access to validated dataset [5.7] Provide access to validated dataset Mar 4, 2018
public function getDataForRules()
{
$ruleKeys = collect($this->getRules())->keys()->map(function ($rule) {
return str_contains($rule, '.') ? explode('.', $rule)[0] : $rule;
Copy link
Member

Choose a reason for hiding this comment

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

Just a small optimization, but I suspect the following is faster and does the same:

return explode('.', $rule, 2)[0];

The str_contains is not necessary, and the limit arg to explode will increase performance by early termination.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great -- I actually copied that line from Foundation/Providers/FoundationServiceProvider.php so the optimization can be made there as well.

Is there any way this can go into 5.6? I really need this in a project I'm working on.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please target the 5.6 branch. I marked it as 5.7 because you targetted master. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! I've rebased off the 5.6 branch and implemented your change. Are unit tests needed or is the PR good as-is?

Copy link
Member

Choose a reason for hiding this comment

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

Tests would be great please. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added tests. Anything else I can do to get this merged faster?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TravisCI appears to be failing due to another commit that is already on 5.6

Cannot redeclare Illuminate\Tests\Database\DatabaseQueryBuilderTest::testWhereTimePostgres

@adam1010 adam1010 changed the base branch from master to 5.6 March 5, 2018 00:18
@adam1010 adam1010 changed the title [5.7] Provide access to validated dataset [5.6] Provide access to validated dataset Mar 5, 2018
@deleugpn
Copy link
Contributor

deleugpn commented Mar 5, 2018

Can't the validate method itself return the data like the validate in the request?

@adam1010
Copy link
Contributor Author

adam1010 commented Mar 5, 2018

@deleugpn Currently validate() does not return anything -- are you saying we should have it return the validated data from my new function?

return $this->getDataForRules();

That would probably be preferable, but I didn't want to delay this PR if that was going to be a controversial change. If it's not controversial I can delete my getDataValidated() method and instead just have the validate() method return $this->getDataForRules();

@deleugpn
Copy link
Contributor

deleugpn commented Mar 5, 2018

I guess that would be a breaking change, yes. So maybe you can open a separate PR to change that tagging 5.7 while keeping this one as-is tagging 5.6

@taylorotwell
Copy link
Member

Probably validated could return the data being validated similar to $request->validate() Feel free to open a new PR for that.

@adam1010
Copy link
Contributor Author

adam1010 commented Mar 5, 2018

@taylorotwell Thanks Taylor -- I've updated the code and created a new PR to add a return value for validate() so that it mimics $request->validate()

#23397

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