0

So I am developing a web page where people can buy some t-shirts, and they can choose colors, size, and quantity, the problem is, that the user can edit the size and color on html source, so I need to validate if the size match with enum on database and the color with the colors available on the table! I need to do this on request validation, and so far I have this:

    return [
        'tam' => 'required',
        'color' => 'required',
        'quantity' => 'required|numeric|min:1|max:20',
    ];

And the page has this:

enter image description here

Database enum has this:

enter image description here

with the "tamanho" field as enum, how can I verify if the "tamanho" on webpage is one of the enum values from database

Gonçalo Bastos
  • 376
  • 3
  • 16
  • 2
    Does this answer your question? [Laravel IN Validation or Validation by ENUM Values](https://stackoverflow.com/questions/28976658/laravel-in-validation-or-validation-by-enum-values) – Abishek Jul 20 '21 at 16:57
  • Also refer to this question as well https://stackoverflow.com/questions/43317806/laravel-validation-with-enum-column/45398621 – Abishek Jul 20 '21 at 16:58
  • @Abishek That okey, but how can I make it dynamically, so if the database changes, this changes to – Gonçalo Bastos Jul 20 '21 at 16:59
  • I need to check dynamically the enum values – Gonçalo Bastos Jul 20 '21 at 17:12
  • I am afraid, there isn't a good way to do this, unless and otherwise you create a custom rule that uses the Query Builder to query the `INFORMATION SCHEMA` and get the enum values dynamically and use it on the rule. That's just an overhead you don't want. – Abishek Jul 20 '21 at 17:14
  • I think as suggested by @Abishek first link will best suite. instead of accessing table information – John Lobo Jul 20 '21 at 17:17

1 Answers1

0

Your problem is one of the reasons ENUM is "evil". :) http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

Keeping the ENUM, the solution is to query the ENUM values. Please refer to: How can I get enum possible values in a MySQL database?

The cleaner solution would be to store sizes in a separate table and reference them in your original table. This way you could use "normal" validation stuff.

Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19