Yii框架-在主视图中,我可以';不要添加CActiveForm,因为它需要一个模型,当我设置模型时,什么都不会发


Yii Framework - in a main view I can't add a CActiveForm because it needs a model, when I set the model nothing happens

我把它放在我的视图中,我必须添加<?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>

这就是控制器操作的样子:

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'),
    );
}