logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Post Validators ONE AT A TIME

How can I execute post validators one at a time?

This can be explained by an example, I have a login form with a captcha.

There is a post validator to check that the username and password match, and another post validator to check that the captcha is correct.

If the username and passwords are correct but the captcha, I only get an error from the captcha, but this renders the captcha useless because it reveals that the user/password are correct.

Is there a way to execute them in order so that the user/password validator is only executed once the captcha has been validated?

This question has been answered.

fxsymfony | 10/07/10 at 9:14pm Edit


(8) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/07/10
    9:41pm
    Arturo Linares says:

    Don't run the second validator if you already have errors in the first one. Use an if statement inside your postValidator class.

    An example showing your post validator doClean() method:


    <?php
    protected function doClean($values)
    {
    $errorSchema = new sfValidatorErrorSchema($this);
    if ($invalid_login)
    {
    $errorSchema->addError(new sfValidatorError($this, 'login'), 'login');
    }

    if (count($errorSchema) == 0 && $invalid_captcha)
    {
    $errorSchema->addError(new sfValidatorError($this, 'captcha'), 'captcha');
    }

    if (count($errorSchema) > 0)
    {
    throw new sfValidationErrorSchema($this, $errorSchema);
    }

    return $values;
    }
    ?>

    Previous versions of this answer: 10/07/10 at 9:41pm

  • avatar
    Last edited:
    10/07/10
    10:52pm
    Damian Martinelli says:

    Please try with:

    $this->validatorSchema->setPostValidator(new sfValidatorAnd(new sfValidatorAnd(array(
    new sfValidatorSchemaReCaptcha('challenge', 'response', $options = array()),
    new sfValidatorCallback(array('callback' => array($this, 'checkPassword'))))))
    );

  • avatar
    Last edited:
    10/08/10
    10:08pm
    Jimish Gamit says:

    try with 'halt_on_error'. by default it's false.

    Symfony tests all the validators and declares the validation failed if at least one of them fails. And even if some of the rules of the validation file fail, symfony will still look for a validateXXX() method and execute it. So the two validation techniques are complementary. The advantage is that, in a form with multiple failures, all the error messages are shown.


    $this->validatorSchema->setPostValidator(
    new sfValidatorAnd(
    array(
    new sfValidatorSchemaReCaptcha('challenge', 'response', $options = array()),
    new sfValidatorCallback(array('callback' => array($this, 'checkPassword')))
    ),
    array('halt_on_error'=>true)
    )
    );


    I am sure it will work for you. :)

    Previous versions of this answer: 10/08/10 at 3:14am | 10/08/10 at 12:23pm

  • avatar
    Last edited:
    10/08/10
    11:16am
    Nate Flink says:

    As I understand the problem, it relates to the fact that the capcha image validator call is in a separate form class from your username/password form. I haven't used the symfony capcha plugin before, but to my way of thinking, the solution to this issue would be to consolidate both form classes to a single class that handles all the desired validation and application logic.

    This should be fairly easy to achieve by simply overriding the plugin's form class. Make a copy of the plugin's form class and save to yourproject/lib/form/whateverForm.class.php

    Then copy the username / password submission form stuff to that one. From there it should be a trivial matter to tweak the validators to get them to behave the way you want.



  • avatar
    Last edited:
    10/08/10
    12:16pm
    Ivan Rey says:

    The halt_on_error option is the best way to do it as Jimish Gamit posted.

  • avatar
    Last edited:
    10/07/10
    9:59pm
    fxsymfony says:

    But in my case both post validators are separate methods, so I wouldn't know how to test them as conditions like in your code. This is what I have in the form->configure():

    $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
    new sfValidatorSchemaReCaptcha('challenge', 'response', $options = array()),
    new sfValidatorCallback(array('callback' => array($this, 'checkPassword')))))
    );

    So I created my own "checkPassword" method, and the one for the captcha is provided in the plugin. I need somehow to execute them one at a time in a sequence.

  • avatar
    Last edited:
    10/07/10
    10:57pm
    fxsymfony says:

    Tried it but it doesnt change anything, I mean if my username/password are correct I only get an error for the captcha, if my username/password are not correct I get both error messages. This renders the captcha useless.

    What I'm looking for is being able to run the captcha postvalidator first, so that the username/password are only checked when the text entered in the captcha is valid.

  • avatar
    Last edited:
    10/08/10
    10:08pm
    fxsymfony says:

    Worked perfectly! thanks!

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.