jform checkboxes multiple default value #1265

Closed
mhehm opened this Issue Jun 8, 2012 · 15 comments

Projects

None yet

3 participants

@mhehm

I created a component. in form view i created a checkboxes field with multiple default value. example:

<field name="meal" type="checkboxes" class="inputbox" required="true" multiple="true" default="1,2,3,4,5"  label="Mells" >
  <option value="1">Breakfast</option>
  <option value="2">Lunch</option>
  <option value="3">Dinner</option>
  <option value="4">Morning snack</option>
  <option value="5">Evening snack</option>
</field>

but in form view checkboxes not checked.

@mhehm

i searched and founded the issue.
in libraries/joomla/form/fields/checkboxes.php
line line 67:
$checked = (in_array((string) $option->value, (array) $this->value) ? ' checked="checked"' : '');

issue is in (array) $this->value that do not convert Comma Separated String into Array

solution: change (array) $this->value to explode(",",$this->value)

@elinw

Actually I think you are very close, i spent some time testing yours but I think the issue is that
$this->value should be $option->value

and you should do this:

label="COM_CONTACT_FIELD_INFORMATION_SUBURB_LABEL"
description="COM_CONTACT_FIELD_INFORMATION_SUBURB_DESC"
multiple="true" default= "(array) blue,red"
>
red
blue

to get two checked boxes

  • COM_CONTACT_FIELD_INFORMATION_SUBURB_LABEL string(3) "red"
    string(4) "blue"
    • red
  • @mhehm

    but in this line, it check $option-> value that is in array $this->value or not, then $this->value must be an array.
    $this->value come from default parameter that is comma separated.
    (array) $this->value do not convert $this->value to array that in_array() function can search it. then i use explode(",",$this->value) and it worked correctly.

    @elinw

    Ok I have been testng this for a few hours and got some errors (you have to have your error reporting on development) and you are in my opinion SO SO close to what is wrong but not quite there.

    (array) $this->value

    This should be
    (array) $option->value)

    Then

    label="COM_CONTACT_FIELD_INFORMATION_SUBURB_LABEL"
    description="COM_CONTACT_FIELD_INFORMATION_SUBURB_DESC"
    multiple="true" default= "(array) blue,red"
    >
    red
    blue

    Will give you the correct HTML

    https://gist.github.com/2897660

    (so it doesn't render)

    Now, however the array is not saving which is actually what is expected because you need to customize the saving.

    @elinw

    It must be an array that comes in from the xml and that is why the xml must say "(array) red,blue"

    @mhehm

    i tested your opinion and changed
    (array) $this->value
    to
    (array) $option->value)

    then

    label="COM_CONTACT_FIELD_INFORMATION_SUBURB_LABEL"
    description="COM_CONTACT_FIELD_INFORMATION_SUBURB_DESC"
    multiple="true" default= "(array) blue,red"

    red
    blue

    when we want to add new item it checked all values. check this example when you have 3 value and want to 2 of 3 value be default, it do not work and all 3 value are checked

    @mhehm

    test this:

    label="COM_CONTACT_FIELD_INFORMATION_SUBURB_LABEL"
    description="COM_CONTACT_FIELD_INFORMATION_SUBURB_DESC"
    multiple="true" default= "(array) blue,red"

    red
    blue
    green
    yellow

    @mhehm

    in this code in file:
    in_array((string) $option->value, (array) $this->value

    code check that $option->value is in (array) $this->value or not

    you say that it must be
    in_array((string) $option->value, (array) $option->value
    but in your code, you are check that $option->value is in $option->value that all of this is same.

    @elinw

    Editing this because you explained it better while i was posting this

    Yes, it's checking it is in an array and it needs to have an array to read but $this->value is not an array while $option->value is an array if you have entered it as such. It then is casting a string to be an array which is why if you enter red,blue it thinks it is an array with only one element.
    If it is set to an array it is saving as an array the casting does nothing, but you must tell it is an array. If you just set it to 'red,blue' it thinks that you want to save 'red,blue' as the string and that it should be prechecked.

    like

    <option value="red,blue">red,blue</option>

    Also it actually works to say checked="red" or checked="(array) red,blue" and that is more correct since it is not really setting a default it is setting checked.

    @elinw

    Okay on looking at it even deeper I did this which has aspects of what each of us said plus a bug fix where checkboxes was not using the saved data.
    So with this you would say checked="red,blue" and the only real issue is that you cannot have a comma in any of your values since it messes up the explode.
    elinw@a6b59ba

    I'm finishing up tests and then I'll send a pull request.

    @mhehm

    I've applied your changes in the code and put in checkboxes field, checked="red,blue". when i add a new item, it works and I also wrote store code to store value in the database and the values ​​stored in the database were to be separated by commas, such as red,blue
    options is : red, blue and green

    But when you edit that item again, options ​​of that checkboxes that are stored in database are not selected in the. While the code that i was written , work correctly

    @mhehm

    my example with your code changes:

    field xml:

    <field name="meal" type="checkboxes" class="inputbox" required="true" multiple="true" checked="1,2,5"
            label="COM_SIBDIET_FORM_LBL_FOOD_MEAL"
            description="COM_SIBDIET_FORM_DESC_FOOD_MEAL" >
      <option value="1">Breakfast</option>
      <option value="2">Lunch</option>
      <option value="3">Dinner</option>
      <option value="4">Morning snack</option>
      <option value="5">Evening snack</option>
    </field>
    

    to store values :

    public function bind($array, $ignore = '')
    {
    ....
    ....
    if (key_exists( 'meal', $array ) && is_array( $array['meal'] )) {
    $array['meal'] = implode( ',', $array['meal'] );
    }
    return parent::bind($array, $ignore);
    }

    i add new item. default value are selected. i select options 2,3,4 and save item.
    values stored in database field (text) : 2,3,4

    when i want edit item again, no options arenot selected

    @mhehm

    my example with my changes that work correct:

    field xml:

    <field name="meal" type="checkboxes" class="inputbox" required="true" multiple="true" default="1,2,5"
            label="COM_SIBDIET_FORM_LBL_FOOD_MEAL"
            description="COM_SIBDIET_FORM_DESC_FOOD_MEAL" >
      <option value="1">Breakfast</option>
      <option value="2">Lunch</option>
      <option value="3">Dinner</option>
      <option value="4">Morning snack</option>
      <option value="5">Evening snack</option>
    </field>
    

    to store values :

    public function bind($array, $ignore = '')
    {
    ....
    ....
    if (key_exists( 'meal', $array ) && is_array( $array['meal'] )) {
    $array['meal'] = implode( ',', $array['meal'] );
    }
    return parent::bind($array, $ignore);
    }

    i add new item. default value are selected. i select options 2,3,4 and save item.
    values stored in database field (text) : 2,3,4

    when i edit that item again, opteions 2, 3, 4 are selected. when i add new item, options 1,2,5 are selected.

    @elinw

    see #1268

    @elinw

    So this was really resolved with #1268 but also then Cristophe has proposed completely redoing again in #1280, so this one can probably be closed.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment