Laravel custom validation on registration form

user6073700

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

Rwd

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Custom Wordpress Registration Form with Validation

From Dev

validation with middleware for registration form in laravel

From Dev

Devise custom validation for a custom field in the registration form

From Dev

Adding custom validation errors to Laravel form

From Dev

Laravel custom field name in form request validation

From Dev

Laravel Form Request Array Validation Custom Rules

From Dev

Django custom registration form

From Dev

Set custom validation message for field in laravel5 form validation?

From Dev

Django-registration, custom registration and validation

From Dev

Django-registration, custom registration and validation

From Dev

State validation in a JavaScript registration form

From Dev

Registration Form using Laravel

From Dev

laravel 5 registration form

From Dev

laravel 5 registration form

From Dev

Janrain custom js validation for registration

From Dev

Custom registration with Laravel 5.1

From Dev

Custom registration with Laravel 5.1

From Dev

Django Registration Redux custom registration form

From Dev

Drupal Custom registration form save

From Dev

Wordpress | Woocommerce custom registration form

From Dev

Drupal 8 custom registration form

From Dev

Remove custom registration form on wordpress

From Dev

How to give custom field name in laravel form validation error message

From Dev

Set One Custom message for multiple field in laravel form validation

From Dev

Custom directive for form validation

From Dev

Angular custom form validation

From Dev

Login and registration form in Laravel 5

From Dev

customize registration form in laravel 5

From Dev

Laravel 5.3 customizing registration form

Related Related

HotTag

Archive