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

Unchecked checkbox behavior question #504

Closed
SachaG opened this Issue Dec 8, 2014 · 9 comments

Comments

Projects
None yet
4 participants
@SachaG
Copy link
Contributor

SachaG commented Dec 8, 2014

Let's say I have a "sticky" Boolean represented by a checkbox. Checking the checkbox should mark it as true, but what about leaving the checkbox unchecked? Should it set the property to false, or else not set it at all?

I'm asking because in my current implementation, leaving the checkbox unchecked when inserting a document leaves the property unset, but doing the same when editing a document marks it as false. Although I'm not sure if this is because of Autoform or something else in my code…

@aldeed

This comment has been minimized.

Copy link
Owner

aldeed commented Dec 8, 2014

It's one of those tricky issues. Since normal check box behavior is "include the value with the submission if checked", that doesn't really help us when using it to represent a boolean value. My preference is for it always to be false when unchecked and true when checked. Seems what one would expect.

So this is basically an implementation detail of the "boolean-checkbox" input type. As you can see, it's returning the checked state, which I thought would always be either true or false, but maybe it just needs to be changed to return !!this.is(":checked");.

@SachaG

This comment has been minimized.

Copy link
Contributor Author

SachaG commented Dec 8, 2014

Makes sense. I ended up setting defaultValue: false to avoid the problem altogether.

@aadamsx

This comment has been minimized.

Copy link

aadamsx commented Dec 8, 2014

@SachaG I ran into the same thing. How did setting the defaultValue: false for the checkbox avoid the problem altogether?

@aldeed aldeed closed this in 26fb1f4 Dec 8, 2014

@SachaG

This comment has been minimized.

Copy link
Contributor Author

SachaG commented Dec 9, 2014

@aadamsx because now I can rely on the property always defaulting to false unless it's set to true, so I can sort on it (which was my original problem).

@aadamsx

This comment has been minimized.

Copy link

aadamsx commented Dec 9, 2014

@SachaG, nice, thanks for the feedback. @aldeed, I noticed you changed behavior here in 26fb1f4, will checkboxes default to false now?

@aldeed

This comment has been minimized.

Copy link
Owner

aldeed commented Dec 9, 2014

Yes

aramk added a commit to aramk/meteor-autoform that referenced this issue Dec 13, 2014

Merge commit '44769ca5eafc26ac45799fa4cdaf50956b8f00fd' into develop
* commit '44769ca5eafc26ac45799fa4cdaf50956b8f00fd': (21 commits)
  bump version
  clarify security (closes aldeed#520)
  autosave contenteditable inputs (closes aldeed#494)
  Bugfix: if type of keys is Number
  fixes to selectOptions merge
  Feature suggest: autoform.options from hashtable
  bump version
  fix aldeed#474 bootstrap-radios values
  array field style fix
  fix aldeed#504
  suggest values for afFieldValueContains
  Fix multi-select issue where we were determining selection status based on 'opt' value (which is at the group level) instead of the actual option ('subOpt'). Multi-select 'selected' now works.
  fixes issue aldeed#497
  allow providing an onSuccess result from onSubmit (fixes aldeed#479)
  set this.docId for insert form onSuccess, too
  workaround for resetting update forms; fixes to hot code push migration
  clarify before hooks a bit (aldeed#477)
  list add on packages available from others
  If value invalid return unmodified value, not null
  Check if view isn't destroyed
  ...
@aramk

This comment has been minimized.

Copy link
Contributor

aramk commented Jan 11, 2015

@aldeed I'm getting an issue where two seemingly identical collections have a boolean field with defaultValue: true but only one of the forms has the checkbox in the checked state when inserting.

The collections are here. Products and Colors have the isBright boolean. It only works for the colorItem template but not productItem.

@aldeed

This comment has been minimized.

Copy link
Owner

aldeed commented Jan 11, 2015

@aramk, the Products form is calling AutoForm.resetForm on rendered, which is clearing it. I'm not entirely sure why the browser clears the checkbox when you reset the form, even though it has the checked attribute. I found the prescribed behavior here:

The reset algorithm for input elements is to set the dirty value flag and dirty checkedness flag back to false, set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise, set the checkedness of the element to true if the element has a checked content attribute and false if it does not, empty the list of selected files, and then invoke the value sanitization algorithm, if the type attribute's current state defines one.

This is a bit difficult to parse, but it seems to me that it should be retaining the checked state on reset. It's possible this is an issue that could be fixed in Blaze, but you'd have to recreate the issue without autoform involved. Autoform is simply calling form.reset(), so I'm hesitant to do any specific overrides of that behavior.

All of that being said, I don't think there's any reason why you should have to reset the form on render.

@aramk

This comment has been minimized.

Copy link
Contributor

aramk commented Jan 11, 2015

Hi @aldeed, thanks for that clear and detailed explanation, I think that's in indeed the case. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.