我正在创建一个“创建”用户的模块,我在模型规则中设置了默认值,但每当我选择“创建”时,它仍显示为“必需”
我正在尝试创建一个新用户,但我在尝试创建用户时遇到了麻烦,因为创建用户所需的某些值必须是默认值,我不太确定如何设置。我还需要输入一个不同的表,而实际的“创建”发生在不同的控制器上。
这是我的表单代码:
<?php
/* @var $this SystemUserController */
/* @var $model SystemUser */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'system-user-form',
'enableAjaxValidation'=>false,
)); ?>
<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,'party_id'); ?>
<?php echo $form->textField($model,'party_id',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'party_id'); ?>
</div>
!-->
<div class="row" id="toshow" style="display:none" name="suppliers"> <?php $supplier = SupplierHead::model()->findAll();
$list = CHtml::listData($supplier ,'head_id','head_name');
echo $form->DropDownList($model,'party_id',
$list, array('prompt'=>'Select Supplier')); ?>
</div>
<button id="abutton">Already a Supplier</button>
<script>
$(document).ready(function() {
$("#abutton").click(function(e){
e.preventDefault();
$("#toshow").css('display', 'block');
});
});
</script>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>200)); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<script>
$("#supplier").click(function () {
$("#suppliers").show("slow");
});
</script>
<!--
<div class="row">
<?php echo $form->labelEx($model,'date_last_login'); ?>
<?php echo $form->textField($model,'date_last_login'); ?>
<?php echo $form->error($model,'date_last_login'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'status'); ?>
<?php echo $form->textField($model,'status',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'status'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'date_created'); ?>
<?php echo $form->textField($model,'date_created'); ?>
<?php echo $form->error($model,'date_created'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'date_modified'); ?>
<?php echo $form->textField($model,'date_modified'); ?>
<?php echo $form->error($model,'date_modified'); ?>
</div>
--!>
<div class="row">
<?php echo $form->labelEx($model,'user_role'); ?>
<?php echo $form->textField($model,'user_role',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'user_role'); ?>
</div>
<!--
<div class="row">
<?php echo $form->labelEx($model,'isLogin'); ?>
<?php echo $form->textField($model,'isLogin'); ?>
<?php echo $form->error($model,'isLogin'); ?>
</div>
--!>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
正如您所看到的,我已经注释掉了我不想使用的属性。我还修复了SystemUser模型属性rules(),以定义用户输入不需要哪些属性:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password,' 'required'),
array('username', 'length', 'max'=>200),
array('password', 'length', 'max'=>255),
array('user_role', 'default', 'value'=>'Member'),
array('date_created' 'default', 'value'=>new CDbExpression('NOW()'),
array('date_modified','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'update'),
array('isLogin,date_last_login' =>new CDbExpression('NOW()'),
array('status' 'default', 'value'=>'Approved'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('party_id, username, password,isLogin,status,date_created,date_modified user_role', 'safe', 'on'=>'search'),
);
}
最后,还有一个下面列表,我在表格中包含了需要插入到不同控制器模型中的下拉列表。我该怎么做?
需要默认值的属性如下:
date_last_login
status
date_created
date_modified
修改
我上传了一张当我选择“创建”时会发生什么的图片
我决定不添加默认值尝试将它们保持为NULL只是为了查看规则()是否有效。我
任何帮助?
答案 0 :(得分:0)
这帮助了我。
$model->save(false);
或者在控制器中,您可以在提交表单时为每个字段添加值。