Checkbox automatically checked in array

Asked

Viewed 240 times

2

I’m cracking my head to set a checkbox as marked.

I know the markup syntax (<input checked>) but this particular checkbox is being mounted from an array and I couldn’t, and I don’t even know if it’s possible, define how checked.

That is the code:

array(
            'form_id'      => 'global_settings',
            'id'           => 'enable_public_questions',
            'element'      => ($settings['enable_public_questions']) ? 'checkbox' : false, 
            'label'        => $this->_('Accept Public Questions'),
            'description'  => $this->_('Check the above checkbox to allow site users to post public questions .'),
            'multiOptions' => array(
                1 => null,
            ),
        ),
        array(

I believe that part is responsible for the exhibition:

<global_settings>
    <label>Global Settings</label>
    <module>members</module>
    <controller>tools</controller>
    <action>global-settings</action>
    <resource>Tools</resource>
    <privilege>GlobalSettings</privilege>
</global_settings> 

I found another part:

class Checkbox extends Element
{

    /**
     *
     * type of element - override the variable from the parent class
     * 
     * @var string
     */
    protected $_element = 'checkbox';

public function render()
    {
        $output = null;
        $value = $this->getValue();

        $multiple = '';
        if (count((array) $this->_multiOptions) > 1 || $this->getMultiple() === true) {
            $multiple = $this->_brackets;
        }


        $output .= '<input type="hidden" name="' . $this->_name . $multiple . '" value=""'
                . $this->_endTag;

        foreach ((array) $this->_multiOptions as $key => $option) {
            $checked = (in_array($key, (array) $value)) ? ' checked="checked" ' : '';

            if (is_array($option)) {
                $title = $option[0];
                $description = $option[1];
            }
            else {
                $title = $option;
                $description = null;
            }
            $output .= '<label class="checkbox">'
                    . '<input type="' . $this->_element . '" name="' . $this->_name . $multiple . '" value="' . $key . '" '
                    . $this->renderAttributes()
                    . $checked
                    . $this->_endTag
                    . ' ' . $title
                    . ((!empty($description)) ? '<span class="help-block">' . $description . '</span>' : '')
                    . '</label>'
                    . "\n";
        }

        return $output;
    }

}
  • 2

    Marko, how are you converting this to HTML?

  • That is, the only part responsible for that field that I found was on that page on that array. I tried searching the other pages for an array call: 'id' => 'enable_public_questions', but could not find on any page

  • A little further down the page I found the call that mounts the $array Return; }

  • There is a echo somewhere that sends it to the client side?

  • foreach ((array) $this->_multiOptions as $key => $option) {&#xA; $checked = (in_array($key, (array) $value)) ? ' checked="checked" ' : ''; the party responsible for marking as checked That’s the one, but you still can’t do much, because $value receives values from another function getValue, and in that part of your code you can’t see that function. But it’s basically this, it marks as checked the values derived from getValue that are on the array multiOptions.

2 answers

2

Try adding:

'values' => true,

or

'values' => array('value',true),

In some settings php with xml is like this.

  • i added, but not 'values' => true, and 'values' => array('value',true), in php but still not checked.

-2


I already solved, I had to adapt a function that when the field was not selected gave a false Return, changed to true Return, and even without checking the field the problem was solved. It was what I needed, but the question of defining how checked in the array gets a mystery.

I thank all those who were willing to help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.