Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:08:40+00:00 2026-06-04T02:08:40+00:00

I have a field customercode. it will be disabled if the value of the

  • 0

I have a field customercode. it will be disabled if the value of the listbox is staff and enabled if the listbox value is admin.
If case of admin, the customercode is required. but in case of staff the customercode is disabled and no rules has to applied.

How to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:08:41+00:00Added an answer on June 4, 2026 at 2:08 am

    I can think of two ways, or a combination of those. Both need some tweaking for perfect results, but they are pretty good:

    1) Scenario:
    Note that I have two fields / attributes: text / dropdown
    In your controller:

    public function actionScenario()
    {
        $model=new ScenarioForm;
    
        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='scenario-form')
        {
            if($_POST['ScenarioForm']['dropdown'] == 'admin')
                $model->scenario = 'admin';
    
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    
        // collect user input data
        if(isset($_POST['ScenarioForm']))
        {
            $model->attributes=$_POST['ScenarioForm'];
    
            if($model->dropdown == 'admin')
                $model->scenario = 'admin';
    
            // validate user input
            $model->validate();
            //do other stuff
        }
        // display the login form
        $this->render('scenario', array('model'=>$model));
    }
    

    As you can see, I check if the dropdown value is admin; if so I change the scenario of the model to ‘admin’. In the model rules, I have this:

    public function rules()
    {
            return array(
                    array('dropdown', 'required'),
                    array('text', 'required', 'on' => 'admin'),
            );
    }
    

    You see, text is only required when the scenario is admin. When you now have admin in the dropdown, and nothing in your text field, and submit the form, you will get the right errors. If you want instant, ajax feedback, when the user changes the dropdown, you might want this in your view:

        <?php echo $form->labelEx($model,'dropdown'); ?>
        <?php echo $form->dropDownList($model,'dropdown', array('admin' => 'Admin', 'staff' => 'Staff!')); ?>
        <?php echo $form->error($model,'dropdown', array('afterValidateAttribute' => 'js:function(form, attribute, data, hasError)
        { 
            mSettings = $.fn.yiiactiveform.getSettings(form);
            $.fn.yiiactiveform.updateInput(mSettings.attributes[1], data, form)
        }')); ?>
    

    The third parameter in the error for dropdown is an array which contains several html options, such asthe callback that is called after the dropdown is validated with ajax. Normally, only the current field is updated when you do ajax-validation (even though you get all errors). So we specify in the callback that also the errors for the text field should be updated. You will see now that you get an error when you select admin in the dropdown (not on startup).

    It’s not perfect, since you don’t get your nice required * for the text field, and you get the warning immediately. I am not sure if Yiiactiveform has a function for drawing the field decoration, rather then the warning. But you could probably extend it to do that, or use custom js in the callback.
    If you just need normal validation after submit, you can also add the required class to the text label with custom Javascript. I am not sure at the moment if CActiveForm has a special parameter in the attribute for that.

    2) custom validation rule.
    Similar to one, although you don’t use a scenario (but still the js callback in the view). Instead, you create a custom validation rule for the dropdown, and put it into the rules array. When the dropdown changes, we add an error to the text field.

    public function check_dropdown($attribute,$params){
        if($this->$attribute=="admin"){
            $this->addError('text', 'Please enter Text!');
    
            $this->clearErrors('text');
        }            
    }
    
    public function check_text($attribute,$params){
        if($this->$attribute!=""){
                $this->clearErrors('text');
        }
    }
    

    Here you don’t need the to set the scenario in the controller. But you lose the required rule. Same issue here also with the missing asterisk.

    I would probably go with way 1. If you don’t want an outright warning, you probably have to read out the error message from the JSON, check if it is the right one, and apply the right class to the label of text with custom Javascript in the callback function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field in a CCK node form that is hidden, and has
Table has 1 500 000 records, 1 250 000 of them have field =
I have Field f = this.getClass().getFields()[0]; I need to know if f 's value
How can I have field dependency? Case 1: If boolean field call_me is set,
For example, I have collection with documents, where documents can have field url (but
I have field codes set up in some documents which will display the current
I have field which contain a value like: F28_TIT245_0_90A_OOC_LCL by AARBITMA(aarbitma@LC13UPE-CIM) I want to
I have form with such field: categories = forms.ModelChoiceField( queryset=Field.objects.all(), widget=CheckboxSelectMultiple, required=False ) And
I have field like a_b_c_d I want as the output a b c_d, this
I have field X that contains text with spaces in the end of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.