Ciao devo creare un form di registrazione su più pagine e sono entrato in difficoltà. Ho iniziato con il creare piu model per ogni stato della view ma i form non si validano più.
<?php
class RegisterForm1 extends CFormModel
{
public $username;
public $password;
public $mail;
public function rules()
{
return array(
// name, email, subject and body are required
array('username, password, mail', 'required'),
// email has to be a valid email address
array('mail', 'email')
);
}
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
?>
<?php
class RegisterForm2 extends CFormModel
{
public $nazione;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('nazione', 'required'),
);
}
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
?>
public function actionRegister()
{
$model1=new RegisterForm1;
$model2=new RegisterForm2;
$page = 1;
if(isset($_POST['RegisterForm1'],$_POST['RegisterForm2']))
{
$model1->attributes=$_POST['RegisterForm1'];
$model2->attributes=$_POST['RegisterForm2'];
if($model1->validate())
{
$page=2;
$this->render('register',array('model1'=>$model1,'model2'=>$model2,'page'=>$page));
return;
}
}
$this->render('register',array('model1'=>$model1,'model2'=>$model2,'page'=>$page));
}
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'register-form-register-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary(array($model1,$model2)); ?>
<?php if ($page=1){?>
<div class="row">
<?php echo $form->labelEx($model1,'username'); ?>
<?php echo $form->textField($model1,'username'); ?>
<?php echo $form->error($model1,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model1,'password'); ?>
<?php echo $form->textField($model1,'password'); ?>
<?php echo $form->error($model1,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model1,'mail'); ?>
<?php echo $form->textField($model1,'mail'); ?>
<?php echo $form->error($model1,'mail'); ?>
</div>
<?php }else if($page=2){?>
<div class="row">
<?php echo $form->labelEx($model2,'nazione'); ?>
<?php echo $form->textField($model2,'nazione'); ?>
<?php echo $form->error($model2,'nazione'); ?>
</div>
<?php }?>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
<?php echo($page) ?>
</div><!-- form -->