Welcome Guest, Not a member yet? Register   Sign In
Form validation's set_value stopped repopulating
#1

[eluser]Jason Tan Boon Teck[/eluser]
I had a form with checks that a birth date field is required and checks the format in a call back function. It was working correctly, repopulating when the form has validation errors.

Now, I have to add in complexity where the user can provide age instead and the application will compute the year and using the current date and month to estimate the birth date. The computation is now done in the controller and the values are pushed to the forms.

Somehow, the set_value is not repopulating age and birth_date. The form fills up correctly the variable values if I don't use set_value or I turn off the validation for these fields.

Is this a bug or am I doing it wrong?

Controller ehr_individual.php snippets:
Code:
$data['init_birth_date']        =   $this->input->post('birth_date');
$data['init_birth_date_estimate'] =   $this->input->post('birth_date_estimate');
$data['init_age']              =   $this->input->post('age');
...
// echoing from controller just for debugging purposes
if(($data['init_birth_date_estimate'] == "TRUE") && ($data['init_age'] > 0)){
    echo "Compute birth date based on age";
    $age                        =   "-".floor($data['init_age']*365)." days";
    $data['init_birth_date']    =   date("Y-m-d", strtotime($age));
    echo "<br />data['init_age']=".$data['init_age']." <br />data[init_birth_date]=".$data['init_birth_date']." <br />age=".$age;
} elseif(($data['init_birth_date_estimate'] == "FALSE") && (!empty($data['init_birth_date']))) {
    echo "Compute age based on birth date";
    $data['init_age']  = round((time()-strtotime($data['init_birth_date']))/(60*60*24*365.2425),1);
    echo "<br />data['init_age']=".$data['init_age']." <br />data[init_birth_date]=".$data['init_birth_date'];
} else {
    echo "E R R O R";
}

View ehr_edit_patient.php snippets:
Code:
echo "\n<tr><td>Birth Date estimated</td><td>";
echo form_error('birth_date_estimate');
echo "\n<select class='normal' name='birth_date_estimate' id='birth_date_estimate'>";          
    echo "\n<option value='TRUE' ".($init_birth_date_estimate=='TRUE'?"selected='selected'":"").">Yes</option>";
    echo "\n<option value='FALSE' ".($init_birth_date_estimate=='FALSE'?"selected='selected'":"").">No</option>";
echo "\n</select>";
echo "\n</td>";
echo "\n</tr>";

echo "\n<tr><td>Age </td><td>";
echo form_error('age');
echo "\n&lt;input type='text' name='age' id='age' value='".set_value('age',$init_age)."' size='4' ".($init_birth_date_estimate=='TRUE'?"":"disabled='disabled'")." /&gt; years old</td></tr>";

echo "\n<tr><td>Birth Date  <font color='red'>*</font></td><td>";
echo form_error('birth_date');
echo "\n&lt;input type='text' name='birth_date' id='birth_date' value='".set_value('birth_date',$init_birth_date)."' size='10' ".($init_birth_date_estimate=='FALSE'?"":"disabled='disabled'")." /&gt; YYYY-MM-DD</td></tr>";

// jQuery for datepicker
    [removed]

    $(function() {
        $( "#birth_date" ).datepicker({
            dateFormat: 'yy-mm-dd',
            changeYear: true,
            yearRange: 'c-100:c'
        });
    });


      function inputAge(obj)
      {
         txt1 = obj.value;
         if (txt1  == "FALSE")
         {
            document.patient_form.birth_date.disabled = false;
            document.patient_form.age.disabled = true;
            //document.form.age.value = "";
            document.patient_form.birth_date.style.backgroundColor="#FFFFFF";
            document.patient_form.age.style.backgroundColor="#CCCCCC";
         }
         else {
            document.patient_form.birth_date.disabled = true;
            document.patient_form.age.disabled = false;
            document.patient_form.birth_date.style.backgroundColor="#CCCCCC";
            document.patient_form.age.style.backgroundColor="#FFFFFF";
         }
         return true;
      }

      [removed]

Form validation rules:
Code:
'edit_patient' => array(
                                    array(
                                            'field' => 'patient_name',
                                            'label' => 'Patient Name',
                                            'rules' => 'trim|required'
                                         ),
                                    array(
                                            'field' => 'gender',
                                            'label' => 'Sex',
                                            'rules' => 'required'
                                         ),
                                    array(
                                            'field' => 'birth_date',
                                            'label' => 'Birth Date',
                                            'rules' => 'trim|required|callback_cb_correct_date'
                                         ),
                                    array(
                                            'field' => 'age',
                                            'label' => 'Age',
                                            'rules' => 'trim|numeric'
                                         ),
                                    array(
                                            'field' => 'email',
                                            'label' => 'Email Address',
                                            'rules' => 'valid_email'
                                         )                                    
                                    ),
#2

[eluser]Jason Tan Boon Teck[/eluser]
I found that somehow, set_value() hangs onto the values retrieved from POST, ignoring any reassignments in the controller. So I got it to work by subsequently pushing the new values back to the POST array and pulling back the values from this updated array.

Is this a bug?

[quote author="Jason Tan" date="1290878744"]
Controller ehr_individual.php snippets:
Code:
$data['init_birth_date']        =   $this->input->post('birth_date');
$data['init_birth_date_estimate'] =   $this->input->post('birth_date_estimate');
$data['init_age']              =   $this->input->post('age');
...
// echoing from controller just for debugging purposes
if(($data['init_birth_date_estimate'] == "TRUE") && ($data['init_age'] > 0)){
    echo "Compute birth date based on age";
    $age                        =   "-".floor($data['init_age']*365.2425)." days";
    $data['init_birth_date']    =   date("Y-m-d", strtotime($age));
    echo "<br />data['init_age']=".$data['init_age']." <br />data[init_birth_date]=".$data['init_birth_date']." <br />age=".$age;
} elseif(($data['init_birth_date_estimate'] == "FALSE") && (!empty($data['init_birth_date']))) {
    echo "Compute age based on birth date";
    $data['init_age']  = round((time()-strtotime($data['init_birth_date']))/(60*60*24*365.2425),1);
    echo "<br />data['init_age']=".$data['init_age']." <br />data[init_birth_date]=".$data['init_birth_date'];
} else {
    echo "E R R O R";
}
[/quote]
Code:
$_POST['birth_date']        =   $data['init_birth_date'];
$data['init_birth_date']    =   $_POST['birth_date'];
$_POST['age']               =   $data['init_age'];
$data['init_age']           =   $_POST['age'];
...
$this->load->vars($data);




Theme © iAndrew 2016 - Forum software by © MyBB