Check if record exists() and notify in popup

In create action I want to check if record exist (check 3 or more attributes)

and if exists don’t insert.

Please tell me if the below code is ok, it works but I want to know if there is any logical problem

I might have…And how can I show the error message in popup? Then users clicks ok and goes back to create form

and choses another participant.

Thank you in advance.




	public function actionCreate()

	{

		$model=new Registration;


		 // Uncomment the following line if AJAX validation is needed

		 // $this->performAjaxValidation($model);


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

		 {

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


                        $criteria = new CDbCriteria();

                        $criteria->condition = 'participantID=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/tongue.gif' class='bbc_emoticon' alt=':P' /> AND seminarID=:s AND .......';

                        $criteria->params = array('<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/tongue.gif' class='bbc_emoticon' alt=':P' />'=>$model->participantID, ':sch'=>$model->seminarID); 

			

                        if($model->exists($criteria)){

                                throw new CHttpException(404, 'Record exists');

                        }


                        elseif($model->save())

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

		}


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

			'model'=>$model,

		));

	}




Code is correct.

You can show popup passing a variable, called for example $error, to view to handle in that context displaying popup.

So you can change:




        public function actionCreate()

        {

                $error = null;

  

                $model=new Registration;


                 // Uncomment the following line if AJAX validation is needed

                 // $this->performAjaxValidation($model);


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

                 {

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


                        $criteria = new CDbCriteria();

                        $criteria->condition = 'participantID=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/tongue.gif' class='bbc_emoticon' alt=':P' /> AND seminarID=:s AND .......';

                        $criteria->params = array('<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/tongue.gif' class='bbc_emoticon' alt=':P' />'=>$model->participantID, ':sch'=>$model->seminarID); 

                        

                        if($model->exists($criteria)){

                                // throw new CHttpException(404, 'Record exists');

                                $error = 'Record exists';

                        }


                        elseif($model->save())

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

                }


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

                        'model'=>$model,

                        'error' => $error

                ));

        }



In your view




<?php if($error != null) { .... } ?>



I can’t make it work…

I do this:




<?php if($this->$error != null) { 


$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

    'id'=>'mydialog',

    'options'=>array(

        'title'=>'Test',

    ),

));


echo "Participant already registered";


$this->endWidget('zii.widgets.jui.CJuiDialog');


} ?>

It works only if I put a CHtml:link

But it should work when sending the form

What am I doing wrong?

Try to add in ‘options’ attribute ‘autoOpen’ with true value.

So:




<?php if($this->$error != null) { 


$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

    'id'=>'mydialog',

    'options'=>array(

        'title'=>'Test',

        'autoOpen' => true

    ),

));


echo "Participant already registered";


$this->endWidget('zii.widgets.jui.CJuiDialog');


} ?>



http://www.yiiframework.com/doc/api/1.1/CJuiDialog

Still not working…

I use:




<?php 

if($this->error != null){

	$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

			'id'=>'test',

			'options'=>array(

					'title'=>'Test',

					'autoOpen'=>true,

					'buttons'=>array(

						'OK'=>'js:function(){$(this).dialog("close");}'

				)

			)

	));

	echo "Participant already registered, choose another";

	$this->endWidget('zii.widgets.CJuiDialog');

}

?>



When creating a record which already exists, form doesn’t submit, stays on create page until a non existing record is created. This is fine except the popup is not showing…

If I do the following, popup is showing if create page is loaded…


if($this->error == null)

Any idea how to fix it?

Have you tried to check html source after you want insert a record that exists?

To check if html is ok and there arent’ warning message in javascript console.

No warning message or error in javascript console