Yii CActiveForm单选按钮列表.在“;另外请指定“;选择


Yii CActiveForm radioButtonList. Add your own answer on "Other. Please specify" select

例如,我们有:

<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"单元格中。

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

用户选择"其他(请指定)",有一个特殊的文本字段,他可以在其中键入"我自己的所有权类型",例如,THIS值保存在表中

或者(我不知道,这是不是更好的做法):对于这种情况,表中还有另一个特殊单元格吗?例如,如果用户选择"A",则该值将保存在单元格"所有权类型"中,而单元格"其他类型"为空。但如果他选择了其他,那么"所有权类型"具有"其他"值,他键入的内容将保存在"其他类型"中

有什么建议吗?

更新:谢谢回复。你能告诉我我做错了什么吗?我现在尝试用这样的方式来实现它(我不懂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:一个讨厌的小错误由于一切都很完美,出现了一个问题:您指定"其他"按钮,将显示一个隐藏的文本字段。您决定忽略它并继续填写完整的申请表。在您按下提交(SEND)后,会出现我们的beforeValidate规则中指定的错误。完美,但是:隐藏的文本字段再次被隐藏。为了让它再次出现,用户必须点击另一个单选按钮(例如,PRIVATE COMPANY HERE),然后点击回"其他",直到隐藏的文本字段出现。亲爱的亚历克斯,我需要你的帮助。并不是每个人都能猜到这些操作

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

更新:this.value!='"其他","其他"-是文本,值将不同。试试这个

this.options[this.selectedIndex].innerHTML

或者更好地检查的值

console.log(this.value);

最好将'onchange'=>'return muFun(this.value)'更改为'onchange'=>'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()方法中删除它们的"required"规则。

如果要使用规则验证company_type_of_ownership_other,请创建一个自定义验证规则。(注意,为了可读性,我使用了company_ownership_type_other_description而不是company_type_of_ownership_other……你可以选择你喜欢的)

您的模型应该具有以下属性(或数据库表中的字段)

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

它的基本作用是:根据文本字段(在规则中设置)运行验证规则$属性为'company_ownership_type_other_description'。

"getAttributeLabel"为文本字段提供标签(如果已在模型中设置)。

顺便说一句,我只是想添加一些关于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

也必须指定(此处为NOT EMPTY)。然后我们可以添加一些错误文本,比如"请指定所有权类型",它就会起作用!荷拉!