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

StackOverflow https://stackoverflow.com/questions/23151214

  •  05-07-2023
  •  | 
  •  

質問

For example, we have:

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

If user selects A, then A is being saved in the 'company_type_of_ownership' cell.

But how to realize the next feature?

User selects "Other(Please specify)" and there is a special textfield where he can type "My own type of ownership", for example and THIS value is being saved in the table.

Or (I don't know, if its a better practise): There is another special cell in the table for such case? For example, if user selects "A", then this value is being saved in the cell "TYPE OF OWNERSHIP", and cell "OTHER TYPE" is empty then. But if he selects other, then "TYPE OF OWNERSHIP" has "Other" value, and what he types is being saved in "OTHER TYPE"

Any suggestions, please?

UPDATE: Thanks for reply. Could you please tell what am i doing wrong? I try to implement it in a such way now (I'm poor in js, so have found this in the web):

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

When I choose OTHER, then this txtfield becomes visible. I've created a special cell in the table for it: 'company_type_of_ownership_other' But if I type something into it, it is not being saved into my db. What could be the problem and if it the same thing you suggested? Thank you

UPDATE 2: A little nasty bug Since everything works perfect, one problem occured: You specify "Other" button and a hidden textfield appears. You decide to ignore it and go on fullfilling application form. After you press SUBMIT (SEND), the error specified in our beforeValidate rules appears. PERFECT, BUT: The hidden textfield becomes hidden again. And to make it appear again user HAS TO CLICK ON ANOTHER radio button (for example, PRIVATE COMPANY HERE) and then click back to Other - only then the hidden textfield appears. Dear Alex, i need your help. Not really everyone can guess to make these manipulations.

役に立ちましたか?

解決

Add hidden textfield, when user select Other show hidden field with jvascript, then in controller check this field and create new record for it.

Update: this.value != 'Other', 'Other' - is text, value will be different. Try this

this.options[this.selectedIndex].innerHTML

or better check what value will be there

console.log(this.value);

Better change 'onchange'=>'return muFun(this.value)' to 'onchange'=>'return muFun(this)'. Then function will be

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

And in beforeValidate method of model you should manually check that one of this two fields is filled. Remove 'required' rule for them from rules() method of model.

他のヒント

If you want to validate that company_type_of_ownership_other using rules, create a custom Validation rule. (Note that I have used company_ownership_type_other_description for readability instead of company_type_of_ownership_other... you can chose whichever you like)

Your model should have the following attributes (or fields in the db table)

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

add the following to rules:

array('company_ownership_type_other_description',
        'validateOtherCompanyTypeOwnershipText'),

add the following method to your model:

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

What this basically does is: Validation rule is run against the text field (set in the rules). $attribute is 'company_ownership_type_other_description'.

'getAttributeLabel' gives the label for the text field (if you have set it in the model).

By the way, just wanted to add some useful info about beforeValidate: For this example, we need to add:

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

So, what does it do? If the "Other, please specify" radio is selected. then this rule:

$this->company_type_of_ownership=='Other'

works and it is required that this one rule:

&& !$this->company_type_of_ownership_other

also has to be specified (NOT EMPTY here). Then we can add some error text, like 'Please, specify the type of ownership' and it will work! Hola!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top