-1
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
  • May I know the reason behind idea of shortening validation rule? Commented Nov 10, 2022 at 5:25
  • To shorten the code, I have multiple inputs with the same validation rule which is 'required'. Commented Nov 10, 2022 at 5:27
  • Unfortunately you have to write like this only, Currently There is no other way to achieve this. Commented Nov 10, 2022 at 5:33

3 Answers 3

0

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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()));

Comments

-1

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); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.