phplaravelvalidationruleslaravel-validation

Validate a field: required_if the url contains parameter in Laravel


Is there any way to validate required field when the requested url contains some parameter?


Solution

  • Assuming you are using Form requsts, you can simple use PHP condition.

    public function rules()
    {
        $rules = []; // here you put some rules
    
        // here you check condition and add some rule when it's true
        if (str_contains($this->input('url'), 'something')) {
           $rules['some_other_field'] = 'required';
    
        }
    
        return $rules;
    }