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
Delete empty objects before validate #123
Comments
@radekmie the way I see it, either or better yet both of following solutions would help a lot a) expose an onBeforeValidate option that takes in the model and returns a new model |
a) It surely won't be enough very soon: Validation is one thing, another one is the form submit flow. Right now, both As I was reviewing Solution? I think, that This will simplify your suggested solution, @serkandurusoy - we could have a new prop (I can't think any possible name right now) for a hook of this method. Example implementation in getModel (mode) {
let model = ...; // As currently implemented
if (this.props.X) {
model = this.props.X(model, mode);
}
return model;
} What do you think? |
@radekmie onValidate actually can be used in place of onBeforeSubmit, right? My first inclination was to actually alter the modifier form example, too! But it proved to be too verbose for something so seemingly trivial. and yes a preexisting prop - I can't think of a name either :/ - as you suggested can definitely help mitigate this issue. |
@serkandurusoy no, because As soon as we come with a prop name, I'll implement it. |
How about // Model transform.
// Function transforming one model into another. It's used in few
// situations (modes) described below.
modelTransform={(model, mode) => {
// This model will be passed to the fields.
if (mode === 'form') {/* ... */}
// This model will be submitted.
if (mode === 'submit') {/* ... */}
// This model will be validated.
if (mode === 'validate') {/* ... */}
// Returning undefined is the same as returning unaltered model.
}} The default ( <AutoForm
// Other props...
modelTransform={(model, mode) => {
if (mode === 'submit' || mode === 'validate') {
return removeEmptyObjects(model);
}
}}
/> |
Ah, I think this is a little too verbose for small problems, but it also On Wed, Nov 2, 2016 at 7:06 PM, Radosław Miernik notifications@github.com
|
Hello, I think the modelTransform approach is pretty nice. With the approach posted above this would be easily solvable. Best regards and thanks for your efforts! |
I was quite busy recently (but documentation is complete!). It's on my roadmap - it will be ready later this week. |
Check it out in |
Hello there,
thanks for this nice library ;)
I'm currently facing a bug with conditional form inputs:
So i basically select a courseType and then display a nested form in regards to the selection using the DisplayIf https://github.com/vazco/uniforms#example-displayif approach.
Problem
The Problem I now face is, that after selecting e.g. 'learningPath' an empty 'learningPath' object will be generated inside the model. When now changing the selection to 'songCourse' - filling out the form and trying to submit the form I will get an error inside the learningPath.xxx fields as simpleschema tries to validate the empty 'learningPath' object, but is of course missing all the props inside it.
Workaround
I currently use a kind of ugly workaround where I basically delete the empty object out of the model inside onValidate and than rerun the validation.
Proposed solution
Eventually one could iterate over the model and remove empty objects before validation?
Regards,
Lukas
The text was updated successfully, but these errors were encountered: