Apps script: use a form as a template for other forms creation - forms

I have a form with a set of questions, and I want to use it for other forms creation. In other words, I want to iterate through this template form items, choose those that I need and append them to another, newly created form.
I had no problem with getting the items and choosing those that I need; however, I can't find the way to append these items to another form - all of the Form.addSomethingItem() methods don't take arguments (docs for example). Is it really impossible, or do I miss something foolishly simple?

There are two ways to achieve this:
Make a copy of the form using DriveApp and remove/modify items in-place on the new form copy.
Iterate over existing items, check it's type and add them to the new form. For example, The following gets Item1's enum and converts it to a camelCase string and executes add{Type}Item method, where Type is MultipleChoice in case of MULTIPLE_CHOICE Enum Item type.
const newForm2Item0 = Form2[
`add${String(Form1.getItems()[0].getType())
.toLowerCase()
.replace(/(^|_)[A-Z]/gi, match =>
match.toUpperCase().replace('_', '')
)}Item`
]();//equivalent to Form2.addMultipleChoiceItem()
You would then add choices and other configurations as needed in a similar manner.

Form methods like addTextItem() don't take arguments, rather they return Items classes (e.g. TextItem). Those Item classes have their own methods for assigning properties to a question on a form.
Examples
here's an example of adding a text item with a specific title
form.addTextItem().setTitle('I am a text question title');
here's an example of adding a multiple choice item to a form with some choices
var item = form.addMultipleChoiceItem();
var choices = ['choice 1', 'choice 2', 'choice 3'];
choices.forEach(function(choice) {
item.createChoice(choice);
})

Related

SAPUI5 No dynamic way to get form data without data binding. And no Form submit event.

I have a simple form that's in a dialog fragment used to submitting two fields for log-in auth.
For simplicity I was hoping to not have to use data binding, but rather use some method to gather all data inside my sap.ui.layout.form.SimpleForm.
I added a name property to each input element which says in the docs it is " Defines the name of the control for the purposes of form submission."
https://openui5.hana.ondemand.com/#/api/sap.m.InputBase/controlProperties#name
However hard as I try to find there doesn't seem to be any getFormData methods.
All SO questions and guides either use data binding to a model, or hard-code references to the individual input controls with .getValue() methods.
And looking further into the form API, there doesn't seem to be a Submit event either.
Given an arbitrary form, what would be the best way to gather all submission values without hard-coded references or data-binding?
Would a method that walks though all the children elements of a form looking for all submission values work? I think it might, but there are more submission input types then just the input component.
You can get the value of the fields by directly using;
var oField = sap.ui.getCore().byId('IdOfTheFieldAtTheDialog');
var sValue = oField.getValue();
But it's always better and convenient to use data binding which keep things neat.
And If I assume that you have the id of parent form container, you can iterate over the items and get the sap.m.Input elements in it without knowing the IDs of the individual inputs, and you may check the name property of the fields if you want. Check this snippet;
https://jsfiddle.net/hdereli/9e92osfk/3/

redux-form Wizard form with linked fields

I am building a multi-step application form with React. Having first built it with pure internal state I am now in the process of refactoring to Redux using redux-form.
Having used the example here as a basis: http://redux-form.com/5.2.5/#/examples/wizard?_k=oftw7a we have come a good way.
However the problem appears when i have two forms which are supposed to have the same value. During one of the pages i have a name field, that is supposed to be duplicated on the name field of the next page. The opposite should happen if you go back from the last page. Any tips to how this could be achieved?
Using the wizard, you are basically working with the exact same form that's split into multiple pieces. Ultimately it's the same form, because redux-form tracks them by name. It is how the library identifies the pieces of the same form - using the name.
form: 'wizard',
Here you can see that the exact same instance of the form will be shared throughout the pieces. fields work in a similar manner. Each field is defined as part of a form.
As long as you use the same field constants inside the fields object that you pass into the reduxForm function and as long as the value for form is the same, so that they use the same underlying form object, it should work for you just fine.
On one page you should pass in
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'someOtherFieldThatShouldOnlyBeHere',
},
...
And then on the other page:
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'thirdFieldHere',
},
...
Also, make sure you keep destroyOnUnmount equal to false if you want to navigate back-and-forth.
Hope that helps.

How can I get the Zend_Form object via the Zend_Form_Element child

I've built a Zend_Form_Decorator_Input class which extends Zend_Form_Decorator_Abstract, so that I could customize my form inputs -- works great. I ran into a problem in the decorate class, in trying to get the form name of the element, so as to built a unique id for each field (in case there are multiple forms with identical field names).
There is no method like this: Zend_Form_Element::getForm(); It seems Zend_Form_Decorator_Abstract doesn't have this ability either. Any ideas?
I don't think changing the id from the decorator is the right approach. At the time the decorator is called the element already has been rendered. Thus changing the id would have no effect to the source code. Additionally, as you already have pointed out, the relation between a form and its elements is unidirectional, i.e. (to my best knowledge) there is no direct way to access the form from the element.
So far the bad news.
The good news is, that there actually is a pretty easy solution to your problem: The Zend_Form option elementsBelongTo. It prevents that the same ID is assigned to two form elements that have the same name but belong to different forms:
$form1 = new Zend_Form(array('elementsBelongTo' => 'form1'));
$form1->addElement('Text', 'text1');
$form2 = new Zend_Form(array('elementsBelongTo' => 'form2'));
$form2->addElement('Text', 'text1');
Although both forms have a text field named 'text1', they have different ids: 'form1-text1' and 'form2-text1'. However, there is a major drawback to this: This also changes the name elements in such a way that they are in the format formname[elementname]. Therefore $this->getRequest()->getParam('formname') will return an associative array containing the form elements.

Zend Form changing element type in controller

I have my Zend_Form, and sometimes, one of the fields should be hidden, and not seen by the user. Is there a way when I call the form in my controller, I could change one of the fields to be hidden?
Thanks
Kousha
You can remove the element using:
$form->removeElement('my-element-name');
in the controller.
You could also create two forms, one overriding the other, in which the child calls $this->remove('my-element-name').
Or, you could make the form constructor accept a boolean $flag that determines whether to add the field to the form.
So, as you can see, lots of different ways to structure it.
To change that field to one of type "hidden" (i.e. <input type="hidden">) is a different thing, but I'm not sure that's what you mean/need/want.
The best solution I have for this is to add a specific class to the element when it needs to be hidden. It may not be the perfect solution, but let me explain.
First, its very difficult to switch from one element type to another in Zend Form. Your elements are actually classes. So the text is Zend_Form_Element_Text - so its not just as easy as changing the 'type' attribute.
If the element must remain on the form (so, not removing it like the answer above suggests), your only other option would be hiding it with CSS.
Try the following code when it needs to be hidden:
$element = $form->getElement('MyElement');
$newClass = trim($element->getAttrib('class') . ' hidden');
$element->setAttrib('class', $newClass);
Then, of course, create CSS for the .hidden class.
Hope this helps!

Use DataAnnotations to force a DropDownList populated from my model

Is there a way use DataAnnotations to tell Razor to render a DropDownList and populate the choices from a specific field in a specific entity?
Specifically I am capturing a "Calendar Year" property in my View and would like it to be a DropDownList. I am doing this currently by passing the years into my view via the ViewBag and then calling "DropDownListFor". I was hoping for a way to define it in my ViewModel and them simply call "EditorFor".
Thanks!
I can't find a way to do it with a DataAnnotation, but you don't need to use the ViewBag, you could pass the values in, like this:
#Html.ListBoxFor(m => m.SelectedFoos, Model.AllFoos.Select(f => new SelectListItem { Text = f.Name, Value = f.ID }))
(Note: This uses ListBox, taken from an answer here, but should be similar for DropDownList).