Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

Checkbox fields are unchecked

edited September 2015 in Bug Reports
Because the functionality of the checkbox in the form doesn't work as I expected I tried to simplify the code.
So, I have a simple checkbox field:

$fields['test_12345'] = array( 'type' => 'checkbox', 'label' => 'TEST: ', 'checked' => true, // default is checked );

As you can see I've set to be always checked: 'checked' => true
I used the validator to check another required conditions from the same form. The checkbox keeps the value as "checked" after page submit, but becomes "unchecked" when a rule is not fulfilled.

So in this way always the checkbox is toggled when I submit the page (checked/unchecked), even if the checked is set always to be true. I checked the post after value $this->validator->validate() and the correct value is posted. When the page is refreshed to be shown the error message the value of checkbox changes.

I suppose that is a bug and I wonder if you can confirm.

Thank you


  • edited 5:44AM
    Are you able to post the entire form?
  • edited September 2015
    Sorry but I still have somewhere 600 line of code.

    To simulate I propose do it in this way: create a controller with a form with any input X and add a validation for that input. More than that add also a checkbox with attribute always checked = true
    Submit the form and try to invalidate the X input (for instance make required but send it empty) and look at the value that takes the checkbox

    If it's really necessary I will create one complete controller with that 2 elements.
  • edited 5:44AM
    How about the minimal amount of the form to replicate the problem?
  • edited 5:44AM
    The minimal amount of the form is the following:
    <?php /** * @package No_Module\Controllers */ class contact_us extends Fuel_base_controller { public function __construct(){ parent::__construct(); } public function index(){ $vars = array(); // load Form_builder; $this->load->library('form_builder'); // load validator; $this->load->library('validator'); if($this->input->post()) { $this->validator->add_rule('first_name', 'required', 'First name is required', $this->input->post('first_name')); if ($this->validator->validate()) { $message = $this->input->post('message'); echo "OK"; } } // set the fields for the contact form $fields = array(); $fields['first_name'] = array('label' => lang('p_first_name')); // 'required' => true, $fields['checkbox'] = array('type' => 'checkbox', 'label' => 'Checkbox', 'checked' => true); // always true $form_init = array('submit_value' => 'Confirm', 'css_class' => 'c_form'); $this->form_builder->initialize($form_init); $this->form_builder->set_fields($fields); $this->form_builder->set_field_values($_POST); $this->form_builder->display_errors = TRUE; $this->form_builder->required_text = '<span class="required">*</span> Required'; $vars['form'] = $this->form_builder->render_divs(); // set the data from CMS $vars['body'] = $this->load->view('page/contact_form', $vars, true); // show page $this->fuel->pages->render('contact_us', $vars, array('render_mode' => 'cms')); } } ?>

    It seems that even remains in the same page the checkbox is toggled at each form submit.
  • edited 5:44AM
    Try adding "value" => 1 to the $fields['checkbox']
  • edited 5:44AM
    Using your proposition I get the same result. The checkbox input is toggled only when is set to TRUE or 1.

    I tried to set FALSE or 0 and in that case keeps the same value => unchecked

    This issues persists in application and also in modules
  • edited 5:44AM
    Try the following:
    $checkbox = (!empty($_POST['checkbox'])) ? $_POST['checkbox'] : true; $fields['checkbox'] = array('type' => 'checkbox', 'label' => 'Checkbox', 'value' => 1, 'checked' => $checkbox);
  • edited 5:44AM
    Your code will always keep the checkbox in the "checked" status.

    I used $fields['checkbox'] = array('type' => 'checkbox', 'label' => 'Checkbox', 'value' => 1, 'checked' => $this->input->post('checkbox')); and everything seems to work well. As you can see I added the missing parameter 'value' => 1, and now it works.
    Thank you for your quick answer.
  • edited 5:44AM
    first define your checkbox fields in model form_fields function
    public function form_fields($values = array(), $related = array())
    $fields = parent::form_fields($values, $related);
    $checkbox = (!empty($values['is_editing_allowed'])) ? $values['is_editing_allowed'] : true;

    $fields['is_editing_allowed'] = array('type' => 'checkbox','id'=>'is_editing_allowed' , 'name'=>'is_editing_allowed','label' => lang('is_editing_allowed'),'value' => 1, 'checked' => $checkbox);

    now change the value if checked is_editing_allowed field than value 1 or value 0 from on_before_save function.
    public function on_before_save($values)
    $values['is_editing_allowed'] = '0';

    That's it
Sign In or Register to comment.