Yii CActiveForm 组建的使用

1 创建 Form 表单

<?php $form = $this->beginWidget('CActiveForm', array(
    'id'=>'user-form', //标志id
    'enableAjaxValidation'=>true,  //是否开启ajax验证
    'enableClientValidation'=>true, //是否开启客户端验证
    'focus'=>array($model,'firstName'), //焦点聚焦位置
)); ?>

<?php echo $form->errorSummary($model); ?> //错误汇总

<div class="row">
    <?php echo $form->labelEx($model,'firstName'); ?> //标签
    <?php echo $form->textField($model,'firstName'); ?> //文本域
    <?php echo $form->error($model,'firstName'); ?> //错误提示
</div>
<div class="row">
    <?php echo $form->labelEx($model,'lastName'); ?>
    <?php echo $form->textField($model,'lastName'); ?>
    <?php echo $form->error($model,'lastName'); ?>
</div>

<?php $this->endWidget(); ?>

2 处理表单

public function actionCreate()
{
    $model=new User;
    $this->performAjaxValidation($model);  //如果开启了ajaxValidation 
    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];
        if($model->save())
            $this->redirect('index');
    }
    $this->render('create',array('model'=>$model));
}

protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}                                                                                           

3 常见的表单域


a 文本输入框

public string textField(CModel $model, string $attribute, array $htmlOptions=array ( ))


b checkbox

public string  checkBox( CModel $model, string $attribute, array $htmlOptions=array ( ))
一组复选框

public string checkBoxList(CModel $model, string $attribute, array $data//标签, array $htmlOptions=array ( ))

 c 下拉菜单

public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))

d 隐藏域

public string hiddenField(CModel $model, string $attribute, array $htmlOptions=array ( ))

 e label 标签

public string labelEx(CModel $model, string $attribute, array $htmlOptions=array ( )) //自动的提取model中各个字段的label

f 单选框

public string radioButton(CModel $model, string $attribute, array $htmlOptions=array ( ))

一组单选框

public string radioButtonList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))


g textarea

public string textArea(CModel $model, string $attribute, array $htmlOptions=array ( ))



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值