Yii Framework Forum: Problems with CFormBuilder and nested forms - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problems with CFormBuilder and nested forms Rate Topic: -----

#1 User is offline   Chris83 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 27-February 10
  • Location:Helsinki, Finland

Posted 02 March 2010 - 03:28 PM

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.
Best regards,
Chris

My contribution to the Yii community:
Account | Yiistrap | Auth | Bootstrap | NordCms | Rights | LESS | SEO | Img

Follow me:
Twitter | GitHub | Bitbucket
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 02 March 2010 - 03:42 PM

What is the error call stack?
0

#3 User is offline   Chris83 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 27-February 10
  • Location:Helsinki, Finland

Posted 02 March 2010 - 03:46 PM

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}

Best regards,
Chris

My contribution to the Yii community:
Account | Yiistrap | Auth | Bootstrap | NordCms | Rights | LESS | SEO | Img

Follow me:
Twitter | GitHub | Bitbucket
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 02 March 2010 - 03:51 PM

You have 'firstname' in your User validation rules?
0

#5 User is offline   Chris83 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 27-February 10
  • Location:Helsinki, Finland

Posted 02 March 2010 - 03:58 PM

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. :)
Best regards,
Chris

My contribution to the Yii community:
Account | Yiistrap | Auth | Bootstrap | NordCms | Rights | LESS | SEO | Img

Follow me:
Twitter | GitHub | Bitbucket
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users