samedi 17 juin 2023

Laravel Custom Rule Validation with Array of Form Fields

I want to access attribute values in the custom validation rule I created.

HTML

Designation Dropdown
<select name="resource[{?}][designation]>
<option value=1>First Value</option>
<option value=2>Second Value</option>
</select>

Allocation Dropdown
<select name="resource[{?}][allocation]>
<option value=1>First Value</option>
<option value=2>Second Value</option>
</select>

Controller Code

'resource.*.allocation' => ['required',new ResourceQuotation()],
'resource.*.designation' => 'required',

Laravel Custom Rule - ResourceQuotation

public function passes($attribute, $value)
{

$designation = request()->input("resource.*.designation"); <!--//I am confused here, how to access the designation array value since we might have multiple.!->

$allocations = ResourcePrice::select('allocation','fixed_allocation')->where('id',$designation)->first();

 if($allocations->fixed_allocation=1){
    $this->message = "Maximum allocation allowed is 10%";
    return false;
 }

 else{
   return true;
 }

}

public function message()
{
 return $this->message;
}

First I thought to use a loop but how to display individual message errors? I found this tutorial but they are using it for a single value comparison, not with different field attributes.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire