ajax capcha

i hava a form on page that does not use model. this logic i cannot change.

i need to add capcha for the form. how to do it without model?

The fact is that i have a model with capcha rule and i need to validate capcha of that model on my form.

I don’t understand how capcha widget links with model. Can i load capcha for my model through ajax?

Captcha links with model by property ‘captchaAction’ in validation rules. Widget is build upon those validation rules - it takes url to action and basing on that widget is being built.

When you use code such as:


		<?php $this->widget('CCaptcha'); ?>

		<?php echo $form->textField($model,'verifyCode'); ?>

your model must be instance of either CModel or CFormModel with following validation rule defined:


array('verifyCode', 'captcha', 'allowEmpty' => YII_DEBUG || !\CCaptcha::checkRequirements(), 'captchaAction' => 'urltocaptcha'),

and also with following property:


    

    /**

     * @var string

     */

    public $verifyCode;

Please attach some codes of your forms/models/views related to this problem.

I’ll show you my example form used in my project using this captcha:




<?php

/**

 * @class ContactUsForm

 */

class ContactUsForm extends CFormModel

{

    /**

     * @var string

     */

    public $name;

    

    /**

     * @var string

     */

    public $email;

    

    /**

     * @var string 

     */

    public $subject;

    

    /**

     * @var string

     */

    public $body;

    

    /**

     * @var string

     */

    public $verifyCode;


    /**

     * Declares the validation rules.

     */

    public function rules()

    {

        return array(

            // name, email, subject and body are required

            array('name, email, subject, body', 'required'),

            // email has to be a valid email address

            array('email', 'email'),

            // verifyCode needs to be entered correctly

            array('verifyCode', 'captcha', 'allowEmpty' => YII_DEBUG || !\CCaptcha::checkRequirements(), 'captchaAction' => 'captcha/captcha'),

        );

    }


    /**

     * @return array

     */

    public function attributeLabels()

    {

        return array(

            'name' => Yii::t('misc', 'label_name'),

            'email' => Yii::t('misc', 'label_email'),

            'subject' => Yii::t('misc', 'label_subject'),

            'body' => Yii::t('misc', 'label_body'),

            'verifyCode' => Yii::t('misc', 'label_verify_code'),

        );

    }

}



and then in the view:




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