S
S
SvizzZzy2017-05-23 15:26:06
Laravel
SvizzZzy, 2017-05-23 15:26:06

How to use the validate setting multiple times for similar controllers?

The question refers to the Laravel 5.4 framework.
The following is of interest:
For example, there is such a check for the registration form:

$this->validate($request, [
        'name' => 'required|min:2|max:20',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
      ]);

It may well fit for some other forms, for example, for "updating a profile".
How can it be reused so as not to write the same thing a bunch of times?
In addition to Scope, I haven’t come up with anything yet.
public function scopeProfileValidate() {

        $data = [
          'name' => 'required|min:12|max:20',
          'email' => 'required|email|max:255|unique:users',
          'password' => 'required|min:6|confirmed',
        ];

          return $data;
      }

And then in the right places:
$this->validate($request, User::ProfileValidate());

Is it ok to use Scope for this or are there more elegant ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pantagruel964, 2017-05-23
@SvizzZzy

If you happen to read the documentation, you will come across this thing:
And no scopes and traits are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question