CActiveForm对象不呈现JS代码

时间:2012-11-07 20:50:48

标签: yii

我有一个简单的CFormModel子类,用作更改密码的表单。该类只有两个属性(password1,password2 - 用于确认 - )。这是代码:

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?

1 个答案:

答案 0 :(得分:2)

为需要验证的字段添加$form->error()。我想你在控制器中执行ajax验证,如果没有 - 按performAjaxValidation here搜索