yii 在运行时进行验证吗?

sczxawaw  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(274)

我有一个用户Model和一个permisssion模型。在用户表单中有2个字段主管和城市。在权限表中,我为required设置1,为非必需设置0。
喜欢

Permission table

user_role      supervisor      city
1                    1              1
2                    1              0
3                    0              0

现在,每当我在用户表单中选择一个角色时,我希望城市和主管(如果为1,则为该user_role激活所需规则)

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'lms-user-form',
    'enableAjaxValidation'=>true,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>
<div class="row">
        <?php echo $form->labelEx($model,'user_role'); ?>
        <?php echo $form->drodownList($model,'user_role',array(); ?>
        <?php echo $form->error($model,'user_role'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'user_first_name'); ?>
        <?php echo $form->textField($model,'user_first_name',array('size'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($model,'user_first_name'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'user_last_name'); ?>
        <?php echo $form->textField($model,'user_last_name',array('size'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($model,'user_last_name'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'user_password'); ?>
        <?php echo $form->textField($model,'user_password',array('size'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($model,'user_password'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'user_city'); ?>
        <?php echo $form->textField($model,'user_city',array('size'=>10,'maxlength'=>10)); ?>
        <?php echo $form->error($model,'user_city'); ?>
    </div>
<div class="row">
        <?php echo $form->labelEx($model,'user_supervisor'); ?>
        <?php echo $form->textField($model,'user_supervisor',array('size'=>10,'maxlength'=>10)); ?>
        <?php echo $form->error($model,'user_supervisor'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

我用了类似的东西,但这个不起作用

$permiss = Permission::model->findByAttributes(array('user_role'=>$model=>user_role));

    if($permiss->user_city && $permiss->user_supervisor){

    $model->rules(array('user_city','user_supervisor','required'));
    }
    elseif($permiss->user_city)
   {

    $model->rules(array('user_city','required'));
    }
 elseif($permiss->user_supervisor)
   {

    $model->rules(array('user_supervisor','required'));
    }

我知道addError,但我希望发生ajax validation

s6fujrry

s6fujrry1#

为什么不使用自定义验证?
你应该试试这个来解决你的问题。
方案一:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'lms-user-form',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'clientOptions'=>array(
     'validateOnSubmit'=>true),
)); ?>

在你的行动中使用这个

$this->performAjaxValidation($model);

备选方案二:
如上所述添加和自定义验证:

array('user_role', 'customValidation'),

public function customValidation($attribute, $params)
    {
        $user_role = $this->attributes['user_role'];
        $permiss   = Permission::model()->findByAttributes(array('user_role' => $user_role));
        if ($permiss->user_city && $permiss->user_supervisor) {
            if ($this->user_city == '') {
                $this->addError('user_city', "User city cannot be blank.");
            }
            if ($this->user_supervisor == '') {
                $this->addError('user_supervisor',
                    "User supervisor cannot be blank.");
            }
        } else if ($permiss->user_city) {
            if ($this->user_city == '') {
                $this->addError('user_city', "User city cannot be blank.");
            }
        } elseif ($permiss->user_supervisor) {
            if ($this->user_supervisor == '') {
                $this->addError('user_supervisor',
                    "User supervisor cannot be blank.");
            }
        }
    }

还有一件事你需要在performAjaxValidation($model)中添加表单名称method.so,它只起作用。
在两种方式中 AJAX 验证都有效。

相关问题