Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

cirot87 opened this issue Jul 15, 2016 · 2 comments
Assignees
Labels
bug A bug report status: medium Medium
Milestone

Comments

@cirot87
Copy link

cirot87 commented Jul 15, 2016

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
Copy link
Contributor

@cirot87 Could you please take a look at #12280

@sergeyklay
Copy link
Contributor

sergeyklay commented Oct 2, 2016

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

git clone [email protected]:phalcon/cphalcon.git
cd cphalcon
git checkout 3.0.x

zephir fullclean
zephir build

@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

3 participants