-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add more extensive examples to the docs, like how to deal with arrays and objects #48
Comments
Of course! You could create a custom element called <template name="elements__multiString">
<div class="reactive-element">
{{#each value}}
<input type="text" value={{.}}>
{{/each}}
</div>
</template> The above is a bit naive in assuming that you'll be passing initial data with an array of strings for the field you want to use (like email). So to get 2 inputs, you'd pass in ['',''] if there wasn't an existing value. We're delegating the ReactiveForms.createElement({
template: 'elements__multiString',
validationEvent: 'keyup',
validationValue: function (el, clean, template) {
var values = $(el).find('input').map(function () {
return $(this).val();
});
return values; // An array with all your input values
}
}) From there, SimpleSchema validation should work on the array of strings automatically. Hope this helps, definitely share any cool things you come up with! |
Thanks for the quick reply! I will meditate on your solution and see if I can get it to work. Your library, imho, has good smell. Much better than autoForm, which seems to have gone down a certain rabbit hole. I've got some Angular experience, and I've been kind of shocked that there isn't a full featured form library for Meteor. Yours is the best I've seen so far, and I would love to see you continue to add to it. Have you looked at the Angular form docs? You're not that far away from what they have, and your approach is similar. If you duplicated much of the functionality they have there, but in the much nicer and more sane Meteor way of doing things, you would be a hero. Is there an object that the form is bound to, such that, if I modify that object then the content of the form's fields changes? I know, for updates, you can use a data object to populate the form. Does that object get bound to the form then? What about an insert form with no data object? Is there an object somewhere that represents the form data? |
Thank you for the kind words. I definitely intend to do my best with this package--I'm using it myself in all kinds of ways, and constantly thinking about how to improve it as well. The form-level data object is privately scoped, and the only way to change it manually is:
The https://github.com/meteortemplates/forms/blob/master/lib/module.coffee#L207 For version |
Having access to the form's backing object, and having that object be reactively tied to the form is incredibly powerful. For example, in an Angular template, you can loop over an array property and build up some form fields. In your code, if you simply add another element to the array, boom, you get another set of fields. Makes dealing with nested properties really easy. Perhaps a block template that allows looping over a property could be useful? I know I mention Angular a lot, but I'm not really a fan of the library. I just think that, when it came to forms, they got the concepts right and that it should be used as a reference. Trying to do something in Angular still makes my head want to explode. |
Haha that's why I never really got into it. Seemed like there were too many ways to do the same thing and a lot of boilerplate and jargon for everything. In // Add helpers for the custom state
Template['inputElement'].helpers({
numberOfFields: function () {
var currentFieldCount = Template.instance().numberOfFields.get();
var times = [];
_.times(currentFieldCount, function (n) {
times.push(n);
});
return times;
},
getValueFor: function (n, values) {
if (n && values && values[n]) {
return values[n];
}
}
});
// Add event to change custom state
Tempate['inputElement'].events({
'click .add-field': function (event, template) {
var currentFieldCount = Template.instance().numberOfFields.get();
Template.instance().numberOfFields.set(currentFieldCount++);
}
});
// Add ReactiveVar here using the `created` callback
ReactiveForms.createElement({
template: 'inputElement',
validationEvent: 'keyup',
validationValue: function (el, clean, template) {
var values = $(el).find('input').map(function () {
return $(this).val();
});
return values; // An array with all your input values
},
created: function () {
this.numberOfFields = new ReactiveVar(1); // Default to one field
}
}); Then in the element's template: <template name="inputElement">
<div class="reactive-element">
{{#each numberOfFields}}
<input class="reactive-element" value={{getValueFor . ../value}}>
{{/each}}
</div>
<button class="add-field">Add another field</button>
</template> The Try to avoid thinking of One of the cool things about this package is that you can create pretty much any crazy element you want and the API to do it just uses regular Blaze. I think the key to maintaining that feel is careful decoupling like what exists currently. If the form block's data context was named, and that named context was reactive and writable from anywhere, it might simplify things in one case but complicate them in another. I don't really know at this point, but it's worth visiting for |
Wow! Thanks for the long reply. I'm new to Meteor and Blaze, and am still learning the right way of doing things. Hopefully in time it'll become more natural to me. I'm gonna spend some time really trying to get what you wrote. I like what you said about not focusing on 2-way binding. And I don't really think the 2-way thing is strictly necessary if there's another, elegant way to get the job done. What I'm really looking for in a form library is what I guess I might call FOM, or Form-to-Object Mapping. Sort of like ORM, but instead of the impedance mismatch existing between objects and the RDB, it's between the form and the structured data. Features that make it easy to map between form fields and arrays and sub-objects would be nice. That's really all I'm looking for, but it may just be staring me in the face. |
Right--this package does that using the The data in With arrays, objects, and integers, it's in your hands to use |
Ok, I will chew on that. Thanks, Jon. |
You may need to put |
I want have validation String in each input from array (see my before figure). So I made this: <template name="inputElement">
<div class="reactive-element">
{{#each numberOfFields}}
{{> basicInput field="answers.ans"}}
{{/each}}
</div>
<button class="add-field">Add another field</button>
</template> The field answers.$.ans is required but the validation doesn't works. |
If you can make an example repository for this I can help solve it. There have been a few other requests around validating a variable list of fields, so I may be able to write more elegant support for this into the package. |
When I wrote the example above, the idea was to put the field on the outer element, retrieve the values from the inputs inside and compile them into an array, and return those for validation. But the way you're doing it here involves a different concept. |
Hello jonjamz here the repository: https://github.com/johngonzalez/examples-forms |
+1 Also looking for docs on Arrays. @jonjamz Really enjoying this package. Any change we can see the example in plain JS instead of coffeescript? Thanks! |
Hi I'm also looking for more documentation or support with regards to arrays of objects Also I found a bug wherein the schema isn't retrieved if the field you specify for the element component is a field within an object element of an array field. |
The issue with @johngonzalez example from what I can tell after a quick look is that he is generating many components with the same As for the rest I'll have to answer later... |
Can you talk about what functionality exists in the library to support properties that are arrays or objects? I'm just working on a simple contact form as a learning exercise, but it features the ability to add multiple emails or phone numbers. Here is the schema:
Any suggestions as to how I would use Forms to support this?
The text was updated successfully, but these errors were encountered: