[ask] not show a validation in action create

hi all, i wanna ask something

I have a db name CBC and a table name tbl_product_category and field like this:

-Id primary key,

-name unique,

-desc,

-channel_use unique,

-deleted

and i have a code in ProductCategoryController like this :


public function actionCreate()

	{

		$model=new ProductCategory;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			$row = Controller::kueri(2,$model);

			if ($row == null)

			{

				if($model->save())

				{

					$last = Yii::app()->db->getLastInsertID();

					Controller::aftersave("ProductCategory",2,$last);

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

				}

			}

			else //if($model->validate())

			{

				Controller::aftersave("ProductCategory",2,$row,true,$_POST['ProductCategory']);

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

			}

		}


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

			'model'=>$model,

		));

	}

	

	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

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


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

		{

			$data = Controller::beforeupdate("ProductCategory",$id,2);

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

			if($model->save())

			{

				Controller::afterupdate("ProductCategory",$id,$data,2);

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

			}

		}


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

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

				$connection = Yii::app()->db;

				$sql = "update tbl_product_category set channel_use = null where id = $id";

				$command = $connection->createCommand($sql)->execute();

				$data = Controller::beforeupdate("ProductCategory",$id,2);

				Controller::afterdelete($data,2);

				Controller::deleted("ProductCategory",$id);

				

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}

	

  • if i create a new name & new channel use, its working

  • if i create a new name & a channel that already in db,it can show a validation like this

    1861

untitled.JPG
and it means its working, the update also can show a validation like that

  • the problem is : if i delete one product category, i set the deleted in table tbl_product_category to 1, so the value its still in db,it not deleted. and i want create the product category use a name with i deleted it just now, and i use channel_use that already in db so its not working, its make error CDbException like this

1862

untitled2.JPG
.

why not showing the validation but the error, can you check the code i wrote , its anything wrong in my code. thanks.

Its because you have a UNIQUE index on that field… you would need to put a UNIQUE filed on 2 fields: that one + the deleted status…

sorry, i don’t understand what u said about that one, and if i use unique in deleted field in db, so i cant make many data just 2 data, because the deleted just can 0 and 1.

If you have a UNIQUE index on the field channel_use… your database will not allow you to have two equal values because your database does not know what "deleted" field means to you…

my idea on making an index on channel_use + deleted is not really good now that I think about it… as you would not be able to delete the second record…

only solution then is to create a normal INDEX (not unique) on channel_use… then in your application you need to check/validate that there are no duplicate (non deleted) values for that field

ok, thanks ^^

can i ask more question ?

how to display error from controller?

i have a code like this in controller.php(the controller.php is a global function):


public function aftersave($nama,$tanda,$id,$set=false,$data='')

	{

					

					$sql1 = "select channel_use from tbl_product_category where channel_use = ".$data['channel_use']."";

					$cek = $connection->createCommand($sql1)->queryScalar();

					if($cek == null)

					{

						$sql = "update ".$tabel." set last_user = '".Yii::app()->user->first_name."', first_user = '".Yii::app()->user->first_name."', last_ip = '".$_SERVER['REMOTE_ADDR']."', first_ip = '".$_SERVER['REMOTE_ADDR']."', last_update = NOW(), first_update = NOW(), deleted = 0, `desc`='".$data['desc']."',channel_use = '".$data['channel_use']."' where id = $id";

						$berita = "Create ".$nama." : Name : $current->name, desc : $current->desc, deleted : 0, channel_use : $current->channel_use ";

	

					}				

				else

					{

							$error=1;

							return $error;

					}

	}

and in ProductCategoryController the code is like this :


public function actionCreate()

	{

		$model=new ProductCategory;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			$row = Controller::kueri(2,$model);

			if ($row == null)

			{

				if($model->save())

				{

					$last = Yii::app()->db->getLastInsertID();

					Controller::aftersave("ProductCategory",2,$last);

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

				}

			}

			else //if($model->validate())

			{

				$cek = Controller::aftersave("ProductCategory",2,$row,true,$_POST['ProductCategory']);

				if($cek == 1)

				{

					[color="#FF0000"]$this->render('index',array('model'=>$model));[/color]

				}

				else

				{

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

				}

			}

		}


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

			'model'=>$model,

		));

	}

and in the _form.php

its already have a coding like this <?php echo $form->errorSummary($model); ?>

i tried this code $this->render(‘index’,array(‘model’=>$model)) but its not working, i have error if i use this code.

thanks.

It’s better to open a new thread for new problems… like I saw you already made - http://www.yiiframework.com/forum/index.php?/topic/21706-ask-display-error/

And it’s not good to post the same question more then once…

One more thing… are you using Yii 1.0.x. ? - I’m asking because you posted in that forum

sorry,about the post. i using yii 1.1.x.

NOTE: moved to proper sub-forum