0

我正在使用 portlet 来显示联系表格 - 与作为标准生成的静态页面上的完全相同。我正在使用相同的 ContactForm 模型。表单显示但验证码未显示并报告“CCaptchaValidator.action“验证码”无效。无法在当前控制器中找到这样的操作。”

我是 yii 的新手,所以要花很多时间才能弄清楚所有事情。有什么快速建议我会做些什么来让它工作吗?

最好的!

Yii::import('zii.widgets.CPortlet');

class ContactFormCard extends CPortlet
{
    public $title='Contact';

protected function renderContent()
{
    $model=new ContactForm;
    if(isset($_POST['ContactForm']))
    {
        $model->attributes=$_POST['ContactForm'];
        if($model->validate())
            $this->controller->refresh();
    }
    $this->render('contactFormCard',array('model'=>$model));
}
public function actions()
{
    return array(
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
        ),
    );
}

}

在 portlet 视图中:

<?php if(CCaptcha::checkRequirements()): ?>
    <div class="row">
        <?php echo $form->labelEx($model,'verifyCode'); ?>
        <div>
        <?php $this->widget('CCaptcha'); ?>
        <?php echo $form->textField($model,'verifyCode'); ?>
        </div>
        <div class="hint">Please enter the letters as they are shown in the image above.
        <br/>Letters are not case-sensitive.</div>
        <?php echo $form->error($model,'verifyCode'); ?>
    </div>
    <?php endif; ?>
4

1 回答 1

2

在您的 SiteController.php 中添加:

    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
            'backColor' => 0xFFFFFF,
            ),

            // [...]
        );
    }

在您的 ContactForm.php 中添加:

public function rules() {
    return array(
        // [...]

        array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message' => Yii::t('formsErros', 'Código de verificação incorreto.')),

        // [...]
    );
}

在你看来.php

<!-- [...] -->

<?php if(CCaptcha::checkRequirements()): ?>
    <div class="captcha">
            <?=$form->labelEx($contactFormModel, 'verifyCode')?>
            <?php $this->widget('CCaptcha', array('clickableImage' => true, 'showRefreshButton' => true, 'imageOptions' => array('id' => 'captchaContactForm', 'class' => 'clickableCursor'), 'buttonLabel' => '')); ?>
            <?=$form->textField($contactFormModel, 'verifyCode', array('class' => 'verticalAlignBottom'))?> 
            <?=$form->error($contactFormModel, 'verifyCode')?>
    </div>
<?php endif; ?>

<!-- [...] -->
于 2012-08-13T13:08:45.077 回答