سؤال

I am using codeigniter for form validation, in all my inputs I have something like this:

<input type="text" id="someId" name="someId" value="<?php echo set_value('someId'); ?> />

And works perfectly. However when using input type 'date' or jquery Datepicker in case browser doesn't recognize date tag, the date field just resets. Here is my exact code for that field:

<input type="date" id="fecnac" name="fecnac"
value ="<?= isset($_SESSION['fech_nacimiento']) ? $_SESSION['fech_nacimiento'] :        
set_value('fecnac'); ?>" />

I have a session tag because once succesfully submitted value is stored on a session variable. But if it doesn't pass validation I use the 'set_value()' from codeigniter. Problem is it isn't working.

Is there another way to remember the selected date from user if form doesn't pass validation? With or without codeigniter.

هل كانت مفيدة؟

المحلول

I have found a solution for this, here it is in case anybody else has the same issue:

  1. Like Jay Bhatt pointed on a comment, date needs to be on right format. For Jquery Datepicker it should be something like this:

    $( "#datepicker").datepicker({
       dateFormat: 'yy-mm-dd'
    });
    
  2. In my case, since I'm using Codeigniter, for the field to remember value should be:

    value="<?php set_value('datepicker'); ?>
    

    It wasn't working because I forgot to add $this->form_validation->set_rule() to that field in my Controller. Turns out that if you want CI's set_value() function to work, you need to set_rule() first. This was new for me.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top