Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

php - How to validate a form with multiple models?

Basically, I am building an online shop where the user can create products with options (so, let's say, a size and a color). When all the options are set, they must provide a quantity and the SKU of each product.

I was thinking of making a two step form.

1st step: they select the name, description, images and create the options (size[sm, m, l, xl] and color[blue, red, yellow]). On the page after, I generated a list of forms for each option (small yellow jacket, small blue jacket, small white jacket, medium yellow jacket, medium blue jacket, medium white jacket, etc.)

Basically, a lot of lines of form that I have to validate. However, I don't see how I can get the data and validate it.

The generated names will each have an associated id and that id needs to be in the form. I was thinking of putting it in the names (sku-1, quantity-1, but this wouldn't work for validation). Thought about making an array but how would I know which value belongs to which product?

How do you handle form validation/matching when you have a lot of models to work with like in my case?

question from:https://stackoverflow.com/questions/65867000/how-to-validate-a-form-with-multiple-models

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You probably need to manually create a validator

Basic validation looks like this (from the docs linked above):

Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
])->validate();

If your validation is more complex than static checks on certain attributes, you'll need to write something a bit more complex to match.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...