简体   繁体   中英

Laravel custom validation on registration form

I'm currently struggling with a bit of validation on a registration form.

Basically when a user registers it will check if the unique code they have entered is valid and if not doesn't let them sign up.

But in my codes table which this reads from I also have an expiry date on the code.

I need to do another check after it is deemed valid that the expiry date hasn't passed, in other words it is not greater than now.

I think you can do this in the validator but I'm struggling a bit with the syntax and not sure where it should go. Here is my code:

protected function validator(array $data)
{

    return Validator::make($data, [
        'code' => 'required|exists:codes',
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'date_of_birth' => 'required|date',
        'password' => 'required|min:6|confirmed',
        'accept_terms' => 'required|accepted',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{   
    Code::where('code', $data['code'])->increment('uses');

    $data['code_id'] = Code::where('code', $data['code'])->value('id');

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'date_of_birth' => $data['date_of_birth'],
        'accept_terms' => $data['accept_terms'],
        'code' => $data['code'],
        'code_id' => $data['code_id'],
        'password' => bcrypt($data['password']),
    ]);
}

Thanks in advance :)

As long as you're using Laravel v5.3.18 (or higher) you can use the Rule Class to save you having to define a custom rule.

This:

'code' => 'required|exists:codes',

Can be replaced with:

'code' => [
    'required',
    Rule::exists('codes')->where(function ($query) {
        $query->where('expiry_date', '>=', Carbon::now());
    }),
],

(the above is assuming expiry_date is the actual name of the column in your db table).

Documentation: https://laravel.com/docs/5.3/validation#rule-exists

Just make sure you import those facades.

Hope this helps!

You can create custom validation rule . Create service provider and register it. Then add something like this:

Validator::extend('is_code_valid', function($attribute, $value, $parameters, $validator) {
    $code = Code::where('code', $value)->where('date', '>', Carbon::now())->first();
    return !$code->isEmpty(); // Pass if valid code exists in DB.
});

And then use it:

'code' => 'required|is_code_valid',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM