in this laravel validator class i want to choose specify id
for required
, when we define specify is for example parent
shouldn't required or status
and i don't know how can i make this validation
for example:
'parent'=>'required/*when id is not 1*/|integer',
'status'=>'required/*when id is not 1*/|string',
class RequestCategory extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'category_name'=>'required|string|min:5:max:20',
'parent'=>'required|integer',
'status'=>'required|string',
];
}
}
You can use Laravel's required_if validation in order to achieve this. Sintax goes like this:
'field_name' => 'required_if:columnName:value'
I think you could try this:
public function rules()
{
return [
'category_name'=>'required|string|min:5:max:20',
'parent'=>'required_if:status,!=,1|integer',
'status'=>'required_if:parent,!=,1|string',
];
}
PS: Not tested, if there are any errors here, let me know.