Closed aman-gautam closed 6 years ago
If you try to access the properties they are still going to equal undefined, model.name and model.not_added will both equal the same thing n your object above.
Is there a reason you want a representation in the model without value, perhaps I can help more specifically? There is a library from the creator of djv called djvi which will generate an object from schema but it will only add a property if it is required.
Thanks for the response @Anthropic.
We have a large schema
object and many form
objects that use a subset of the schema
. When the data is passed on to an external validation API, we are getting back ALL the fields
from the large schema
. Even if some fields
of the schema
are not even there in the current form
.
We need to find a way to show only the invalid fields
that are present in the current form
.
One way can be to have all the fields in the form
there in the model
, that will make it a simple SET operation for us.
djvi works with schema
(as per the README), and we need to work with the form
.
I can try figuring out changes needed in angular-schema-form to accomplish this. Some hints will be really useful for me right now.
I would consider doing something like that outside of ASF, you can parse your form object to create a list of valid keys from the form definition and then process that against the resulting object prior to submission to the back end to remove unwanted items.
My opinion would be that as a schema should represent the model, even a subset form definition should validate against the schema as is and include the minimum requirements for the schema to validate. That's not how ASF was built originally, but that would be how I would always advise people to use it now. In future there will be new additions to JSON Schema and ASF to accommodate if/then/else structures to enforce required properties that are only required under conditions, but for now if something is set as required then it really should be required for all forms even when they are a subset of the schema fields.
Sorry for not updating it earlier.
I ended up writing a small recursive function to find all the fields in the form
function getCurrentFormKeys(data, result){
if(Array.isArray(data)){
data.forEach(function(d){
getCurrentFormKeys(d, result);
})
}else if (typeof data === 'string'){
result.push(data);
}else if(data){
if(data.items){
getCurrentFormKeys(data.items, result);
}else if(data.key){
if(Array.isArray(data.key)){
getCurrentFormKeys(data.key, result);
}else{
result.push(data.key);
}
}
}
}
// Make an array to hold the list of the fields
let result = [];
getCurrentFormKeys(scope.form, result);
// result now has a list of all fields in the form
Thanks for the pointers @Anthropic
Expected behaviour
The model object should have the keys populated even if there is no value (undefined).
example instead of having a default model as
{}
we should have something like{name: undefined, email: undefined, comment: undefined}
Actual behaviour
The model is a blank object by default. If there is no value in the form, the key won't appear in the
model
.Demo
In the examples the model is a blank object till we fill up either name, email or comment.
Is there a way to prepopulate the model with the field names?
Thank you! :)