-
Notifications
You must be signed in to change notification settings - Fork 19
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
Added the ability to reset validation state. #75
base: master
Are you sure you want to change the base?
Conversation
hey @kylefarris, just thinking out loud here, and off the cuff. shouldn't |
@cdaringe I would personally prefer that but it could cause some people's apps to act in unexpected ways if they, for some reason, expect it to work as it currently does. With this PR, they can easily add in the feature by adding and setting the If they want to reset an individual field back to original state and clear validation state, they can run In other words, if we just made it so that My 2 cents. |
gosh, i'm sorry, i'm still having a hard time understanding all of what this PR is attempting to acheive. apologies for the inconvenience, but can you baby step me through it? I'm pretty sure i'm just a little slow today 🐢 |
Hmm... No problem man. So, let's assume this single field form (plus submit button)... var FormView = require('ampersand-form-view');
var InputView = require('ampersand-input-view');
var Users = require('./collections/users');
var users_collection = new Users();
var name_field = new InputView({
label: 'Name',
name: 'name',
required: true,
tests: [
function(val) {
if (val.length <= 1) return "The name must be more than 1 character!"
}
]
});
var form = new FormView({
el: this.queryByHook('place-on-page'),
submitCallback: (form_data) => {
users_collection.create({name: form_data.name}, {wait:true, success: (m,r,o) => {
form.reset();
});
},
fields: () => {
return [
name_field,
new InputView({
name: 'submit',
label: 'Submit',
type: 'submit'
})
];
}
}); Now, if someone were to fill out the name field with, say, "K" and then press the Submit button, it would prevent the form from being submitted and place the Okay, so now the user presses the submit button again after fixing their mistake and it posts the field to the back-end, or whatever, and it clears the form using form.reset(); And... here is the problem... the That's where the PR comes into play. Now we have several options on how to handle the scenario where we want our form to be in the exact state it started in... Option 1: Completely clear the form and validation classes by passing form.clear(true); Option 2: Do a full reset of the form by passing form.reset(true); Option 3: name_field.clear(true);
// ... or ...
name_field.reset(true); Option 4: Have the var name_field = new InputView({
label: 'Name',
name: 'name',
required: true,
clearValidationOnReset: true, // <-- THIS
tests: [
function(val) {
if (val.length <= 1) return "The name must be more than 1 character!"
}
]
});
var form = new FormView({
fields: [name_field]
});
form.reset(); // <-- no need for TRUE to be passed here Option 5: Just reset the validation (don't clear or reset the fields) name_field.resetValidation() I hope that makes it a bit clearer. |
@kylefarris, amazing writeup. thanks so much! with this new understanding, i'm on board. i'm going to speak through my thought process below. i'm sorry to keep dragging out the review, but:
|
No problem at all--I totally welcome (and appreciate) a thoughtful discussion on any PR. It's what makes open source software so much better than anything else. That said, on your first point, I do think Contrived User Update Form: var name_field = new InputView({
label: 'Name',
name: 'name',
value: 'Bob'
tests: [
function(val) {
if (val.length <= 1) return "The name must be more than 1 character!"
}
]
});
var form = new FormView({
el: this.query('#update_user_form'),
fields: [name_field]
});
It might be a somewhat rare scenario but it would be there should a developer need such functionality. As for point 2, I completely agree with you. I tend to prefer that style of parameterization in javascript as well. It does, however, make merging with defaults slightly more of a pain, maybe. Maybe not... there's several ways to go about it. Which do you prefer? 1. Override directly in default object function foo(params) {
params = params || {};
var options = {
validate: params.validate || true,
foo: (params.foo && typeof params.foo === 'object' && Object.keys(params.foo).length > 0) ? params.foo : {
bar: 'baz'
})
};
} 2. Merge parameter object with a function foo(params) {
var options = {
validate: true,
foo: {
bar: 'baz'
}
};
if (params && typeof params === 'object' && Object.keys(params).length > 0)
options = Object.assign({}, options, params);
} My thoughts on it... Option 1 would be more direct, faster, would be more-controllable, and would work on all browsers. It might get a little hairy for multi-level deep parameter objects (rare), though. Option 2 might scale a little better (from a "code-cleanliness" standpoint) for items with a large number of options but would also be a bit slower and wouldn't work with browsers that don't support |
|
The functionality was added as an option to the module so that it is the default if the user chooses for it to be. It can also be ran with a new method
resetValidation
or via a parameter to theclear
andreset
methods.This is useful when the same form is being reused without having to refresh the page or re-render the entire view.
The original functionality is retained as to not break backward-compatibility. This feature, in all cases, is purely optional. I updated the changelog portion of the README to reflect a minor update as their should be no breaking changes but it is not simply a bug patch.