我有一个简单的 CFormModel 子类,用作更改密码的表单。该类只有两个属性(密码1、密码2-用于确认-)。这是代码:
class ChangePasswordForm extends CFormModel {
public $password1;
public $password2;
public function rules() {
return array(
array('password1, password2', 'required'),
array('password2', 'compare', 'compareAttribute'=>'password1'),
array('password1, password2', 'safe'),
);
}
public function attributeLabels() {
return array(
'password1' => 'Enter new password',
'password2' => 'Confirm new password',
);
}
}
在控制器视图文件中,我使用 CActiveForm 对象来呈现此表单:
<?php
$form=$this->beginWidget('CActiveForm',
array(
'id'=>'change-pwd-form',
'action'=>array('site/changePasswordPost'),
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)
);
echo $form->errorSummary($changePwdForm);
?>
<fieldset><legend>Change your password</legend>
<div class="row">
<?php
echo $form->labelEx($changePwdForm, 'password1');
echo $form->passwordField($changePwdForm, 'password1');
?>
</div>
<div class="row">
<?php
echo $form->labelEx($changePwdForm, 'password2');
echo $form->passwordField($changePwdForm, 'password2');
?>
</div>
<div class="row">
<?php
echo CHtml::submitButton('Change password');
?>
</div>
</fieldset>
<?php
$this->endWidget();
?>
在控制器操作中,我渲染上面的视图,传递一个 ChangePasswordForm 对象:
$changePwdForm = new ChangePasswordForm;
$this->render('changePassword', array('userid'=>$userid, 'userType'=>$t,
'changePwdForm'=>$changePwdForm));
Yii::app()->end();
问题是在渲染视图时,没有生成 Javascript 代码。没有什么。所以点击提交按钮,虽然它不应该让我这样做(因为需要两个密码字段),但它确实提交了。上面的代码有什么问题并且没有创建 Javascript?