Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 1 year ago.
0

For starters, the logic you posted above should be encapsulated within some service (a validator or something), not directly in a controller. Maybe it is already, not sure.

Secondly, it's really weird that a user would be specifying the primary key of a model in a form, that's not normal.

Thirdly, it mostly sounds like you're talking here about functions within the context of validation. E.g. - A user submits some data in a form, it gets validated, and some messages get returned.

Can you give some more context as to what you're trying to do? I think what you're really after is a standard / best practices way to validate form data, but not entirely sure.

Last updated 1 year ago.
0

thx for the reply... much appreciated.

I am writing a stats package for a popular online game. The data is pulled via JSON APIs from the game's official site and I take the data and make it more meaningful/personalized.

So the unique ID I pseudo-coded above is not the primary key of the user model (I have one of those for users/members of my site itself) but is the unique key for the game player I am tracking stats for.

I suppose I could add a local primary key for this but the use case is the same, I need to check to see if the entered value is

  • a valid player identifier (which I do by calling the remote API)
  • not already in my local database as a tracked player

Now that you mention it, though... why wouldn't I not just add a Validator on the form and add a unique rule for it? I was trying to use the model to validate, when in fact it's the form that needs checking (against the model in this case). Thank you for pointing that out. It wasn't that obvious as I was staring at my code last night ;)

oh, and yes, I have a helper class to do the actual validation outside the controller.

Last updated 1 year ago.
0

Yep totally, sounds like you're on the right track. Laravel has a MessageProviderInterface that I like to implement on my validators just to keep things consistent...

https://github.com/laravel/framework/blob/master/src/Illuminat...

Whether the input comes from a form or an API makes nearly no difference, like you said, just pass it to your validator.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.