deiucanta / laravel-smart

Automatic Migrations, Validation and More
48 stars 7 forks source link

rule customization #2

Closed vladflip closed 6 years ago

vladflip commented 6 years ago

What about a field customization per request? There are cases when you need to validate the fields differently (basic example is admin user). This is done with form requests. So I’m guessing this package should expose a static method in a model which returns new customized rules (including complete rule removal) which you can use in a form request.

deiucanta commented 6 years ago

@vladflip I just released an alpha version. Please check it out.

Someone raised the same issue on Twitter: https://twitter.com/themsaid/status/1031449131879985152

The basic idea is that the model should be consistent regardless the role of the user, the action being taken (the title shouldn't be longer than 100 characters, the price should be at least 0 and numeric, the slug should match a regex rule). Can you think of some edge cases where this is not true?

We can split the validation rules in two groups

  1. Model rules (these are for the fields that get stored in the database)
  2. Controller rules (these are mostly for the user interface, are not stored in the database; if you have a SPA, you don't need to send them to the server)

For a registration form

  1. Model rules (name, email, password)
  2. Controller rules (terms & condition checkbox is checked, password confirmation field is same with password)

Of course, if you want to send the "terms" checkbox value to the server, you can, but I'm just trying to make my point.

robsontenorio commented 6 years ago

I think roles are out from escope this package.

deiucanta commented 6 years ago

You're right @robsontenorio. User roles are out of the scope of this package but validation rules are right at the core. The questions is this: can user roles change validation rules?

deiucanta commented 6 years ago

I will close this until someone provides a real scenario where this is required.