Problems with CFormBuilder and nested forms

Hello,

I’m having trouble creating a simple registration form using CFormBuilder. The form is supposed to save values for two different models, User and Profile. You can find my code below.

UserController.php




...

/**

* Displays the registeration page

*/

public function actionRegister()

{

	// Create the registration form

	$form = new CForm('application.views.user.registerForm');

	$form['user']->model = new User();

	$form['profile']->model = new Profile();


	// Form is submitted and data is valid

	if( $form->submitted('register')===true && $form->validate()!==false )

	{

		// Get the user- and profile model

		$user = $form['user']->model;

		$profile = $form['profile']->model;


		// Save the user without validation

		if( $user->save(false)!==false )

		{

			// Save the profile without validation

			$profile->user_id = $user->id;

			$profile->save(false);


			// Everything saved, redirect

			$this->redirect(array('site/index'));

		}

	}


	// Render the view

	$this->render('register', array('form'=>$form->render()));

}

...



registrationForm.php




<?php

return array(

	'elements'=>array(

		'user'=>array(

			'type'=>'form',

			'elements'=>array(

				'username'=>array(

					'type'=>'text',

				),

				'password'=>array(

					'type'=>'password',

				),

				'email'=>array(

					'type'=>'text',

				),

			),

		),

		'profile'=>array(

			'type'=>'form',

			'elements'=>array(

				'firstname'=>array(

					'type'=>'text',

				),

				'lastname'=>array(

					'type'=>'text',

				),

			),

		),

	),

	'buttons'=>array(

		'register'=>array(

			'type'=>'submit',

			'label'=>'Register',

		),

	),

);

?>



register.php




<?php

$this->pageTitle = 'Register | '. Yii::app()->name;

$this->breadcrumbs = array(

	'User',

	'Register',

);

?>


<h1>Register</h1>


<div class="form">

	<?php echo $form; ?>

</div>



When the form is submitted it tries to look for firstname in the User model (it’s located in the Profile model).

The error I’m getting is:

Property "User.firstname" is not defined

I tried to look for someone with a similar problem but without any luck. I’d appreciate any advice, I feel like I’ve tried everything. :(


Also, I was wondering if I always need to create a custom form model (that extends CFormModel)? If so, how do I specify which member variables belong to which model? Is it even possible to create models extending CFormModel with multiple models?


Thank you for reading.

What is the error call stack?

Hello,

Thanks for the quick reply Qiang.

Here’s my error callstack:




#0 C:\wamp\www\yii\framework\db\ar\CActiveRecord.php(106): CComponent->__get('firstname')

#1 C:\wamp\www\yii\framework\validators\CRequiredValidator.php(46): CActiveRecord->__get('firstname')

#2 C:\wamp\www\yii\framework\validators\CValidator.php(176): CRequiredValidator->validateAttribute(Object(User), 'firstname')

#3 C:\wamp\www\yii\framework\base\CModel.php(150): CValidator->validate(Object(User), NULL)

#4 C:\wamp\www\yii\framework\web\form\CForm.php(195): CModel->validate()

#5 C:\wamp\www\crisu\forum\protected\controllers\UserController.php(176): CForm->validate()

#6 C:\wamp\www\yii\framework\web\actions\CInlineAction.php(32): UserController->actionRegister()

#7 C:\wamp\www\yii\framework\web\CController.php(300): CInlineAction->run()

#8 C:\wamp\www\yii\framework\web\filters\CFilterChain.php(129): CController->runAction(Object(CInlineAction))

#9 C:\wamp\www\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#10 C:\wamp\www\yii\framework\web\CController.php(990): CFilter->filter(Object(CFilterChain))

#11 C:\wamp\www\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#12 C:\wamp\www\yii\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))

#13 C:\wamp\www\yii\framework\web\CController.php(283): CFilterChain->run()

#14 C:\wamp\www\yii\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#15 C:\wamp\www\yii\framework\web\CWebApplication.php(320): CController->run('register')

#16 C:\wamp\www\yii\framework\web\CWebApplication.php(120): CWebApplication->runController('user/register')

#17 C:\wamp\www\yii\framework\base\CApplication.php(135): CWebApplication->processRequest()

#18 C:\wamp\www\crisu\forum\index.php(11): CApplication->run()

#19 {main}



You have ‘firstname’ in your User validation rules?

Thanks a million!

Seems like I had not updated my model rules correctly.

I had been looking in the wrong place the whole time…

It works now after I updated my rules for both the models. :)