0

I am implmenting a captcha type, bot checking where instead of having users fill out the irritating capture we create a hidden field called password_confirm and then when checking posted variables if it contains a value an error will be made.

I have read around and apparently it would be best to implement in a beforeValidate() event handler as opposed to creating a new validation rule.

However I am unsure how to stop the process and how to return an error message if that is even necessary, as when you call validate() it will clear all existing error messages.

Also not too sure if we can set it up using scenarions so only in the register scenario does it apply.

In the form Widget:

<div class="row">
        <?php echo $form->hiddenField($model, 'password_confirm'); ?>
        <?php echo $form->error($model,'password_confirm'); ?>
    </div>

In the FormModel:

class LoginForm extends CFormModel
{
public $password_confirm; 

...

protected function beforeValidate() 
    {
    if(empty($this->password_confirm)) 
        {
            return false;
        }
        else 
        {
            return parent::beforeValidate();
        }
    }

I got a Property Not Defined Error: So I added to the User model:

public $password_confirm; 

But then after registering, It didn't show anything except the Application log. There are probably a number of problems here, I sorry for the convoluted question.

2 Answers 2

1

try this

protected function beforeValidate() 
    {
    if(empty($this->password_confirm)) 
        {
            $this->addError('password_confirm','You crazy bot... go away');
        }
        else 
        {
            return parent::beforeValidate();
        }
    }

Surely you can add error if the validation fails using $this->addError($attribute,$message);

Here $attribute is the field name corresponding to which you want to add error and $message is the message you want to display

Sign up to request clarification or add additional context in comments.

Comments

0

first you are using a hidden field in your form, hidden, which you won't be able to see, or fill it normally! so change that to a password field,

you can new your model with specific scenario and check for that scenario using getScenario(), and I also think beforeValidate() is a good place for that,

and you can add error to specific attribute, when your logic occurs using addError() and do a manual return false; to stop the process,

and when validation fails you can do getErrors() for all that has went wrong

5 Comments

which you won't be able to see, or fill it normally! correct, so I bot will be tricked into filling the field, but a normal user will not. Hence when it is filled I know it is a bot.
you want to fill this or not?! decide man :D . you said you wanted to show error if not filled right?
Incorrect, read ` if it contains a value an error will be made.`
Then I suggest you make it a visible text/password field, and hide it using javascript.
it really doesn't matter if it is visible or not, the key is to place your logic in beforeValidate() and check for that logic and do necessary functions there, which are adding errors and ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.