Modelbinding issues

Coming from .Net MVC, the model binding in Yii is not working as I expected, and as a result, I’m having difficulty handling forms.

Right now, I have a Model ‘UserModel’ which contains a number of basic string fields, and extends CActiveRecord

I have made a ViewModel for a form page called UserProfileEditForm which extends CFormModel and consists of just one var $user

My controller looks like this:


public function actionEditProfile($id = null)

        {

            $model = new UserProfileEditForm;

            

            if(isset($_POST['ajax']) && $_POST['ajax'] == 'profile-form')

            {

                echo CActiveForm::validate($model);

                Yii::app()->end();

            }

            

            if(isset($_POST['UserProfileEditForm']))

            {

                $model->attributes = $_POST['UserProfileEditForm'];

                if($model->validate())

                {

                    echo "validated";

                }

            }

            

            $model->user = UserModel::model()->findByPk($id);

            $model->submitted = 1;

            $this->render('editProfile', array('model' => $model));

        }

As you can see, what I want to do is to populate my UserProfileEditForm’s $user property with a populated UserModel.

Here is a snippet from my view:




<?php

                            $form = $this->beginWidget('CActiveForm', array(

                               'id' => 'profile-form',

                                'enableClientValidation' => true,

                                //'enableAjaxValidation' => true,

                                'clientOptions' => array(

                                    'validateOnSubmit' => true

                                ),

                                'htmlOptions' => array(

                                    'class' => 'form-horizontal'

                                )

                            ));

                        ?>

                            

                        <fieldset>

                            <legend>Personal Information</legend>

                            

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

                            

                            <div class="control-group">

                                <?php echo $form->labelEx($model->user, "username", array('class' => 'control-label', 'for' => 'username')); ?>

                                <div class="controls">

                                    <?php echo $form->textField($model->user, "username"); ?>

                                    <?php echo $form->error($model->user, "username"); ?>

                                </div>

                            </div>



So, you can see that in my fields, I am referring to UserProfileEditForm’s $user property. The fields are showing the correct data.

However, when I submit the form, what is returned in $_POST is not my UserProfileEditForm, but only a UserModel. (I mean that I have a $_POST[UserModel] and no $_POST[UserProfileEditForm])

In .Net MVC, I can just have the action in my controller that handles the post response receive a strongly typed UserProfileEditForm argument, but no such luck here.

Right now it’s just annoying/confusing, but the real issue lies in that when I am using a multidimensional/nested ViewModel that consists of Models that themselves consist of other models, I’m losing the relationships when I get the POST.

Any help or suggestion of what I need to do, or if I need to approach the whole thing differently would be greatly appreciated.

Thanks,

Jason