javascript - Laravel validation with vuex store always responded error 422
Solution:
this code will only work when you will validate one country per post:
$this->validate($request, ['iso' => 'required']);
But you are storing n countries, so you will have to validate the array:
$this->validate($request, ['countries.*.iso' => 'required']);
Answer
Solution:
the problem is with your validation rules. When you write
$this->validate($request, ['iso' => 'required']);
it means that you request requires a field "iso" to be validated, and in your post
axios.post('/api/countries/upsert',{
countries: state.countries,
})
you only have a "countries" prop and "iso" is considered empty so your required rules is always declined.
If you are trying to validate that every country in your countries array needs to have a required "iso" field you should check how to validate arrays here Laravel Arrays Validation
Source