skip to Main Content

i have a simple registeration form i use this validation for creating u new user

   $validator = Validator::make($request->all(), [
        'first_name' => ['required'],
        'last_name' => ['required'],
        'phone' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', ],
        
    ]);

    if ($validator->fails()) {
        return response()->json(['message' => 'Errors!','errors'=>$validator->errors()], 500);
    }else{
        return response()->json(['message' => 'User registered successfully'], 201);

    }

now i need to make a vidation rule to combine last and first name together and validate max chars

2

Answers


  1. Write Laravel Custom Validation Rules

    <?php
    
    namespace AppRules;
    
    use Closure;
    use IlluminateContractsValidationDataAwareRule;
    use IlluminateContractsValidationValidationRule;
    use IlluminateSupportStr;
    
    class FullNameMaxCharactor implements ValidationRule, DataAwareRule
    {
        protected $requestData=null;
        /**
         * Run the validation rule.
         *
         * @param  Closure(string): IlluminateTranslationPotentiallyTranslatedString  $fail
         */
        public function validate(string $attribute, mixed $value, Closure $fail): void
        {
    
    
            $fullNameCharacters= Str::length(data_get($this->requestData,'first_name').data_get($this->requestData,'last_name'));
    
    
            if ($fullNameCharacters>10) {
    
                $fail('The first and last name should be less than equal to 10.');
            }
    
        }
    
        public function setData(array $data)
        {
           $this->requestData=$data;
    
            return $this;
        }
    }
    

    and in the validation rule

     $validation=IlluminateSupportFacadesValidator::make($request->all(),[
            'first_name' => ['required',new AppRulesFullNameMaxCharactor()],
            'last_name' => ['required'],
            'phone' => ['required', 'string', 'max:255'],
        ]);
    
    Login or Signup to reply.
  2. i would first add the full_name entry to the request object, and then i would validate with the built in validation rules (either max or between).

    $request->request->add(['full_name' => $request->input('first_name').' '.$request->input('last_name')]);
    
    $validator = Validator::make($request->all(), [
        'first_name' => ['required'],
        'last_name' => ['required'],
        'full_name' => ['max:10'],//or between:6,10
        'phone' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', ],
        
    ]);
    
    if ($validator->fails()) {
        return response()->json(['message' => 'Errors!','errors'=>$validator->errors()], 500);
    }else{
        return response()->json(['message' => 'User registered successfully'], 201);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search