caolan / forms

An easy way to create, parse and validate forms in node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add/edit/delete questions in existing form

SheaBelsky opened this issue · comments

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.

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

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

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?

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?)

@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.

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

yep, exactly like that

Sounds good, thank you so much!