phpcheckboxvalidform

How do you get checkboxes to be checked in ValidForm Builder?


I need to get each of the checkboxes to be pre-selected in ValidForm Builder based on their default values if they have were selected when the record initially got written to the DB Table.

Here is my code:

  $objType = $objGroup->addField('locationType', 'Location Type', VFORM_CHECK_LIST,
    array('required' => true),
    array('required' => 'Location Type is required'),
    array(
      'fieldclass' => 'vf__inlineButtons',
      'tip' => (($_SESSION['auth']['tips'] && $_POST['action'] != 'delete') ? VFB_TIP_LOCATIONS_LOCATIONTYPE : NULL),
      (($_POST['action'] == 'delete') ? 'fieldDisabled' : 'fieldEnabled') => (($_POST['action'] == 'delete') ? 'disabled' : 'enabled'),
      'default' => $default['locationType']
    )
  );
  $objType->addField('Destination', 'D');
  $objType->addField('Sales', 'S');
  $objType->addField('Pickup/Dropoff', 'P');
  $objType->addField('Both, Sales & Pickup/Dropoff', 'B');

Here's my trial & error attempts to get it to work:

(1) I removed the 'default' => $default['locationType'] and added 'checked' to each $objType->addField(). So In the code above, that is all 4 of the $objType->addField()'s have the 'checked' element added to them. Result is only the last checkbox gets checked -- no success.

(2) I removed the 'default' => $default['locationType'] and added 'selected' to each $objType->addField(). So In the code above, that is all 4 of the $objType->addField()'s have the 'selected' element added to them. Result is only the last checkbox gets checked -- no success.

(3) I removed the 'default' => $default['locationType'] and added 'checked' => 'checked' to each $objType->addField(). So In the code above, that is all 4 of the $objType->addField()'s have the 'checked' => 'checked' element added to them. Result is the script generates nothing -- no success.

(4) I tried this too -- 'default' => array("D", "", "P", "") -- no success.

(5) I tried this -- 'default' => array(true, false, true, false) -- all boxes get checked -- no success.

(6) I tried this -- 'default' => array("1", "0", "1", "0") -- only the first box gets checked -- no success.

(7) I tried this -- 'default' => array(1, 0, 1, 0) -- only the first box gets checked -- no success.

I am thinking that ValidForm Builder is not ready for checkboxes.

Here is the relevant VFB code (if it helps you see where the issue is). NOTE on 2/22/2014 through my extended T&E troubleshooting I do not believe this class file is even loaded or used in the production of the HTML. Continuing to dig...:

class VF_Checkbox extends VF_Element {

    public function toHtml($submitted = FALSE, $blnSimpleLayout = FALSE, $blnLabel = true, $blnDisplayErrors = true) {
        $blnError = ($submitted && !$this->__validator->validate() && $blnDisplayErrors) ? TRUE : FALSE;

        if (!$blnSimpleLayout) {
            //*** We asume that all dynamic fields greater than 0 are never required.
            if ($this->__validator->getRequired()) {
                $this->setMeta("class", "vf__required");
            } else {
                $this->setMeta("class", "vf__optional");
            }

            if ($blnError) $this->setMeta("class", "vf__error");
            if (!$blnLabel) $this->setMeta("class", "vf__nolabel");

            // Call this right before __getMetaString();
            $this->setConditionalMeta();

            $strOutput = "<div{$this->__getMetaString()}>\n";

            if ($blnError) $strOutput .= "<p class=\"vf__error\">{$this->__validator->getError()}</p>";

            if ($this->__getValue($submitted)) {
                //*** Add the "checked" attribute to the input field.
                $this->setFieldMeta("checked", "checked");
            } else {
                //*** Remove the "checked" attribute from the input field. Just to be sure it wasn't set before.
                $this->setFieldMeta("checked", null, TRUE);
            }

            if ($blnLabel) {
                $strLabel = (!empty($this->__requiredstyle) && $this->__validator->getRequired()) ? sprintf($this->__requiredstyle, $this->__label) : $this->__label;
                if (!empty($this->__label)) $strOutput .= "<label for=\"{$this->__id}\"{$this->__getLabelMetaString()}>{$strLabel}</label>\n";
            }
        } else {
            if ($blnError) $this->setMeta("class", "vf__error");
            $this->setMeta("class", "vf__multifielditem");

            // Call this right before __getMetaString();
            $this->setConditionalMeta();

            $strOutput = "<div{$this->__getMetaString()}>\n";

            if ($this->__getValue($submitted)) {
                //*** Add the "checked" attribute to the input field.
                $this->setFieldMeta("checked", "checked");
            } else {
                //*** Remove the "checked" attribute from the input field. Just to be sure it wasn't set before.
                $this->setFieldMeta("checked", null, TRUE);
            }
        }

        $strOutput .= "<input type=\"checkbox\" name=\"{$this->__name}\" id=\"{$this->__id}\"{$this->__getFieldMetaString()}/>\n";

        if (!empty($this->__tip)) $strOutput .= "<small class=\"vf__tip\">{$this->__tip}</small>\n";

        $strOutput .= "</div>\n";

        return $strOutput;
    }

    public function toJS() {
        $strOutput = "";

        $strCheck = $this->__validator->getCheck();
        $strCheck = (empty($strCheck)) ? "''" : str_replace("'", "\\'", $strCheck);
        $strRequired = ($this->__validator->getRequired()) ? "true" : "false";;
        $intMaxLength = ($this->__validator->getMaxLength() > 0) ? $this->__validator->getMaxLength() : "null";
        $intMinLength = ($this->__validator->getMinLength() > 0) ? $this->__validator->getMinLength() : "null";

        $strOutput .= "objForm.addElement('{$this->__id}', '{$this->__name}', {$strCheck}, {$strRequired}, {$intMaxLength}, {$intMinLength}, '" . addslashes($this->__validator->getFieldHint()) . "', '" . addslashes($this->__validator->getTypeError()) . "', '" . addslashes($this->__validator->getRequiredError()) . "', '" . addslashes($this->__validator->getHintError()) . "', '" . addslashes($this->__validator->getMinLengthError()) . "', '" . addslashes($this->__validator->getMaxLengthError()) . "');\n";

        //*** Condition logic.
        $strOutput .= $this->conditionsToJs();

        return $strOutput;
    }

    public function getValue($intDynamicPosition = 0) {
        $varValue = parent::getValue($intDynamicPosition);
        return (strlen($varValue) > 0 && $varValue !== 0) ? TRUE : FALSE;
    }

    public function getDefault($intDynamicPosition = 0) {
        return (strlen($this->__default) > 0 && $this->getValue($intDynamicPosition)) ? "on" : null;
    }

}

Solution

  • I see where the developer recently applied a fix for your issue. You can get the revised source file at http://code.google.com/p/validformbuilder/source/list