3 years ago

Ignore form request required rules on update

Posted 3 years ago by shanekerwin

I'm really surprised to not find this mentioned anywhere, which makes me think I'm doing this wrong. He's my scenario:

I have a form with Name and URL. The Name is a required field. But what if you want to update just the URL? In that case the name is not required, because it's already there. But I want to keep the same formRequest for validation because it contains other validation rules (valid URL, max, min, unique etc.). I have solved the problem like this:

    public function rules()
        $requiredFields = [

        $rules = [
            'name' => 'max:255|unique:tableName,name',
            'url' => 'url|max:255'

        if ($this->method() == 'POST') {
            foreach ($rules as $field => $rule)
                if (in_array($field, $requiredFields))
                    $rules[$field] .= '|required';

        return $rules;

But ...really? This is not an issue for anyone else? What am I missing?

Please sign in or create an account to participate in this conversation.