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

问题描述 投票:0回答:1

如果在记录最初写入数据库表时选择了每个复选框,我需要根据其默认值在 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()
。所以在上面的代码中,所有 4 个
$objType->addField()
都添加了
'checked'
元素。结果只是最后一个复选框被选中——没有成功

(2) 我删除了

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

(3) 我删除了

'default' => $default['locationType']
并为每个
'checked' => 'checked'
添加了
$objType->addField()
。所以在上面的代码中,所有 4 个
$objType->addField()
都添加了
'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;
    }

}
php checkbox validform
1个回答
0
投票

我看到开发人员最近在哪里应用了针对您的问题的修复。您可以在 http://code.google.com/p/validformbuilder/source/list

获取修改后的源文件
© www.soinside.com 2019 - 2024. All rights reserved.