无法在yii中的控制器中获取表单值


Unable to get form value in controller in yii

我正在学习YII,遇到了一个问题。我正在尝试制作一个表单,但无法从控制器中的表单中获取值。我不明白我做错了什么。这是我的

(模型)Logindetails.php

class Logindetails extends CActiveRecord {             
public $pass;
    //rest of the coding
public function rules() {
    return array(
        array('password', 'length', 'max'=>20),
    );
}

(视图)_form.php

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'logindetails-form',
)); ?>
<div class="row">
    <?php echo $form->labelEx($model,'pass'); ?>
    <?php echo $form->textField($model,'pass',array('size'=>20,'maxlength'=>20)); ?>
    <?php echo $form->error($model,'pass'); ?>
</div>
<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>

控制器

public function actionCreate(){
    $model=new Logindetails;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['Logindetails'])) {
        $model->attributes=$_POST['Logindetails'];
        if(isset($model->pass)) {
            echo 'its present';
        } else {
            echo 'its absent';
        }
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

它不断向我显示它不在。为什么我有这个问题?

从上面的代码中,您只需更改控制器中的条件。

if(isset($model->pass)) 

if(isset($_POST['Logindetails']['pass']))
OR
if($model->pass != ""))

您可以调试代码以检查文本字段的值是否出现。

public function actionCreate(){
    $model=new Logindetails;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['Logindetails'])) {
        $model->attributes=$_POST['Logindetails'];
        print_r($model->attributes); //check either the values are coming or not.
        echo $model->pass ;
        exit; // finish here the program.
        if(isset($model->pass)) {
            echo 'its present';
        } else {
            echo 'its absent';
        }
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

我认为这将对你有所帮助。

谢谢。

(模型)Logindetails.php

你必须添加规则,

class Logindetails extends CActiveRecord {             
public $pass;
public function rules() {
    return array(
        array('password', 'length', 'max'=>20),
        **array('pass', 'length', 'max'=>20),**
    );
}

添加后,您检查控制器中正在接收$model->pass值。