簡體   English   中英

Yii Framework-在主視圖中,我無法添加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; ?> ,它可以工作,但實際上不會將信息發送到數據庫。

我嘗試了另一種視圖(索引視圖),沒有它,它就起作用了: <?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'),
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM