title: Laravel custom validation rule that works with not required empty field
body: <p>I have required file field which is recorded in the database as a path. I want to make it optionally required if its field in the database is null. In the <code>update</code> action of my controller I have set the following validation:</p>
  
  <pre><code>$this-&gt;validate(request(),[
      'drawings' =&gt; 'requiredIfEmpty:'.$product-&gt;drawings.'|file|max:'. config('fox.photoMaxSize').'|mimes:pdf',
  </code></pre>
  
  <p>Then in <code>app/Providers/AppServiceProvider.php</code> I defined <code>requiredIfEmpty</code> validator:</p>
  
  <pre><code>Validator::extend('requiredIfEmpty',function ($attribute,$value,$parameters,$validator){
          if(is_null($parameters[0]) || empty($parameters[0])){
              if (is_null($value)){
                  return false;
              }
          }
          return true;
      });
  
      Validator::replacer('requiredIfEmpty', function ($message, $attribute, $rule, $parameters) {
          return __('The :attr is required',['attr' =&gt; $attribute]);
      });
  </code></pre>
  
  <p>In the view <code>_form</code> I use <a href="/questions/tagged/laravelcollective" class="post-tag" title="show questions tagged &#39;laravelcollective&#39;" rel="tag">laravelcollective</a> form helper like the following for the <code>drawings</code> field:</p>
  
  <pre><code>&lt;div class="form-group {{$errors-&gt;first('drawings','has-error')}}"&gt;
      @if (!is_null($product-&gt;drawings))
      &lt;a href="{{$product-&gt;drawings}}" target="_bfox"&gt;&lt;img src="/imgs/pdf.png" alt="{{__('PDF File')}}" title="{{__('PDF File')}}" /&gt;&lt;/a&gt;
      &lt;br&gt;
      @else
      &lt;img src="/imgs/empty.png" width="64" height="64" alt="{{__('Empty')}}..." title="{{__('Empty')}}..." /&gt; &lt;br&gt;
      @endif
      {!! Form::label('drawings', __('Drawings')) !!}        
  
          {!! Form::file('drawings',['class' =&gt; 'btn btn-info','title' =&gt;__('PDF file')]); !!}
          @php ($eleE =  $errors-&gt;first('drawings'))
          @include('layouts.form-ele-error')
  
  &lt;/div&gt;
  </code></pre>
  
  <p>The problem is, my custom validation rule does not invoked because the field is not required and it has null value. I need any way that allows the two scenarios:</p>
  
  <ol>
  <li>when the <code>drawings</code> file field is empty and <code>$product-&gt;drawings</code> is not null, there's no validation occured </li>
  <li>when the <code>drawings</code> file field is empty and <code>$product-&gt;drawings</code> is null, the validation occurred.</li>
  </ol>
  
  <p>In other words, I need a built in validation rule like <a href="https://laravel.com/docs/5.4/validation#rule-required-if" rel="nofollow noreferrer"><code>requiredIf</code></a> but it <strong>does not take another</strong> form field as parameter, it just take another value and it always works even the form field value is empty and the field is not required. </p>