Ajax Validation

hey,

I learn this beautiful framework and excuse me for my english but i’m a little french…

Well, the Ajax Validation seems to be not activate :

SubscriptionController.php


public function actionCreate()

	{

		$model=new Subscription;


		// Uncomment the following line if AJAX validation is needed

		 $this->performAjaxValidation($model);


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

		{

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

                        $model->subcriptionstart=time();

                        $model->subcriptionend=Subscription::add_date($day=0,$mth=Yii::app()->params['subcriptionperiodtry'],$yr=0);

                        $model->amount=Yii::app()->params['subcriptionamounttry'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

_form.php


<div class="form">


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

	'id'=>'subscription-form',

	'enableAjaxValidation'=>true,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($model,'amount',array('size'=>10,'maxlength'=>10)); ?>

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

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


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


</div><!-- form -->

And the page always refresh

:(

Two more things to do (since you’ve already uncommented the two lines of code needed):

  1. Check that the form name in performAjaxValidation() is ‘subscription-form’ (should be fine with auto-generated code)

  2. Add some validation rules to the Subscription model, e.g add ‘required’ validator




...

array('attribute, attribute2, attribute3', 'required'),

...



/Tommy

Thanks tommy but i always have the same problems :

in SubscriptionController.php :


protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='subscription-form')

		{

			echo CActiveForm::validate($model);

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

		}

	}

and Models/Subscription.php :


public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('users_id, subcriptionstart, subcriptionend, amount', 'required'),

			array('users_id, subcriptionstart, subcriptionend', 'numerical', 'integerOnly'=>true),

			array('amount', 'length', 'max'=>10),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, users_id, subcriptionstart, subcriptionend, amount', 'safe', 'on'=>'search'),

		);

	}

Sorry, But if you can help me…thanks a lot

Guil182

A couple of further steps to take:

  1. I suggest you use Firebug Console to verify there’s an ajax request submitted from the create form, e.g when you tab away from an empty required field.

  2. Enable Yii file logging and add Yii::trace() calls, e.g. to the performAjaxValidation() method.

/Tommy

Yes, that’s it…i have no ajax request submitted.

I put in attachments the screen of the firebug console and the .zip of my project.

Really, it’s a little mistake and i don’t find…

Thanks a lot.

The Firebug Console may be disabled, if not sure click on the console tab to display the context menu. Check that the console is enabled.

/Tommy

The console Firebug is enabled…:(

Did you notice any Javascript error messages? Since no ajax requests seems to be submitted you’ll have to do some client side debugging.

Is jQuery loaded and not in conflict with other scripts?

On change event handler attached to the input element?

First of all delete all subdirs from your AppRoot/assets directory, it will be regenerated.

/Tommy

This 2 scripts are loaded :


<script type="text/javascript" src="/yii/winetracker/assets/e682b645/jquery.js"></script>

<script type="text/javascript" src="/yii/winetracker/assets/e682b645/jquery.yiiactiveform.js"></script> 

In firebug/DOM i have this :


jQuery

	function()

 

	

active

	0

 

	

ajaxSettings

	Object { url="http://127.0.0.1:8888/y...p?r=subscription/create", global=true, more...}

 

	

accepts

	Object { xml="application/xml, text/xml", html="text/html", more...}

 

	

async

	true

 

	

contentType

	"application/x-www-form-urlencoded"

 

	

global

	true

 

	

processData

	true

 

	

type

	"GET"

 

	

url

	"http://127.0.0.1:8888/y...p?r=subscription/create"

 

	

xhr

	function()

Is active=0 normal ?

one things, when i click on a active zone which is ok, it take the green color…so ajax seems to be ok…it’s really strange.

Active seems to be a counter of active ajax requests. 0 should be normal (I first saw the value 1 until I updated the DOM view).

(It’s possible to see attached events by right-clicking a field and inspect it.)

Are you saying valid fields turn greeen when you tab away from them or click somewhere else? So what exactly isn’t working? In the screen dump you attached in post #5 there is an error message displayed. I guess you submitted the form before it showed up.

/Tommy

I find !!!!

the code generated is :

_form.php :


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

	'id'=>'subscription-form',

	'enableAjaxValidation'=>true,

)); ?>

and to active Ajax validation, you must add this line :


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

	'id'=>'subscription-form',

	'enableAjaxValidation'=>true,

        'clientOptions'=>array('validateOnSubmit'=>true),


)); ?>

I hope it will help some other people…

PS : Why this line isn’t generated ?

Thanks Tommy to take time to help me.

Glad you solved it, but what you say is kinda strange since validateOnChange should be the default.

/Tommy

Yes it’s true for validateOnChange.

When i clic on a box, ajax validation is enabled but not on submit…

But a have a second problem…now :


	public function actionCreate()

	{

		$model=new Subscription;


		// Uncomment the following line if AJAX validation is needed

		 $this->performAjaxValidation($model);


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

		{

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

                        $model->subcriptionstart=time();

                        $model->subcriptionend=Subscription::add_date($day=0,$mth=Yii::app()->params['subcriptionperiodtry'],$yr=0);

                        $model->amount=Yii::app()->params['subcriptionamounttry'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

Without Ajax all the zone load with this code, but not with Ajax…

:(

Hey everyone,

Is someone know why :

Without Ajax :


        public function actionCreate()

        {

                $model=new Subscription;


                // Uncomment the following line if AJAX validation is needed

                 $this->performAjaxValidation($model);


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

                {

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

                        $model->subcriptionstart=time();

                        $model->subcriptionend=Subscription::add_date($day=0,$mth=Yii::app()->params['subcriptionperiodtry'],$yr=0);

                        $model->amount=Yii::app()->params['subcriptionamounttry'];

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }


                $this->render('create',array(

                        'model'=>$model,

                ));

        }

When i create a record, the validation work and with ajax :


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

        'id'=>'subscription-form',

        'enableAjaxValidation'=>true,

        'clientOptions'=>array('validateOnSubmit'=>true),


)); ?>

PS : I need to add “clientOptions’=>array(‘validateOnSubmit’=>true)” if i want ajax to work.

each zone of the model is never updated


        public function actionCreate()

        {

                $model=new Subscription;


                // Uncomment the following line if AJAX validation is needed

                 $this->performAjaxValidation($model);


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

                {

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

                        $model->subcriptionstart=time();

                        $model->subcriptionend=Subscription::add_date($day=0,$mth=Yii::app()->params['subcriptionperiodtry'],$yr=0);

                        $model->amount=Yii::app()->params['subcriptionamounttry'];

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }


                $this->render('create',array(

                        'model'=>$model,

                ));

        }

Thanks a lot for your answer :)