I have simple schema looking like:
Schema = new SimpleSchema
name:
label: "Order title"
type: String
optional: true
offers:
type: Array
optional: true
'offers.$':
type: Object
'offers.$.name':
type: String
min: 3
'offers.$.selected':
type: Boolean
defaultValue: false
Firs I'm inserting new entry into Mongo collection. The, when I already have it, I want to add one more offer into it.
I have tried this code to add new offer into offers array, but it doesn't sent any data into method
+autoForm id="addOffer" schema=Schema type="method" meteormethod="addOffer" doc=data
+afQuickField name='offers.$'
button(type="submit") Add
It seems, that I can only edit all of them at once. E.g. using:
+autoForm id="updateOffer" collection="Order" type="update" doc=data
+afQuickField name='offers'
Should I move offers into separate collection if I want to use autoForm to adding new one without seeing already added offers?
It seems that aldeed just solved the problem in new AutoForm (5.0) release. Now we have update-pushArray form type.
More info here: https://github.com/aldeed/meteor-autoform#update-pusharray
Using arrayTracker.addOneToField should do the work. Take a look at this event handler here : https://github.com/aldeed/meteor-autoform/blob/master/autoform-events.js#L474
Related
I am working on a GraphQL server setup where it can parse Types passed into it from strings, and I am looking for a solution to convert a string to an appropriate object. For example, if the string here is passed in:
type User { id: String, name: String }
My function would return the equivelant of running this code:
new graphql.GraphQLObjectType({
name: 'User',
fields: {
id: { type: graphql.GraphQLString },
name: { type: graphql.GraphQLString },
}
});
The key here is to be agnostic, so I could also pass in say, interfaces, and other shorthand and have it return the appropriate object. I have gotten as far as achieving the Abstract Syntax Tree from the graphql/language module bu using graphql_language.parse(str) function, but I'm unsure where to go from here.
The reference GraphQL-JS implementation on GitHub already has a function, buildASTSchema, that takes a parsed type schema and creates a set of JavaScript objects. So the best way to see how to do it would be to consult that source code on GitHub: https://github.com/graphql/graphql-js/blob/master/src/utilities/buildASTSchema.js
Alternatively, perhaps your tool can be built using that function. Since that repository is maintained by the core GraphQL team, you can be pretty confident that it will be up to date with new additions to the spec.
Edit from comment: If what you're trying to do is generate an executable GraphQL schema/server from the type language string, then you can use the generateSchema function from the graphql-tools package, as documented here: http://docs.apollostack.com/apollo-server/generate-schema.html
I'm considering setting up some proof-of-concept tool that could grab metadata from a Symfony2 FormType instance in order to dump a validation schema as JSON, something like the following:
[
{
name: 'someFieldName',
value: '',
email: true
},
{
name: 'yetAnotherFieldName',
value: 'I have a default value',
required: true
}
]
The aim obviously is to use it in front-end JS code (let's say React), to be able to set up the same validation constraints, as much as possible (required and the likes).
However, Symfony is well-architectured and its Form component knows nothing about validation. Considering only the nominal case of a simple form to begin with, how would one go about doing it? How to map back the form to the validatable objects/entities it references?
Using the symfony validator you can get the metadata for a given class:
$this->get('validator')->getMetadataFor(Foo::class);
It returns a ClassMetadata instance. If the passed value is an entity, you will have the members and properties properties returned which then contain a constraints property with the classes being used.
Final step is to serialize that in JSON.
I have schemas set up so that I can have an array of complex input sets. Something like:
address = {
street:{
type: String
},
city: {
type: String
},
active_address: {
type: Boolean,
optional: true
},
...
}
people: {
name:{
type: String
},
address:{
type: [address],
optional: true,
defaultValue: []
}
}
This way adding an address is optional, but if you add an address all of the address fields are required.
This worked in (I believe it was) version 4.2.2. This still works on insert type autoforms, but not on update type autoforms. Doing an update, none of the fields will submit unless all required fields in the nested schema are also valid.
For reference, I'm creating the form as such:
{{#autoForm collection="people" id=formId type="update" doc=getDocument autosave=true template="autoupdate"}}
{{> afQuickField name='name' template="autoupdate" placeholder="schemaLabel"}}
{{> afQuickField name='address' template="autoupdate"}}
{{/autoForm}}
My templates (autoupdate) I copy-pasted the entirety of bootstrap3 autoform templates and rearranged some of the html to fit my needs. I updated these to the best of my ability according to the 5.0.0 changelog when I updated. It could possibly be in there if someone can think of an attribute in the templates that would cause inconsistent behavior between insert and update that changed in 5.0.0.
More information
I just tried recreating all of my form templates using the bootstrap3 templates from 5.0.2. Still the same behavior.
+
I have a Boolean (checkbox) input in the address schema. Looking in a doc, the address array is populated with [0 : {active_address: false}]
active_address: {
type: Boolean,
optional: true
}
Not sure if that helps...
+
As per #mark's suggestion, I added defaultValue:[]. It fixed the issue... sort of. There are no "open" nested schemas in the update form now, and other values can be changed. If you "add" a nested schema to the form with the add button, that entire form becomes required even if you don't insert any value in any field. This happens regardless of the Boolean type input.
I can nail down the Boolean type input in the nested schema causes that entire nested schema to become necessary to do the insert. Removing the Boolean input caused it to be insertable again. So there's a new problem in the same vein.
This new issue can be found here
I think the best solution is to add a defaultValue: [] to the address field in the schema. The behavior you described in the question (not allowing the update) is actually intended -- read on to see why.
The thing is, this behavior only exists if an array form element has already been added to the form. What I mean is, if you click the minus sign that removes the street, city, etc. inputs from the form, the update succeeds because AutoForm doesn't misinterpret the unchecked checkbox as the user explicitly unchecking the box (and therefore setting the value to false). Setting the defaultValue to an empty array lets AutoForm know to not present the address form unless the user has explicitly clicked the plus sign (i.e, they have an address they want to enter), in which case the behavior of making the street, city, etc. fields required is what you want.
Note that this means you'll have to update the existing documents in your collection that are missing the address field and set it to an empty array. Something like this in the mongo shell:
db.people.update({ "address": { $exists: false } }, { $set: { "address": [] } }, { multi: true })
You'll probably want to make sure that the query is correct by running a find on the selector first.
Edit
If the behavior you want is to show the sub-form without making it required, you can work around the checkbox issue by using the formToDoc hook and filtering out all address objects that only have the active_address field set to false (the field that AutoForm mistakenly adds for us).
AutoForm.addHooks('yourFormId', {
formToDoc: function (doc) {
doc.address = _.reject(doc.address, function (a) {
return !a.street && !a.city && !a.active_address;
});
return doc;
}
});
The formToDoc hook is called every time the form is validated, so you can use it to modify the doc to make it so that AutoForm is never even aware that there is an address sub-field, unless a property of it has been set. Note that if you're using this solution you won't have to add the defaultValue: [] as stated above.
I have a property called Copies which is defined on the server that represents the default number of copies allowed. And I can update this value and it will update an input field on my UI.
however, I would like to be able to reset the Copies property to the original value if the user resets this field on the UI.
My idea was to define a custom property on my kendo datasource model called originalValue that references the Copies property. but this just seems to override the Copies property if I do something like this.
schema: {
data: 'd',
total: function (data) {
return data.d.length;
},
model: {
originalCopies: "Copies"
}
}
how can I go about creating a custom property like this which is basically a immutable clone of my Copies property?
You can try to do it on the server side, just create a separate property "OriginalCopies" and set it to Copies. Once passed to the client side , it will lose its immutability.
Something similar could be done on the client side as well. JSON.stringify your Copies and set
OriginalCopies to the JSON.parse value of the stringified variable as:
var copies = JSON.stringify(data.Copies);
data.OriginalCopies = JSON.parse(copies);
I have a store + model which is connected to a 3rd party plugin (Ext.ux.TouchGridPanel). The plugin calls the store's sort() method properly with the relevant mapping. Everything is working fine, and the store sorts itself. However, I would prefer to add customer sorting to the store. I have tried adding a sortType field into my model:
Ext.regModel("Transactions", {
fields: [
{
name: 'unit',
type: 'string',
sortType: function(value) {
console.log('PRINT GDAMNIT');
return 0;
}
},
...
]
});
This, however, is not working, and the sortType is not getting called.
TLDR: How to make custom sorting work for stores?
Your store will need a sorter added that will sort on that field before it will call the sortType function.
var store = new Ext.data.Store({
model: 'Transactions',
sorters: [
{
property: 'unit',
direction: 'DESC'
}
]}
);
Sort type converts the value of a field into another value to ensure proper ordering. If you aren't sorting on that field than there is no reason to call that function. You could add the sortDir to the field which would sort the field into ascending/descending order based on the type of the field alone.
A workaround might be to (I know this sounds inefficient but bear with me) add an extra field to your model instances (lets call it sortField) and use that for your sorting function. You can then loop through your model instances in your store applying your custom sorting algorithm and assign a sort value of 0,1,2,3,4,5 etc.. to sortField. Then in your store, you can add 'sorters: 'sortField'... Hope this helps a bit, I'm going through something similar at the current moment.
The custom SortType in Sencha Touch 2 works accordingly, as per http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes:
Ext.apply(Ext.data.SortTypes, {
asPerson: function(person){
// expects an object with a first and last name property
return person.lastName.toUpperCase() + person.firstName.toLowerCase();
}
});
Ext.define('Employee', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'person',
sortType: 'asPerson'
}, {
name: 'salary',
type: 'float' // sortType set to asFloat
}]
}
});
What you're attempting to do might be tricky. Calling store.sort() removes all existing sorters by default (according to Sencha Touch API documentation). To keep the existing sorters you would need to add the sorter to the MixedCollection store.sorters.
Secondly, to call the sort method with a custom sort function, you would need to pass a specific sorterFn instead of property to the Sorter (again, see the API for more details) - but this might prove tricky since the sort call is initiated from the plugin.
Not sure if this helps to solve your problem, but maybe it assists you to look at the right direction.