如何在yii中发布表单值

时间:2015-01-24 09:40:04

标签: php forms post yii

模型

<?php
 class AdminLogin extends CActiveRecord
 {
    public $admin_username;
    public $admin_password;

   public function rules()
   {
    return array(
        array('admin_username,admin_password','required','message'=>'This is required fields'),);
   }
 }
?>

控制器

<?php
class DashboardController extends Controller
{
   public function actionLogin()
   {
       $model= new AdminLogin;
       if(isset($_POST['btn_admin_login']))
       {
          $model->attributes=$_POST['AdminLogin'];
          if($model->validate())
          {
            echo $model->admin_username=$_POST['admin_username'];
            echo $model->admin_password=$_POST['admin_password'];
            exit;
          }
         else
         {
            print_r($model->getErrors());
         }
     }
     $this->renderPartial('//admin/admin-login',array('model'=>$model));
 }
}
?>

并且查看我正在使用HTML,但是使用带有$form->beginWidget();

的yii开始表单

1 个答案:

答案 0 :(得分:0)

_form.php

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableClientValidation'=>true,
'htmlOptions' => array(
    'class' => 'form-horizontal',
    ),)); ?>

        <?php echo $form->labelEx($user,'name',array('class' => 'control-label')); ?>
        <?php echo $form->textField($user,'name',array('size'=>45,'maxlength'=>45)); ?>
        <?php echo $form->error($user,'name'); ?>
        <?php echo CHtml::submitButton($user->isNewRecord ? 'Create' : 'Save',array('class' => 'btn')); ?>
<?php $this->endWidget(); ?>

控制器

public function actionCreate()
{
    $user=new User();
    $this->performAjaxValidation($user);
    if (isset($_POST['User'])) {
        $user->attributes = $_POST['User'];
        $user->save(false);
    }
    $this->render('_form',compact('user'));
}