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

set value for password field is ignored

Recently switched from 1.3.4 to 2.0.6 and noticed, that the password field in the form, where I left value empty is filled again... I try now to set an empty value for password field like

$field->setAttribute('value', null);

and it seems not working at all. For all other elements like text, textarea etc. setAttribute works ok. Can it be a bug?

When you said filled do you mean it is pulling data from somewhere?



1.3k

Yes, the data comes from the model. new Form($model)

edited Aug '15

Can you post some code?

Yes, the data comes from the model. new Form($model)



1.3k
Accepted
answer

As I understood, there are a couple of possibilities to change the field value:

First:

class YourForm extends \Phalcon\Forms\Form
{
    // your logic

    // has priority 1
    public function getCustomValue($name, $entity, $data)
    {
        if ($name == 'password') return '';
    }

    // AND/OR...

    // @see https://github.com/phalcon/cphalcon/blob/master/phalcon/forms/form.zep#L621
    // has priority 2
    public function getPassword()
    {
        return '';
    }
}

Second:

class Password extends \Phalcon\Forms\Element\Password
{
    // your logic

    public function prepareAttributes($attributes = null, $useChecked = false)
    {
        $default = parent::prepareAttributes($attributes);
        return array_merge($default, ['value' => null]);
    }
}

Third (if an entity used):

class ExampleModel extends \Phalcon\Mvc\Model
{
    // your logic

    // @see https://github.com/phalcon/cphalcon/blob/master/phalcon/forms/form.zep#L595
    public function getPassword()
    {
        return '';
    }
}

The problem is:

// @see https://github.com/phalcon/cphalcon/blob/master/phalcon/forms/element.zep#L520
/** @var \Phalcon\Forms\ElementInterface $element */
$element->clear();

doesn't work like in versions < 2.x and has no effect on the entity form element. And

// @see https://github.com/phalcon/cphalcon/blob/master/phalcon/forms/element.zep#L475
$element->_value

is just a default element value with last priority and is set if no any other values given...