sxtuwy 2012-01-30 11:11:29 4708次浏览 4条回复 0 0 0

真是搞的不明白,突然间yii无法获取表单提交的数据?搞不懂是什么原因,各位有遇到过这种问题吗?说一说经验吧。

  • 回复于 2012-01-30 11:13 举报

    具体什么情况????

  • 回复于 2012-01-30 11:18 举报

    视图:

    <div class="pageFormContent nowrap" layoutH="10">
    <?php 
    $form=$this->beginWidget('CActiveForm', array(
        'id'=>'classroom-form',
        'enableAjaxValidation'=>false,
        'htmlOptions'=>array('onsubmit'=>'return validateCallback(this,dialogAjaxDone)','class'=>'pageForm required-validate'),
    )); ?>
    
    <p class="note">带<span class="required">*</span>号的字段是必填的.</p>
    
    <?php echo $form->errorSummary($model); ?>
    
    <dl>
        <dt><?php echo $form->labelEx($model,'title'); ?></dt>
        <dd><?php echo $form->textField($model,'title',array('size'=>8,'maxlength'=>8,'class'=>'required')); ?></dd>
        <dd><?php echo $form->error($model,'title'); ?></dd>
    </dl>
    
    <dl>
        <dt>  <?php echo $form->labelEx($model,'grade_id'); ?></dt>
        <dd><?php echo $form->textField($model,'grade_id',array('size'=>8,'maxlength'=>8,'class'=>'required')); ?></dd>
        <dd>  <?php echo $form->error($model,'grade_id'); ?></dd>
    </dl>
    
    <dl>
        <dt><?php echo CHtml::submitButton($model->isNewRecord ? '创建' : '保存'); ?></dt>
    </dl>
    
    <?php $this->endWidget(); ?>
    
    </div><!-- form -->
    

    模型:

    <?php
    /**
     * This is the model class for table "{{classroom}}".
     *
     * The followings are the available columns in table '{{classroom}}':
     * @property integer $id
     * @property string $title
     * @property string $grade_id
     */
    class Classroom extends CActiveRecord
    {
    	/**
    	 * Returns the static model of the specified AR class.
    	 * @param string $className active record class name.
    	 * @return Classroom the static model class
    	 */
    	public static function model($className=__CLASS__)
    	{
    		return parent::model($className);
    	}
    
    	/**
    	 * @return string the associated database table name
    	 */
    	public function tableName()
    	{
    		return '{{classroom}}';
    	}
    
    	/**
    	 * @return array validation rules for model attributes.
    	 */
    	public function rules()
    	{
    		// NOTE: you should only define rules for those attributes that
    		// will receive user inputs.
    		return array(
    			//array('title, grade_id', 'required'),
    			//array('title, grade_id', 'length', 'max'=>8),
    			// The following rule is used by search().
    			// Please remove those attributes that should not be searched.
    			array('id, title, grade_id', 'safe', 'on'=>'search'),
    		);
    	}
    
    	/**
    	 * @return array relational rules.
    	 */
    	public function relations()
    	{
    		// NOTE: you may need to adjust the relation name and the related
    		// class name for the relations automatically generated below.
    		return array(
    			'nianji' => array(self::BELONGS_TO, 'Grade', 'grade_id'),//班级属于年级
    			'student' => array(self::HAS_MANY, 'Student', 'classroom_id', 'condition'=>'classrooms.id=students.classroom_id','order'=>'classrooms.id DESC'),
    			'studentCount' => array(self::STAT, 'Student', 'classroom_id', 'condition'=>'classroom_id=classrooms.id'),
    		);
    	}
    
    	/**
    	 * @return array customized attribute labels (name=>label)
    	 */
    	public function attributeLabels()
    	{
    		return array(
    			'id' => 'ID',
    			'title' => 'Title',
    			'grade_id' => 'Grade',
    		);
    	}
    
    	/**
    	 * Retrieves a list of models based on the current search/filter conditions.
    	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
    	 */
    	public function search()
    	{
    		// Warning: Please modify the following code to remove attributes that
    		// should not be searched.
    
    		$criteria=new CDbCriteria;
    
    		$criteria->compare('id',$this->id);
    		$criteria->compare('title',$this->title,true);
    		$criteria->compare('grade_id',$this->grade_id,true);
    
    		return new CActiveDataProvider($this, array(
    			'criteria'=>$criteria,
    		));
    	}
    }
    

    折腾了一个上午,搞不明白,为什么控制器里面无法获取表单数据

  • 回复于 2012-01-30 11:36 举报
    public function actionCreate()
    {
        $model=new Classroom;
    
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
    
        if(isset($_POST['Classroom']))
        {
            $model->attributes=$_POST['Classroom'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }
    
        $this->render('create',array(
            'model'=>$model,
        ));
    }
    

    昨天晚上还正常,今天晚上起来就不正常了。太怪了。
    实在不行,重新生成一次代码算了。

  • 回复于 2012-09-08 13:02 举报

    我也出现这问题了,我把 'onsubmit'=>'return validateCallback(this,dialogAjaxDone)' 注释掉后就能获取了,但是,onsubmit就不能用了,求怎么办?

您需要登录后才可以回复。登录 | 立即注册