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.

Help












