【问题标题】:Yii CActiveForm radioButtonList. Add your own answer on "Other. Please specify" selectYii CActiveForm 单选按钮列表。在“其他。请说明”上添加您自己的答案选择
【发布时间】:2014-04-21 00:41:44
【问题描述】:

例如,我们有:

<div class="row">
    <?php echo $form->labelEx($company,'company_type_of_ownership'); ?>
    <?php echo $form->radioButtonList($company,'company_type_of_ownership',array("Private company"=>"Private company","A"=>"A","B"=>"B","Other"=>"Other(Please specify)"),array(//SOME JS HERE MAY BE?); ?>
    <?php echo $form->error($company,'company_type_of_ownership'); ?>
</div>

如果用户选择 A,则 A 将保存在“company_type_of_ownership”单元格中。

但是如何实现下一个功能呢?

用户选择“其他(请指定)”,并且有一个特殊的文本字段,他可以在其中输入“我自己的所有权类型”,例如,此值将保存在表中。

或者(我不知道,如果它是更好的做法): 对于这种情况,表中还有另一个特殊单元格吗? 例如,如果用户选择“A”,则该值将保存在“TYPE OF OWNERSHIP”单元格中,而“OTHER TYPE”单元格则为空。 但是如果他选择了other,那么“TYPE OF OWNERSHIP”就有了“Other”的值,他输入的内容被保存在“OTHER TYPE”中

有什么建议吗?

更新: 谢谢您的回复。你能告诉我我做错了什么吗? 我现在尝试以这种方式实现它(我的js很差,所以在网上找到了这个):

<div class="row">
    <?php echo $form->labelEx($company,'company_type_of_ownership'); ?>
    <?php echo $form->radioButtonList($company,'company_type_of_ownership',array("Private company"=>"Private company","Other"=>"Other (please, specify)"),array('onchange'=>'return muFun(this.value)')); ?>
    <?php echo $form->error($company,'company_type_of_ownership'); ?>
</div>

<div id="check_1" style="display:none">                 
    <div class="row">  
        <?php echo $form->labelEx($company,'company_type_of_ownership_other'); ?>             
        <?php echo $form->textField($company,'company_type_of_ownership_other',array('size'=>50,'maxlength'=>25)); ?>
        <?php echo $form->error($company,'company_type_of_ownership_other'); ?>                
    </div>    
</div>

<script>
function muFun(obj){    

            if(obj=="Other"){
            document.getElementById('check_1').style.display="block";
                            return false;
            }else{
            document.getElementById('check_1').style.display="none"; 
            return false;
            }
     }
</script>

当我选择 OTHER 时,这个 txtfield 变得可见。我在表格中为它创建了一个特殊的单元格:'company_type_of_ownership_other' 但是如果我在其中输入一些东西,它就不会被保存到我的数据库中。可能是什么问题,如果它与您建议的相同? 谢谢

更新 2:一个小错误 由于一切正常,出现了一个问题: 您指定“其他”按钮并出现一个隐藏的文本字段。您决定忽略它并继续填写申请表。在您按下 SUBMIT (SEND) 之后,我们的 beforeValidate 规则中指定的错误就会出现。完美,但是:隐藏的文本字段再次隐藏。为了让它再次出现,用户必须单击另一个单选按钮(例如,这里的私人公司),然后单击返回到其他 - 只有这样隐藏的文本字段才会出现。亲爱的亚历克斯,我需要你的帮助。并不是每个人都能猜到进行这些操作。

【问题讨论】:

    标签: php yii radiobuttonlist


    【解决方案1】:

    添加隐藏文本字段,当用户选择其他使用 jvascript 显示隐藏字段时,然后在控制器中检查该字段并为其创建新记录。

    更新:this.value != 'Other', 'Other' - 是文本,值会有所不同。试试这个

    this.options[this.selectedIndex].innerHTML
    

    或者更好地检查那里的值

    console.log(this.value);
    

    最好将'onchange'=&gt;'return muFun(this.value)' 更改为'onchange'=&gt;'return muFun(this)'。那么函数将是

    function muFun(sb){    
       if(sb.options[sb.selectedIndex].innerHTML=="Other")
           document.getElementById('check_1').style.display="block";
       else
           document.getElementById('check_1').style.display="none"; 
    
       return false;
     }
    

    在模型的 beforeValidate 方法中,您应该手动检查这两个字段之一是否已填充。从模型的 rules() 方法中删除它们的“必需”规则。

    【讨论】:

    • 更新了我的帖子,请看一下。泰
    • 我完全糊涂了。我应该在哪里插入这个? this.options[this.selectedIndex].innerHTML
    • 我已经按照您的建议进行了所有更改,但是虽然我想选择 PRIVATE COMPANY - 它无法记录到数据库中。它也无法通过验证 - 如果我选择它 - 它告诉我根本没有价值,我必须提供它:( p.S. 谢谢你的耐心。
    • 您是否在模型中添加了规则 ('company_type_of_ownership_other', 'safe')?
    • 所以,我不能将值写入同一个字段。因为如果我选择 Private Company - hidden txtfield 没有任何价值并且无法验证。我已在隐藏文本字段中将“company_type_of_ownership”改回“company_type_of_ownership_other”。但是如果我现在选择其他,文本字段仍然是隐藏的。规则是“安全”,“开启”=>“搜索”。没事吧?
    【解决方案2】:

    如果您想使用规则验证 company_type_of_ownership_other,请创建自定义验证规则。 (请注意,我使用company_ownership_type_other_description 来提高可读性而不是company_type_of_ownership_other...你可以选择任何你喜欢的)

    您的模型应具有以下属性(或 db 表中的字段)

    company_ownership_type //field that gets the value from radio buttons
    company_ownership_type_other_description //field for text
    

    将以下内容添加到规则中:

    array('company_ownership_type_other_description',
            'validateOtherCompanyTypeOwnershipText'),
    

    将以下方法添加到您的模型中:

    public function validateOtherCompanyTypeOwnershipText($attribute) {
      if ($this->company_ownership_type == 'other' && empty($this->attribute))
        $this->addError($attribute,'You need to enter a value for ' .
                $this->getAttributeLabel ($attribute) .
                ' if you select "Other" for Company Type');
    }
    

    这基本上是:针对文本字段运行验证规则(在规则中设置)。 $attribute 是 'company_ownership_type_other_description'。

    'getAttributeLabel' 给出文本字段的标签(如果你在模型中设置了它)。

    【讨论】:

      【解决方案3】:

      顺便说一下,只是想添加一些关于 beforeValidate 的有用信息: 对于这个例子,我们需要添加:

      public function beforeValidate() {
         if ($this->company_type_of_ownership=='Other' && !$this->company_type_of_ownership_other) {
              $this->addError('typeofwnership', 'Please, specify the type of ownership');
          }
         if ($this->company_legal_form=='Other' && !$this->company_legal_form_other) {
              $this->addError('legalform', 'Please, specify the company legal form');
          }
          return parent::beforeValidate();
      }
      

      那么,它有什么作用? 如果选择了“其他,请说明”单选。那么这条规则:

      $this->company_type_of_ownership=='Other'
      

      有效,并且需要这条规则:

      && !$this->company_type_of_ownership_other
      

      也必须指定(此处为非空)。然后我们可以添加一些错误文本,例如“请指定所有权类型”,它会起作用!喂!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-27
        • 1970-01-01
        • 2019-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多