caolan / forms

An easy way to create, parse and validate forms in node.js
MIT License
1.01k stars 167 forks source link

Add/edit/delete questions in existing form #184

Closed SheaBelsky closed 8 years ago

SheaBelsky commented 8 years ago

I was wondering if there was any functionality for adding a question to an existing form. If a standard form is pre-defined, and a user wants to go in and add, edit, or delete elements from this form, would they be able to do that?

If they can't, is there a way to read the contents of an existing form, loop through them and do the creation/editing/deletion manually, then create a new form to replace the old one?

Thanks.

ljharb commented 8 years ago

This lib is mostly intended for static forms - or are you asking about composing one static form into a new one with changes?

SheaBelsky commented 8 years ago

That would be ideal, overwriting an existing form with new (or changed) elements.

ljharb commented 8 years ago

A form should have the .fields attribute, which can be used with Object.assign to make a new form - ie, forms.create(Object.assign({}, form.fields, overrides)) - does that suffice?

SheaBelsky commented 8 years ago

What's the first argument of Object.assign? Would that be newer form elements to add to the new form, or something else? form.fields is the forms of the pre-existing form, correct? How would you append newer form elements to that? (Or is that what the first argument is?)

ljharb commented 8 years ago

@SHBelsky it's an empty object, so that you avoid mutating form.fields. The third argument is the new elements to add to the form, and/or the field names you wish to override.

SheaBelsky commented 8 years ago

And overrides is just an object of newly instantiated fields? Would you construct it normally (e.g. overrides = {username: fields.string({ required: true })}?

ljharb commented 8 years ago

yep, exactly like that

SheaBelsky commented 8 years ago

Sounds good, thank you so much!