Yii Prepends 'models_Forms_' To The Model Name In $_Post

Hello there,

I am new to Yii and PHP and started to work on an account registration form using CActiveForm and a derived class from CFormModel named RegistrationAccountForm.

When CActiveForm is submitted, $_POST doesn’t contain the ‘RegistrationAccountForm’ key, but rather ‘models_forms_RegistrationAccountForm’. Everything else is works well.

Is this the normal behaviour?

I viewed the HTML source and the name of the fields linked to the model are named such as models_forms_RegistrationAccountForm[email].

Thanks in advance!

If you did not set form ID, the system will set unique ID for you. Check if you have any other element use RegisterationAccountForm already, so system have to add the prefix for you.

No, it’s not.

Plz paste the code of form creation, seems like you have a typo somewhere.

Here is the piece of code that configures the CActiveForm instance.




	$wdgRegisterForm = $this->beginWidget('system.web.widgets.CActiveForm', array(

		'id'=>'register-account',

		'action'=>Yii::app()->urlManager->createUrl($this->id . '/mainContainer.registerAccount'),

		'enableAjaxValidation'=>true,

		'enableClientValidation'=>true,

		'htmlOptions'=>array(

			'class'=>'uk-form uk-form-stacked',

			'enctype'=>'multipart/form-data'

		),

		'clientOptions'=>array(

			'validationDelay'=>0,

			'validateOnSubmit'=>true,

			'validateOnChange'=>false,

			'hideErrorMessage'=>true,

After this I have some JavaScript validation routines and here comes how the e-mail field is declared:


	echo $wdgRegisterForm->textField($this->modelRegister, 'email', array(

    			'id'=>'txtRegisterEmail',

    			'class'=>'uk-width-1-1',

    			'placeholder'=>'Adresa de e-mail')) . PHP_EOL;

?>

echo $wdgRegisterForm->error($this->modelRegister, 'email', array(

	'inputID'=>'txtRegisterEmail',

	'validateOnChange'=>true,

	'validateOnType'=>true

));



There is also a captcha with a captcha field tied to the form model:


	$this->widget('system.web.widgets.captcha.CCaptcha', array(

		'captchaAction'=>$this->id . '/mainContainer.captcha',

		'buttonLabel'=>'Genereaza cod nou',

		'buttonOptions'=>array(

			'id'=>'btnRegisterNewCaptcha'

		),

		'imageOptions'=>array(

			'id'=>'imgRegisterCaptcha'

		),

	));

	echo $wdgRegisterForm->textField($this->modelRegister, 'verifyCode', array(

		'id'=>'txtRegisterCaptcha',

		'class'=>'uk-align-left uk-form-width-medium',

		'placeholder'=>'Scrie codul din imagine'

	));

	echo $wdgRegisterForm->error($this->modelRegister, 'verifyCode', array(

		'inputID'=>'txtRegisterCaptcha'));

?>

These pieces of code are written in the main layout and modelRegister is an instance of RegisterAccountForm that is instantiated in a behavior attached to the controller.

Here is how RegisterAccountForm looks:


namespace models\forms;


use \Yii;

use \CFormModel;

use \CCaptcha;

use \models\Users;


class RegisterAccountForm extends CFormModel

{

        /**

	 * Model Attributes

	 */

	public $email;

	public $verifyCode;


	// validation rules for attributes

	public function rules() {

		return array(

			array('email', 'length', 'allowEmpty'=>false, 'min'=>6, 'max'=>70),

			array('email', 'email'),

			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'captchaAction'=>Yii::app()->controller->id . '/mainContainer.captcha')

		);

	}

}

There is also a lot of HTML code that was stripped.

Seems like the problem is caused by namespace usage, because here’s how the input field name is generated.

Try to remove it and see what will happen.

You are right! That was it!

Thank you very much! :)