skip to Main Content

Here is my validation code in the controller

 $eldate = Carbon::now()->addDay(4)->format('Y-m-d');

 $validatedData = $this->validate($request, [
        'ldate' => 'required|date|unique:leaves,ldate,NULL,employee,employee,'.auth()->id(),
        'ldate2'      => 'nullable|date|after_or_equal:ldate',
        'type' => 'required'
    ]);

In this validation code I have to add one more condition for date1

If type =2 then ldate should be greater than $eldate

3

Answers


  1. use custom validation rules to solve the issue

    $eldate = Carbon::now()->addDay(4);
    
         $validatedData = $this->validate($request, [
                'ldate' => ['required','date','unique:leaves,ldate,NULL,employee,employee,'.auth()->id(), function (string $attribute, mixed $value, Closure $fail) use ($request, $eldate) {
                    if ($request->type==2 && $eldate->gt($value)) {
                        $fail("The {$attribute} is invalid.");
                    }
                }],
                'ldate2'      => 'nullable|date|after_or_equal:ldate',
                'type' => 'required'
            ]);
    

    For custom validation rules

    Using Closures

    Using Rule Objects

    Login or Signup to reply.
  2. If you’re using Laravel Form Request, then simply add

    public function rules()
    {
        $eldate = Carbon::now()->addDay(4)->format('Y-m-d');
        $rules = [
            'ldate' => ['required', 'date', Rule::unique('leaves', 'ldate')->where(function ($query) {
                return $query->where('employee', auth()->id());
            })],
            'ldate2' => 'nullable|date|after_or_equal:ldate',
            'type' => 'required',
        ];
    
        if ($this->input('type') == 2) {
            $rules['ldate'][] = 'after:' . $eldate;
        }
    
        return $rules;
    }
    
    Login or Signup to reply.
  3. I can show how to use Custom Class
    documentation: https://laravel.com/docs/10.x/validation#custom-validation-rules

    controller function code

    public function test(Request $request)
        {
            $eldate = Carbon::now()->addDay(4);
            
            $rules = [
                'ldate'  => ['required', 'date', new ValidateDateTwo($eldate, $request->get('type'))],
                'ldate2' => ['nullable', 'date', 'after_or_equal:ldate'],
                'type'   => 'required'
            ];
    
            $validator = Validator::make($request->all(), $rules);
    
            if ($validator->fails()) {
                $messages = $validator->errors();
                $errors = [];
                foreach ($rules as $input => $rule) {
                    foreach ($messages->get($input) as $key => $message) {
                        $errors[$input] = $message;
                    }
                }
    
                return Response::json(['status' => false, 'errors' => $errors]);
    
            }
    
    
            return response()->json([
                'status' => 'success',
                'data' => [
                    'ldate' => $request->get('ldate'),
                    'ldate2' => $request->get('ldate2'),
                    'type' => $request->get('type')
                ]
            ]);
        }
    

    And the Validation Class

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateContractsValidationRule;
    
    class ValidateDateTwo implements Rule
    {
    
    
        /**
         * @var mixed
         */
        private $eldate;
        /**
         * @var mixed
         */
        private $type;
    
        public function __construct($eldate, $type)
        {
            $this->eldate = $eldate;
            $this->type = $type;
        }
        public function passes($attribute, $value)
        {
            return $this->type != 2 || !$this->eldate->gt($value);
        }
    
    
        public function message()
        {
            return 'Your error message.';
        }
    }
    

    i have tested it for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search