如何在ValidForm Builder中检查复选框?

时间:2014-02-20 17:11:04

标签: checkbox validform

我需要在ValidForm Builder中根据默认值预先选择每个复选框,如果在记录最初写入数据库表时选择了它们。

这是我的代码:

  $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');

这是我的试用版&错误尝试让它工作:

(1)我删除了'default' => $default['locationType']并将'checked'添加到每个$objType->addField()。因此,在上面的代码中,$objType->addField()中的所有4个都添加了'checked'元素。结果只有最后一个复选框被选中 - 没有成功

(2)我删除了'default' => $default['locationType']并将'selected'添加到每个$objType->addField()。因此,在上面的代码中,$objType->addField()中的所有4个都添加了'selected'元素。结果只有最后一个复选框被选中 - 没有成功

(3)我删除了'default' => $default['locationType']并将'checked' => 'checked'添加到每个$objType->addField()。因此,在上面的代码中,$objType->addField()中的所有4个都添加了'checked' => 'checked'元素。结果是脚本不生成任何内容 - 没有成功

(4)我也尝试了这一点 - 'default' => array("D", "", "P", "") - 没有成功

(5)我试过这个 - 'default' => array(true, false, true, false) - 所有方框都会被检查 - 没有成功

(6)我试过这个 - 'default' => array("1", "0", "1", "0") - 只检查第一个框 - 没有成功

(7)我试过这个 - 'default' => array(1, 0, 1, 0) - 只检查第一个框 - 没有成功

我认为ValidForm Builder还没有为复选框做好准备。

以下是相关的VFB代码(如果它可以帮助您查看问题所在)。 2014年2月22日的注释通过我的扩展T& E故障排除我不相信这个类文件甚至被加载或用于HTML的制作。继续挖掘......:

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;
    }

}

1 个答案:

答案 0 :(得分:0)

我看到开发者最近为您的问题应用了修复程序的位置。您可以在http://code.google.com/p/validformbuilder/source/list

获取修订后的源文件