How to validate combination unique for input array

Viewed 36

html code for the currency input array

<div class="form-group ">
  <label>Currency *</label>
  <select id="ddlCurrencies1"  name="currency[]" class="form-control ddl-form-control 
   @error('currency.*') is-invalid @enderror" required>                              
  </select>
  @error('currency.*')
  <span class="invalid-feedback" role="alert">
    <strong>{{ $message }}</strong>
  </span>
  @enderror 
</div>

validation in controller

$validatedData = Validator::make($request->all(),[
        'month'         => 'required|date|unique:sap_rate,month,NULL,NULL, currency.*',
        'rate.*'        => 'required|numeric|between:0.0001,999.9999',
        'currency.*'    => 'required|not_in:Choose State/Province|unique:sap_rate,currency.*,NULL,NULL,month',
        
    ],
    [
        'required'      => 'This field is required.',
        'date'          => 'Invalid input. It must be a date',
        'numeric'       => 'Invalid input. It must be number.',
        'between'       => 'Invalid input. It must between 0.0001-999.9999',
        'max'           => 'Please select a currency.',
        'not_in'        => 'Please select a currency.',
        'unique'        => 'This record already existed',
    ]);

    
    

if($validatedData->fails()){
    Log::debug('Fails to validate data');
    Log::debug('Invalid input');
    return redirect()->back()->withInput()->withErrors($validatedData);
}

I had tried several methods for the validation, but all the methods will returning errors.What I need to do is validated month input and currency input array, before insert both of them into database. This is because there is a unique index for these two columns in the database. The code showing here is giving the undefined offsets error.

0 Answers
Related