Home > other >  Laravel dynamic validation
Laravel dynamic validation

Time:02-25

I'm using Laravel and I have a custom validation. I want to pass some value to the validation class and validate it against the DB so I keep all the validation logic in the validation class. Here's my code but the validation error is not triggered:

public function rules() {

    $userCode = UserCode::getCode();

    $rules = [
        'name' => 'required|string',
        'email' => 'required|email',

        $userCode => 'unique:user_codes,code,'. $userCode
    ];

    return $rules;
}

What am I doing wrong here? the validation is never triggered even if the value $userCode already exists in the DB. The validation is triggered if the request contains bad email for example.

Edit: I have this user_codes table that has a code column that must be kept unique. UserCode::getCode() produces this code. So I also want to check if this code is unique or not in the table user_codes before passing it to the controller. I already have the custom validator but the problem is that I want it to also validate this code. The validator works fine with the other request body. The problem is that this code is not passed from the request body but it is simply generated from another method.

CodePudding user response:

You could do this using a FormRequest class with the prepareForValidation() method. It should look something like this:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class WhateverRequest extends FormRequest
{
    protected function prepareForValidation(): void
    {
        $this->merge([
            'user_code' => UserCode::getCode(),
        ]);
    }

    public function rules(): array
    {
        return [
            'name' => 'required|string',
            'email' => 'required|email',
            'user_code' => 'unique:user_codes,code'
        ];
    }
}

CodePudding user response:

your question is not clear, I understand you have a column named by code

and you want to be unique, you can add usercode to request,

request()->merge(['userCode'=>$userCode]);

then

$rules = [
        'name' => 'required|string',
        'email' => 'required|email',

        'userCode' => 'unique:user_codes,code,'.UserCode::getId().',id'
    ];

I answered what I understand from your question

CodePudding user response:

Okay start with the following:

php artisan make:rule SomeAwesomeRule
Then edit that created Rule
class SomeAwesomeRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
       //some logic that needs to be handled...
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return __('Some.nice.translated.message');
    }
}

And then apply the rule

$rules = [
        'name' => 'required|string',
        'email' => 'required|email',

        'userCode' => new SomeAwesomeRule(#this is a constructor so pass away)

    ];

CodePudding user response:

see

$rules = [
            'name' => 'required|string',
            'email' => 'required|email',
    
            'userCode' => 'unique:user_codes,code'
        ];

//or

$userCode="";

do{
$userCode = UserCode::getCode();
}
while(UserCode::where('code',$userCode)->first());
// after loop you sure the code is unique
  $rules = [
            'name' => 'required|string',
            'email' => 'required|email',
 
        ];
  • Related