如果未禁用该字段,则为该代码字段创建规则 [英] Creating rules for a code field if that field is not disabled

查看:25
本文介绍了如果未禁用该字段,则为该代码字段创建规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字段客户代码.如果列表框的值为人员,它将被禁用,如果列表框的值为 admin,它将被启用.如果是管理员,则需要客户代码.但如果是员工,则客户代码被禁用,无需应用任何规则.

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.

怎么做?

推荐答案

我可以想到两种方法,或者它们的组合.两者都需要进行一些调整以获得完美的结果,但它们都非常好:

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

1) 场景:请注意,我有两个字段/属性:文本/下拉列表在您的控制器中:

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

如您所见,我检查下拉值是否为 admin;如果是这样,我将模型的场景更改为管理员".在模型规则中,我有这个:

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

你看,只有当场景是管理员时才需要文本.当您现在在下拉列表中有 admin 并且文本字段中没有任何内容并提交表单时,您将得到正确的错误.如果您想要即时的 ajax 反馈,当用户更改下拉列表时,您可能希望在您的视图中显示:

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

dropdown 错误中的第三个参数是一个数组,其中包含多个 html 选项,例如使用 ajax 验证下拉列表后调用的回调.通常,当您进行 ajax 验证时,只会更新当前字段(即使您收到所有错误).所以我们在回调中指定文本字段的错误也应该更新.您现在会看到,当您在下拉列表中选择 admin 时(不是在启动时)会出现错误.

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

它并不完美,因为您没有在文本字段中获得所需的 *,并且您会立即收到警告.我不确定 Yiiactiveform 是否具有绘制字段装饰的功能,而不是警告.但是你可以扩展它来做到这一点,或者在回调中使用自定义 js.如果提交后只需要正常验证,也可以使用自定义Javascript将所需的类添加到文本标签中.我目前不确定 CActiveForm 的属性中是否有一个特殊的参数.

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) 自定义验证规则.类似于一个,虽然你没有使用场景(但仍然是视图中的js回调).相反,您为下拉列表创建自定义验证规则,并将其放入规则数组中.当下拉列表发生变化时,我们会在文本字段中添加一个错误.

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.

我可能会采用方式 1.如果您不想要直接警告,您可能必须从 JSON 中读出错误消息,检查它是否正确,并将正确的类应用于标签回调函数中带有自定义 Javascript 的文本.

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.

这篇关于如果未禁用该字段,则为该代码字段创建规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆