Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72443

Changing exists rule based on choosen option

$
0
0

So i have this simple code to input value based on database value in registration form and it's work great,

The code below located at register.blade.php

  1. This for selecting branch

    <div class="form-group form-material floating">
      <select class="form-control" name="branch" id="branch">
          <option value="1">Option A</option>
          <option value="2">Option B</option>
      </select>
      <label class="floating-label" for="inputStatus">Branch</label>
    </div>
    
  2. This one for inputing reference

    <div class="form-group form-material floating {{ $errors->has('reference') ? ' has-error' : '' }}">
            <input type="text" class="form-control empty" id="reference" name="reference">
              <label class="floating-label" for="inputStatus">Reference</label>
              @if ($errors->has('reference'))
                  <span class="help-block">
                      <strong>{{ $errors->first('reference') }}</strong>
                  </span>
              @endif
          </div>
    

the code below located atRegisterController.php

  1. And this one for validation

    protected function validator(array $data)
     {
    
     return Validator::make($data, [
    'name' => 'required|string|max:255',
    'email' => 'required|string|email|max:255|unique:users',
    'password' => 'required|string|min:6|confirmed',
    'phone' => 'required|string|min:5|max:20',
    'rate' => 'required',
    'product' => 'required',
    'branch' => 'required',
    'reference' => [
        'required',
            Rule::exists('references')->where(function ($query) {
                if( 'branch' == 1 ){
                    $query->where('references.type', '=', "TOP"); 
                }
                else{
                    $query->where('references.type', '=', "BOTTOM");     
                }
            }),
        ],
       ]
      );
     }
    

In registration form when user choose the first option on branch, the user can only write (Example : 'ABC', 'DEF', 'GHI') if the user write another value it will return Error Message : 'Wrong reference code', but when the user choose second option on branch, the user can only write (Example : '123', '456', '789'), , does anyone have and idea how to do it? Thanks


Viewing all articles
Browse latest Browse all 72443

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>