We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

getValue() from Password Forms Element??

Hi

I'm trying to get the value of a password forms element..

It works fine when using getValue() on a Text Element, but when doing it from a Password element, I just get an empty string??

Any hints???

edited Jul '16

Just tested and it is workign as intended:

echo $form->get('textInput')->getValue() . '<br/>';
echo $form->get('passwordInput')->getValue();
exit;
// Outputs
test input value
test password value  

Perhaps somewhere in your script you unset the value?

Also was curious and checked Zephir source files, there is nothing special regarding to password values in both password element and form.

Hmmm

I have:


        $passwd = new Text('passwd');
        $passwd->setLabel('Password');
        $passwd->setFilters(array('striptags', 'string'));
        $passwd->addValidators(array(
            new PresenceOf(array(
                'message' => 'Password is required'
            )),
            new LoginValidator(array(
                'passwd' => $passwd->getValue(),
                'user' => $user->getValue(),
                'test' => "Test!"

                )
            ))
        ,['user','passwd','test']);
        $this->add($passwd);

The $passwd->getValue() passes an empty string to the LoginValidator.. but if I move "$this->add($passwd);" to above the addValidators part.. then it works???

edited Jul '16

Beacause it's dont know anything about data in form ? Obviously you need to add element to form first to get value from this element.



93.7k
Accepted
answer

$this->add() adds the element to the form, thats why it works when you move it above. I can't test your exact scenario at the moment because I'm at work, but I can show you how I realised custom validator for Google Recaptcha. You should be able to use it for your situtation:

My recaptcha field:

$recaptcha = new \Helpers\Recaptcha($name);
$recaptcha->addValidator(new \Helpers\RecaptchaValidator([
    'message' => $this->translations->public->forms->recaptchaError
]));

My validator class:

class RecaptchaValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Validation $validation, $attribute)
    {
        $value = $validation->getValue('g-recaptcha-response');
        $ip = $validation->request->getClientAddress();

        if (!$this->verify($value, $ip)) {
            $validation->appendMessage(new Message($this->getOption('message'), $attribute, 'Recaptcha'));
            return false;
        }
        return true;
    }

    /**
     * Verify recaptcha response
     *
     * @param string $value
     * @param string $ip
     * @return bool
     */
    protected function verify($value, $ip)
    {
        $config = loadConfig();
        $params = [
            'secret' => $config->sensitive->recaptcha->privateKey,
            'response' => $value,
            'remoteip' => $ip
        ];
        $response = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?' . http_build_query($params)));

        return (bool)$response->success;
    }
}

As you can see I'm not passing values to the validator, but getting the input values inside the Custom validator class.

In the valdiate() method above you can see how I'm accessing the input value, there you can get Username and Password values and check if login is correct.