1

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

<?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 正确验证的?对不起我的英语不好

4

1 回答 1

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);

它必须创建 URL/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));
    }
}
于 2013-11-04T09:23:26.090 回答