您有一个用户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
出现
答案 0 :(得分:0)
为什么不使用自定义验证?
你应该尝试这个问题。
选项1:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'lms-user-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true),
)); ?>
在你的行动中使用这个
$this->performAjaxValidation($model);
选项2:
如上添加和自定义验证:
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验证都有效。希望它有所帮助