Сори что поднимаю старую тему, но столкнулся с такой же проблемой.
В итоге решил использовать сценарии - но ничего не вышло.
Вот мой код:
Контроллер:
Код: Выделить всё
public function actions() {
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha' => array(
'class' => 'CCaptchaAction',
'backColor' => 0xFFFFFF,
'testLimit' => 1,
),
....
....
public function actionPostNew() {
....
if (isset($_POST['Posts'])) {
$post->attributes = $_POST['Posts'];
$post->verifyCode = $_POST['Posts']['verifyCode'];
if (Yii::app()->user->isGuest) {
$post->setScenario("noAjax");
}
if ($post->validate()) {
// form inputs are valid, do something here
if ($post->save())
$this->redirect($_SERVER['HTTP_REFERER']);
}
}
$this->render('application.components.views.postNew', array('post' => $post));
Валидация:
Код: Выделить всё
...
array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), "on" => "noAjax"),
...
Так вот - проблема оказалась в том что при
проходит валидация
еще раз, во время которой генерится уже
другая капча, а так как капча уже другая то save не сохраняет.
Т.к. код в CActiveRecord
Код: Выделить всё
public function save($runValidation=true,$attributes=null)
{
if(!$runValidation || $this->validate($attributes)){
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
}
else{
return false;
}
}
В итоге пришел к тому что при решении этой проблемы нужно использовать сценарии и отказаться от второй валидации:
Код: Выделить всё
if ($post->save(false))
$this->redirect($_SERVER['HTTP_REFERER']);
P.S. Версии Yii: 1.1.8