Yii框架 - 在主视图中我无法添加CActiveForm,因为它需要一个模型,当我设置模型时没有任何反应

时间:2014-11-04 01:53:32

标签: php forms yii model yii-cactiverecord

我把它放在我的视图上,我必须添加<?php $model = new Usuarios; ?>并且它有效,但实际上并没有将信息发送到数据库。

我尝试了另一个视图(索引视图),如果没有它,它可以工作:<?php $model = new Usuarios; ?>

<a class="list-group-item">
    <form role="form">
        <div class="form">
            <?php $model = new Usuarios; ?>
            <?php $form = $this->beginWidget('CActiveForm', array(
                'id' => 'usuarios-form',
                'action' => $this->createUrl("usuarios/create"),
                'enableAjaxValidation' => false,
            )); ?>

            <?php echo $form->errorSummary($model); ?>

            <div style="padding:1px;" class="input-group input-group-sm">
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-user" style="color:white"></span>
                </span>
                <?php echo $form->textField($model, 'Nombre', array('maxlength' => 128, 'placeholder' => 'Nombre y Apellido')); ?>
                <?php echo $form->error($model, 'Nombre'); ?>
            </div>

            <div class="row buttons" style="padding:4%; color:white ; font-size:1.5vmax; font-family: Signika; border-radius:30px">
                <center>
                    <?php echo CHtml::submitButton($model->isNewRecord ? 'Enviar' : 'Save'); ?>
                </center>
            </div>
            <?php $this->endWidget(); ?>   
        </div>
    </form>
</a>

1 个答案:

答案 0 :(得分:0)

这是您的控制器操作应该是这样的:

public function actionCreate() {
    $model = new Usuarios;

    if(isset($_POST['Usuarios'])) {

        // Populate the model with values from form
        // These attributes must be set to safe in the model rules() function
        $model->attributes = $_POST['Usuarios'];

        // ->save() will validate the model with the validation rules 
        // in the Usuarios.php model. If you do not want validation, use ->update()
        if($model->save()) {
            $this->redirect(array('view', 'id' => $model->primaryKey));
        }
    }

    $this->render('create', array(
        'model' => $model, // You pass the model to the view page
    ));
}

在您的模型中,您需要更新rules()函数以接受用于保存在数据库中的字段:

public function rules() {
    return array(
        array('Nombre', 'safe'),
    );
}