Re-populate checkbox if it fails in validation in

2019-02-21 05:14发布

问题:

I works on the an data-edit form in codeigintier. And the problem is about re-populate checkbox

It works if it is an add form (that means I need not concern about the value in database):

 <?= set_checkbox('is_default', '1'); ?> for checkbox

The problem is, in the edit form:

I can't repopulate the checkbox

<?php if ($customer_group[0]['is_default'] == "1") echo "checked"; set_checkbox('is_default', '1'); ?>

The checkbox will check even I have not check it in the edit => fail to validate in the form, thanks for helping

I have already set the validation rule in controller, the code in the add form is working , but how to handle the case for edit form?

回答1:

set_checkbox takes a third argument to set the default state, so basically you have to do something like this

echo set_checkbox('is_default', 1, $customer_group[0]['is_default'] == "1"); 


回答2:

In order to re-populate checkbox following code might be helpful:

set_checkbox('fieldName', 'fieldValue');

Where 'value' is the second parameter of the form_checkbox call. Like this:

form_checkbox('fieldName[]', 'value', set_checkbox('fieldName', 'value'));

Now if you are on edit form then below code might help you

$getVal=$valFromDb; //$valFromDb is actually value of the filed from db as you are on edit page
if($getVal!=0){
{
   echo form_checkbox('fieldName[]', 'value', true);
}
else
{
   echo form_checkbox('fieldName[]', 'value', false);
}


回答3:

Can give one suggestion?? 1. Hide all the checked value of checkbox in input box when you are directed towards edit page.

  1. If checked box is checked in edit page, edit the value of hidden input field of textbox value.

  2. Submit it, when validation failed, checked or repopulate the checkbox value according to hidden field value. send checkbox value of checked box field through array from controller to edit page view like this. e.g $data['repopulate_checks'] = $this->input->post('array name of checkboxs'); In view : getit like this $catch_checkbox = $repopulate_checks; You can directly get through $repopulate_checks also. Hope this help you.



回答4:

You can use form_checkbox() function: Guide

$isChecked = False; // or True for default value

If have stored data then:

$isChecked = $customer_group[0]['is_default']; echo form_checkbox('input_name', 'value', $isChecked);

or the hard way: set_checkbox():

The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE)

<input type="checkbox" name="is_default" value="1" <?php echo ($customer_group[0]['is_default']) ? set_checkbox('is_default', '1') : '' ; ?>/>



回答5:

set_checkbox takes a third argument to set the default state, so basically you have to do something like this

$checked = FALSE; if($customer_group[0]['is_default']){ $checked = TRUE; }

echo set_checkbox('is_default', 1, $checked);