Dear Adde
I was thinking that problem is only about dealing with validation rules.
The problem here is how we are going to display validation errrors in search form.
I was just beating around validation rules. I feel sorry about that.
I hope I have found solution for that.
1.Modify the _search.php. Add code to display the errors.
fdate and sdate are not actual attributes. Here I have added them as virtual properties.
It was just done to simulate your scenario.
<div class="wide form">
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
)); ?>
<div class="row">
<?php echo $form->label($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'username'); //This is added. ?>
</div>
<div class="row">
<?php echo $form->label($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'fdate'); ?>
<?php echo $form->textField($model,'fdate'); ?>
<?php echo $form->error($model,'fdate'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'sdate'); ?>
<?php echo $form->textField($model,'sdate'); ?>
<?php echo $form->error($model,'sdate'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- search-form -->
2.Now modify the actionAdmin method in controller.
I have done in the following way.
public function actionAdmin()
{
$model=new User('search');
$model->unsetAttributes(); // clear any default values
$model->clearErrors(); // clear any default errors sticking
if(isset($_GET['User']))
{
$model->attributes=$_GET['User'];
$model->validate(array('username','fdate','sdate'));//Here just add the attributes you need to validate.
$error=$model->hasErrors();
/*If any error occurs we are not going to display the grid. We are going to use $error in cgridview to decide whether we are going to display the grid or not.*/
$this->render('admin',array(
'model'=>$model,
'error'=>$error
));
}
else
{
$error=true;//By default we are not displaying the grid
$this->render('admin',array(
'model'=>$model,
'error'=>$error,));
}
}
3.In admin.php we are going to add $error.
I have displayed just part of the admin.php.
If there is any validation error ,grid will not be displayed.
<?php
//I have commented out the script below.
//At times it prevents errors getting not displayed.
//I think it prevents default behavior of submit button.(2nd script)
?>
/*Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').slideToggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('user-grid', {
data: $(this).serialize()
});
return false;
});
");*/
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<?php //set diplay as block ?>
<div class="search-form" style="display:block">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php if(!$error) //grid is displayed only when there are no validation errors.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'username',
'password',
'email',
'status',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
As I have already said fdate and sdate are not actual attributes.
I have made them as virtual properties.
4.Modification in AR model.
class User extends CActiveRecord
{
public $fdate;
public $sdate;
public function rules()
{ return array(
array('username', 'required', 'on'=>'search'),
array('fdate,sdate','date','format'=>'yyyy/MM/dd','on'=>'search'),
array('sdate','compare','compareAttribute'=>'fdate','operator'=>'>=','message'=>"sdate should
follow fdate",'on'=>'search'),
);
}
}
Althogh the fdate and sdate are not relevant to this model. I have added here
just simulate your scenario. They are nicely displaying errors.
Regards.