1

So I have made a form where staff members can post activities like soccer or tennis with a form. So I have 2 dates which they can select. The first date is the start date of the activity and the second date is the end date of the activity. I don't want it to be able to select an end date earlier than a selected startdate. So for example I want to fill in an activity like bowling and it in the form I put at the start date 03-04-2018 and the end date 02-02-2018, I don't want that to be possible.

This is how my form looks:

  <?php  echo form_open('index.php/Jongeren_activiteiten/Add_activiteit'); ?>
            <br>
            <center>Naam van activiteit:</center>
                <div class="form-group">
                    <input class="form-control" name="activiteit" id="activiteit" type="text">
                </div>
                <center>Begindatum:</center>
                 <div class="form-group">
                    <input class="form-control" name="begindatum" id="begindatum" placeholder="Startdatum cursus" type="date">
                </div>
                <center>Einddatum:</center>
                 <div class="form-group">
                    <input class="form-control" name="einddatum" id="einddatum" placeholder="einddatum cursus" type="date">
                </div>

                <div>
                    <button class="btn btn-primary" name="Add_activiteit" >Toevoegen</button>
                </div>
            </form>       

And this is the controller function of the form:

public function Add_activiteit()
    {
    if (isset($_POST['Add_activiteit'])) {
            $this->form_validation->set_rules('activiteit', 'Activiteit', 'required');
            $this->form_validation->set_rules('begindatum', 'Begindatum', 'required');
            $this->form_validation->set_rules('einddatum', 'Einddatum', 'required');
            //If form validation true
            if ($this->form_validation->run() == TRUE) {
               // echo 'form validated';

                $data = array (
                    'activiteit'=>$_POST['activiteit'],
                    'begindatum'=>$_POST['begindatum'],
                    'einddatum'=>$_POST['einddatum'],
                    );
                $this->db->insert('activiteit',$data);

                $this->session->set_flashdata("success", "u heeft een nieuwe activiteit toegevoegd");
                redirect("index.php/Jongeren_activiteiten", "refresh");
            }
        }


    }

begindatum means start date and einddatum means end date. So how do I fix it so you can only select an end date later than a startdate? I also don't want people to being able select a day in the past.

Any kind of help is appreciated

Jeremy
  • 35
  • 7

1 Answers1

0

You should use a callback function..

The way you would do this is by creating a function like this:

function checkDate($start, $end){
    if($start > $end){
        $this->form_validation->set_message('checkDate', 'The end date must be later than the start date.');
        return FALSE;
    }else{
        return TRUE;
    }
}

Then you would add it to your form validation rules(I would do it for the end date), but prefix it with 'callback_'

So it would look like this:

$this->form_validation->set_rules('einddatum', 'Einddatum', 'required|callback_checkDate');

A further explanation provided by CodeIgniter can be found here: https://www.codeigniter.com/user_guide/libraries/form_validation.html

adamoffat
  • 639
  • 6
  • 22