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

Attribute reset() fails if initial value does not pass the validator #1208

Open
ItsAsbreuk opened this issue Sep 20, 2013 · 4 comments
Open

Comments

@ItsAsbreuk
Copy link

This seems the right thing, but IMO is not.

It is very common to have an initial value that is undefined while having a validator that is restrictive. The thing is, when a user enters a valid value for the attribute, the reset() function DOES NOT clear its value (because value===null does not pass validation).

I have a situation like described below and it gives very unpleasant behaviour:

Y.MyModel = Y.Base.create('mymodel', Y.Model, [], {},
        {
        ATTRS : {
           password: {
              value: null,
              validator: function(v) {
                    return v && (typeof v==='string') && (v.length>=3) && (v.indexOf(' ')===-1);
                }
           }
        }
      });
      var mymodel = new Y.MyModel();
      // a user enters a new password which is set in the model:
      mymodel.set('password', 'thisisavalidpassword');
      // because the new password is qualified as 'wrong' (on the server-side), we need to reset:
      mymodel.reset();
      console.log(mymodel.get('password')); // <-- value === 'thisisavalidpassword'

See the jsbin:

http://jsbin.com/opEGOHE/1/

I hope you feel like changing it.

Regards,
Marco.

@clarle
Copy link
Collaborator

clarle commented Sep 20, 2013

Hm, it feels a little bit weird for me, since if you have the validator, I'd imagine you'd expect that the value there would either pass the validator or be undefined.

I see what you mean in your case, though, and I think it is something that probably should be fixed. What if we set it so that if you didn't specify a value property in ATTRS, then when you reset it, it would go back to being undefined?

@ItsAsbreuk
Copy link
Author

Yeah that will probably be the best solution.

I agree it sound weird. But as my usercase showed, i need a way to go to initial (or undefined) values again.
The Model's attribute-values username and password are being refilled (synced) in a login-form, and I want these to be clean, not the last 'valid' value.

Thanks for picking it up.

Marco.

@clarle
Copy link
Collaborator

clarle commented Sep 20, 2013

In that case, I think you would just remove the value property from username and password, and that way, when you do model.reset(), they would go back to the undefined state.

I'll work on a patch for that in a bit, cheers!

@ItsAsbreuk
Copy link
Author

@clarle

To show a use case: http://gallerydocs.itsasbreuk.nl/gallery-itsaviewmodelpanel/form-without-buttons.html
If you enter wrong login, the form (which represents the Model-attributes) is reset. And should be empty..

Marco.

@clarle clarle closed this as completed Nov 10, 2013
@clarle clarle reopened this Nov 10, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants