Yii小部件,验证后无法重定向

时间:2013-11-04 09:09:15

标签: yii yii-components

我有小部件并在布局中调用它,为什么不重定向到站点/搜索?

<?php
class CSearch extends CWidget
{
    public $model;
    public function init()
    {
        $this->model = new SearchForm();
        if(Yii::app()->request->isAjaxRequest && Yii::app()->request->getPost('FormSearch'))
        {
            $this->model->attributes = Yii::app()->request->getParam('FormSearch');
            if($this->model->validate())
            {
                $this->owner->redirect(array('site/search', 'model' => $this->model->attributes));
            }
        }
    }
    public function run()
    {
        $this->render('search', array('model' => $this->model));
    }
}

表格

<div class="header-search">
    <?php $form = $this->beginWidget('CActiveForm', array(
        'id' => 'search-form',
        'enableAjaxValidation' => true,
        'enableClientValidation' => true,
        'clientOptions' => array(
            'validateOnSubmit' => true,
            'validateOnChange' => false,
            'validateOnType' => false),
        'htmlOptions' => array('class' => 'form-search'),
    )); ?>
    <?php echo $form->error($model, 'search'); ?>
        <div class="input-append">
            <?php echo $form->textField($model, 'search', array('class' => 'span2', 'placeholder' => 'Поиск')); ?>
            <?php echo CHtml::htmlButton('<i class="icon-search"></i>', array('class' => 'btn', 'submit' => array('site/search')))?>
        </div>
    <?php $this->endWidget(); ?>
</div>

如何通过集团Yii正确验证?对不起我的英文

1 个答案:

答案 0 :(得分:1)

尝试改为:

$this->owner->redirect(array('site/search', 'model' => $this->model->attributes));

请:

$route_params =  $this->model->attributes;
array_unshift($route_params, 'site/search');
$this->owner->redirect($route_params);

必须创建网址/site/search?field1=va1&field2=val2...

但我看代码:你的变体也必须工作......比你需要正确处理$ _GET参数,例如:

public function run()
{
    if(!empty($_GET))
    {
        $model = new ModelAR(); // you ActiveRecord class
        $model->attributes = $_GET;
        $this->render('search', array('model' => $model));
    }
}