skip to Main Content
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?

3

Answers


  1. 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 AppHttpRequestsRequestName;
    
    Login or Signup to reply.
  2. 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()));
    
    Login or Signup to reply.
  3. Try this one in Laravel

    $validation=array();
            $validation= [
                'contract_code' => 'required',
                'name' => 'required|string',
            'abbreviation' => 'required|string',
             'linecount_divisor' => 'required|integer'
            ];
           
            $this->validate($request,$validation); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search