Specific validations for dates selects

Posted 1 week ago by BrownieCoffee

Hi everyone.

Can you help me to fix my validations problems please ? I'm stuck.

I use selects for start day and end day ( day, month and year).

I would create some conditions around those.

If the start day or the end day are less than the current day ( today), it gerenates error.

If the end day is less than start day, it generates error too.

But with my configuration, I have some difficulties.

Can you help please?

Thank you a lot in advance and good bye !

my actual code on my ServiceRequestController.php

   public function store(Request $request){


        $requestDatas = $request->validate([

            'needed-service' => 'required',

            'address' => '',
            'zip-code' => 'required|numeric|digits:5',
            'city' => 'required|string|max:300',

            'startDay' => 'required',
            'startMonth' => 'required',
            'startYear' => 'required',

            'endDay' => 'required',
            'endMonth' => 'required',
            'endYear' => 'required',

            'provided-service' => 'required',

            'complementaries-informations' => 'nullable'
        ]);



        ServiceRequest::create($requestDatas);

        

        $currentDay = Carbon::now()->day;
        $currentMonth = Carbon::now()->locale('fr')->monthName;
        $currentYear = Carbon::now()->year;

        $currentDate = Carbon::today();
        // dump($currentDay);
        // dump($currentMonth);
        // dump($currentYear);
        dump($currentDate);


        $theStartDate = Carbon::create(Input::get('startYear'), Input::get('startMonth'), Input::get('startDay'));
        $theEndDate = Carbon::create(Input::get('endYear'), Input::get('endMonth'), Input::get('endDay'));

        // dump($theStartDate);
        // dump($theEndDate);

    
//test pour savoir si la date de debut ou de fin sont inférieurs à la date courrante
        if($theStartDate < $currentDate  || $theEndDate < $currentDate ){
            dump('not ok');
        }else{
            dump('ok');
        }

//test pour savoir si la date de fin de mission est plus petit que la date de debut de mission
        if($theEndDate < $theStartDate){
            dump('impossible to have the enddate smallest than the startdate');
        } else{
            dump('perfect ! ');
        }
        

// test pour savoir la date de debut et de fin sont la meme
        if(!($theEndDate == $theStartDate)){
            dump('is not same date');
        }
        else{
            dump('');
        }

    dump($request);
        
    }

Please sign in or create an account to participate in this conversation.