[Solved] Form Doesnt Pass Variable

Hi. I made validator to check code entered by user is existing in database. But it doesn’t pass value of field! There are files:

controllers/SiteController.php




...

public function actionIndex()

	{

                $model=new CodeForm;

		if(isset($_POST['CodeForm']))

		{

                    if($model->validate())

                    {

                        $this->render('info',array('message'=>"Code is correct"));

                    } else { 

                        $this->render('info',array('message'=>$model->getError('code') ));

                    }

                } else {

                    $this->render('main',array('model'=>$model));

                }

	}

...



models/CodeForm.php




<?php


class CodeForm extends CFormModel

{

	public $code;

        public $referer;


	/**

	 * Declares the validation rules.

	 */

	public function rules()

	{

		return array(

			array('code', 'required'),

                        array('code', 'checkCode'),

                    

		);

	}


	/**

	 * Declares customized attribute labels.

	 * If not declared here, an attribute would have a label that is

	 * the same as its name with the first letter in upper case.

	 */

	public function attributeLabels()

	{

		return array(

			'code'=>'Code',

		);

	}

        

        public function checkCode($attribute,$params)

	{

	    var_dump($this->code);

            $this->addError('code',$this->code); 

	}

        

}

views/main.php


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'site-code-form',

        'enableClientValidation'=>true,

        'clientOptions'=>array(

            'validateOnSubmit'=>true,

        ),

)); ?>


	<p class="note">Pola z <span class="required">*</span> są wymagane.</p>


	<div class="row">

		<?php echo $form->labelEx($model,'code'); ?>

		<?php echo $form->textField($model,'code'); ?>

		<?php echo $form->error($model,'code'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Go!'); ?>

	</div>


<?php $this->endWidget(); ?>

When I write code and i click sumbit, always I get error: "Field Code can not be blank" and in left top is writed NULL, which is generated by var_dump. What may be wrong?

Hi,

you can put the code on acionIndex




if(Yii::app()->getRequest()->getIsAjaxRequest()) {


echo CActiveForm::validate( array( $model)); 


Yii::app()->end(); 


}



please see the link it may be helpful

http://help.discretelogix.com/php/yii/how-to-create-custom-validation-rule.htm

I dont use Ajax validation. But i try this code with turn ajax validation and this is response:


string(6) "43f3t3"

[]

Hi

you can write the query on checkCode function

You dont understand me. I deleted checkCode validator and there is only one rule: code, required. And after submit there is error: ‘Code can not be blank’. But there is send in request:


CodeForm[code]:testcode

yt0:Go!

Edit: I forgot this:


$model->attributes=$_POST['CodeForm'];