Almost all things in laravel can have customization. See https://laravel.com/docs/5.3/validation#manually-creating-validators
validation required_unless != ?
i was wondering if there was a built-in validation in laravel 5.3 that means a field is required unless another field does not equal to a value. let's say i have a products table and a categories table. i have an entry in categories called "None Specified". i have a column called description in products. I only want products.description to be required if products.category_id != "None Specified".
i know there's a required_unless validation rule but i'd have to supply all category IDs that != "None Specified" for that to work. so far i've had to manually enter them and that does not scale. i also seen not_in validation. just can't seem to put things together.
i could create a custom validation rule but wanted to see if i missed something already made.
@jlrdw @thoughtControl ya i've created some custom ones but can't seem to get it right. it's weird that the validation is only triggered when there's a value. but i'm validating required_if. i can't let it ignore if there's no value at all. i ended up querying the database and getting the values that way
// get category IDs that is not "None Specified"
$categories = Category::select('id')->where('name', '!=', 'None Specified')->get();
// validation
$rules['description'] = 'required_if:category_id,'.$categories->implode('id', ',');
// custom message
$messages['description.required_if' => 'Description is required because of the selected category.';
the implode() separates the id with commas. hopefully in the future "not equal" is added to validation as well as >= <=. i had to create a custom one for less/greater than or equal to value. the existing validation only checks for greater/less than.
Please or to participate in this conversation.