mercredi 24 janvier 2018

How to use the rule validation in FormRequest class?

I have a Company, where I like to validate the update request. Until now I validated the request inside the update() action but I like to move this to its own CompanyUpdateRequest.

In the validation I check of the uniqueness of the tax number but of course I like to allow the same tax number for the very company.

'tax_number' => [
    'required',
    'string',
    Rule::unique('companies')->ignore($company->tax_number),
],

This works as long it is placed inside the action, where I have $company already:

public function update(Request $request, Company $company) 
{
}

My question is now, how I get the company inside the CompanyUpdateRequest?

I know that I could put the ID of the company inside a hidden field in the form, send it along with the request, pull the company from DB ... but this feels kind of wrong. Does anybody have a better / another approach or idea?

Thanks a lot.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire