0

Although there are quite a lot of questions concerning Yii2 captchas as well as problems with saving, I couldn't find and answer that solved my problem. Hope someone can help:

In my website I included a contact form along with a captcha. Therefor I added code to the model, to the IndexController and the form as described here: https://yii2-cookbook-test.readthedocs.io/forms-captcha/#how-add-captcha-to-a-form The captcha is displayed and prevents from submitting the form if the code wasn't entered correctly. However, if I include the captcha validation rules, the message is not saved to the database. So there seems to be something wrong with the validation rule.

['verifyCode', 'captcha', 'captchaAction' => '/contact/index/captcha']

I just don't see what is wrong here. When I comment it out, it the model is saved but the captcha isn't validated. Leaving the part with "captchaAction" out leads to an Invalid Captcha ID error, the solution is to include the captchaAction as described here: Yii2 Invalid CAPTCHA action ID in module

Does anyone have an idea of what might be wrong here? Running in circles...

Form:

...
 <div class="form-group">
     <div><?= Yii::t('ContactModule.form', 'Please enter the letters from the image.'); ?></div>
         <?= $form->field($model, 'verifyCode')->widget(Captcha::class, [
                            'captchaAction' => '/contact/index/captcha',
                       ])->label(false); ?>
 </div> 
...

Model:

class Contact extends ActiveRecord{
    public $verifyCode;

       /**
    * @inheritdoc
    */
   public static function tableName()
   {
       return 'contact';
   }
   /**
    * @inheritdoc
    */
    public function rules()
    {
        return [
            [['name','email','message'], 'required'], //Checking that all the fields are required
            ['email', 'email'], // Validating that email is a valid email
            [['name'],'string', 'max' => 50], //Verifying that name is not greater than its max length
            [['email'], 'string', 'max' => 50], //Verifying that email is not greater than its max length
            [['message'], 'string', 'max' => 255],//Verifying that message is not greater than its max length
            ['state', 'boolean'],
            [['date'], 'date', 'format' => 'php:Y-m-d'],
            ['verifyCode', 'captcha', 'captchaAction' => '/contact/index/captcha'],           
        ];
    }
...

Controller

class IndexController extends Controller
{

    public $subLayout = "@contact/views/layouts/default";

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
            ]
        ];
    }

    /**
     * Renders the index view for the module
     *
     * @return string
     */
    public function actionIndex()
    {
        $model = new Contact();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->save();
...
Younica
  • 3
  • 2

0 Answers0