i want to avoid the saving on database whenever the data thats being stored on the field is the same as is being on db (to avoid duplications), in yii, i use the form made by gii, im making a renderpartial on another view, so you might see the code in a view action, its totally normal, here are the codes.
controller/action(view)
public function actionView($id)
{
$dec=new DecretoCaracterizacion;
if(isset($_POST['DecretoCaracterizacion']))
{
$error='error';
$model=DecretoCaracterizacion::model()->findAll();
$dec->attributes=$_POST['DecretoCaracterizacion'];
if(in_array($_POST['DecretoCaracterizacion']['decreto_id'] , $model))
$this->redirect(array('view', 'id'=>$id, 'error'=>$error));
elseif($dec->save())
$this->redirect(array('view', 'id'=>$id));
}
$this->render('view',array(
'model'=>$this->loadModel($id), 'dec'=>$dec
));
}
form renderpatial for DecretoCaracterizacion
<?php
/* @var $this EquipoController */
/* @var $car Equipo */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'decretocaracterizacion-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Campos con <span class="required">*</span> son requeridos.</p>
<?php echo $form->errorSummary($dec); ?>
<div class="row">
<?php echo $form->labelEx($dec,'decreto_id'); ?>
<?php echo $form->dropDownList($dec,'decreto_id',CHtml::listData(Decreto::model()->findAll(),'id','ndecreto'), array('empty'=>'Seleccione decreto', 'value'=>'0')); ?> <p class="note">Valor por defecto "0"</p>
<?php echo $form->error($dec,'decreto_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($dec,'caracterizacion_id'); ?>
<?php echo $form->textField($dec, 'caracterizacion_id', array('readOnly'=>'true', 'value'=>$car->id)); ?>
<?php echo $form->error($dec,'caracterizacion_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($dec->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
what i want here, i dont want the decreto_id to be duplicated, even if i see its not being duplicated, i see in DB thats being duplicated but dont know why its not shown in the view.
update: theres one caracterizacion that can have many decretos, but cant have same decretos in 1 caracterizacion.