Home > Enterprise >  Multiple inputs in one validation rule
Multiple inputs in one validation rule

Time:11-10

return [
'contract_code' => 'required', 
'name' => 'required|string',
'abbreviation' => 'required|string',
'linecount_divisor' => 'required|integer'
];
// into input fields => 'required'

How to shorten the validation rule in multiple inputs?

CodePudding user response:

if You have the same validation rule for multiple and you want to shorten your code just use form requests.

php artisan make:request RequestName

And then in the controller Functions use it

public function save(RequestName $requestName)
{
}

don't forget to use that request class.

use App\Http\Requests\RequestName;

CodePudding user response:

Here is a solution assuming all input fields need to have a common rule like required.

$rules = array_map(function($curr) { return [$curr => 'required']; }, array_keys(request()->all()));
  • Related