Form->clear() doesn't reset element's value when using Form->isValid() #11978

Closed
cirot87 opened this Issue Jul 15, 2016 · 2 comments

Projects

None yet

2 participants

@cirot87
cirot87 commented Jul 15, 2016 edited

Hi, i have a login form with email and password elements. I need to clear the password value when validation fails.
In my /Phalcon/Forms/Form i have this:

$email = new Text('email', [
    'placeholder' => DI::getDefault()->get("translation")->query("Insert your Email")
]);
$email->addValidators( [
    new PresenceOf( [
        'message'       => DI::getDefault()->get("translation")->query("The field is required"),
        'cancelOnFail'  => true
    ]),
    new Email( [
        'message' => DI::getDefault()->get("translation")->query("Invalid email address")
    ])
]);

$password = new Password('password', [
    'placeholder' => DI::getDefault()->get("translation")->query("Insert your Password")
]);
$password->addValidators([
    new PresenceOf( [
        'message' => DI::getDefault()->get("translation")->query("The field is required"),
        'cancelOnFail' => true
    ]),
    new StringLength([
        'min'            => 7,
        'messageMinimum' => DI::getDefault()->get("translation")->query("The text is too short")
    ])
]);
$password->setDefault("");
$password->clear();

$this->add($email);
$this->add($password); 

in my view i have a simple render for both, and after the post i check the value with the isValid() function.
The problem is that when i have a validation error the password won't reset.

I try this for debugging:

Tag::setDefault("password", "cir");
$form->get('password')->clear();
$form->get('password')->setDefault('ci');
$form->clear(['password']);

echo "<br>Tag -------> ".Tag::getValue('password');
echo "<br>Form ------> ".$form->getValue('password');
echo "<br>Element ---> ".$form->get('password')->getValue();

but none of those reset my value on the form.

The problem is that when using isValid() which calls the bind() function,.
The bind() set automatically all post parameters into _data (a protected variable of the form)
let this->_data = data;
and this clear function never change the parameters inserted into this variable.

public function clear(var fields = null) -> <Form>
{
    var elements, element;

    let elements = this->_elements;
    if typeof elements == "array" {
        for element in elements {
            if typeof fields != "array" {
                element->clear();
            } else {
                if in_array(element->getName(), fields) {
                    element->clear();
                }
            }
        }
    }
    return this;
}

My solution

public function clear($fields = null)
{
    if (is_null($fields)) {
        $this->_data = [];
    } else {
        if (is_array($fields)) {
            foreach ($fields as $field) {
                $this->_data[$field] = "";
            }
        } else {
            $this->_data[$fields] = "";
        }
    }
    parent::clear($fields);
}
@sergeyklay sergeyklay added this to the 3.0.2 milestone Oct 1, 2016
@sergeyklay sergeyklay self-assigned this Oct 1, 2016
@sergeyklay
Collaborator

@cirot87 Could you please take a look at #12280

@sergeyklay
Collaborator
sergeyklay commented Oct 2, 2016 edited

Fixed in 3.0.x branch.
Could you please check 3.0.x branch?

git clone git@github.com:phalcon/cphalcon.git
cd cphalcon
git checkout 3.0.x

zephir fullclean
zephir build
@sergeyklay sergeyklay closed this Oct 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment