3
votes

How would you add multiple validation rules to a model?

I've created a BaseModel where the BaseModel extends Eloquent. My other models extends the BaseModel.

models/BaseModel

class BaseModel extends Eloquent {
public $errors;

public function validate($data) {

    $validation  = Validator::make($data, static::$rules);

    if($validation->passes()) {
        return true;
    }

    $this->errors = $validation->messages();

    return false;
}

}

models/User

    protected static $rules = [
    'email_address' => "required|email|unique:users,email_address",
    'first_name' => "required",
    'last_name' => "required",
    'password' => "required|min:6|same:confirm_password",
    'confirm_password' => "required:min:6|same:password",

];

The above rules applies to the registration page.

Now my login page will also be going through the Users model.

It should validate the email address and password.

Is it possible to add multiple validation rules to a model.

I'm sure I can just make $rules a variable which gets passed the validation function but that doesn't seem clean. eg

model/BaseModel

public function validate($data, $rules) {
3
It is rather not model job to validate upon login. Leave that to something else, as model should be responsible (if you want it to) only for creation of a new record. Validating login credentials is another story, and in fact you don't need that at all. Unless there is a reason for that which you want to share?Jarek Tkaczyk
I'm not validating log in, I'm trying to have multiple validation rules.user742736

3 Answers

0
votes

I solved this by storing the $rules together in my models methods. In this example I am validating a user update. I use a validate class method from within this class. Things can be organized better, but I'm working on that.

This is code from my EloquentUserRepository or User model, but I use repositories.

EloquentUserRepository:

public function update($user) {
    $rules = array(
        "email" => "required|email",
        "userrole" => "required"
    )
    if($this->validate(Input::all(), $rules)) {
        $user->save();
        return true;
    } else {
        return false;
    }
}

private function validate($data, $rules) {
    $validator = Validator::make($data, $rules);
    if($validator->passes()) {
        return true;
    } else {
        $this->errors = $validator->errors();
        return false;
    }
}

public function errors() {
    return $this->errors()->all();
}

This works great and keeps my controllers clean and shows my validation rules where they make sense.

0
votes

Maybe your can try various type of validate function
Ex: $User->validateUpdate then inside your User model

private function validateUpdate($data){
    $validator = Validator::make($data, $this->updateRules);
    if($validator->passes()) {
         return true;
    } else {
        $this->errors = $validator->errors();
        return false;
    }
}

public function errors() {
    return $this->errors()->all(); 
}
0
votes

I know it's been so long but answer if someone wants more answer

class LaravelValidation implements UsersValidation
{
    /**
     * Validate user inputs
     *
     * @param  array  $data
     * @return bool
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function validate(array $data, $method='create'): bool
    {
        $rules = $this->$method();

        $validator = Validator::make($data, $rules);

        if ($validator->fails()) {
            if(request()->wantsJson())
            {
                return response()->json($validator->errors(), 400);
            }
            throw new ValidationException($validator);
        }

        return true;
    }

    public function social_login()
    {
        $rules = [
            'access_token'=> ['required']
        ];

        return $rules;
    }