Using NgForm and NgControlGroup in Angular2 Forms - forms

I can code forms with one ControlGroup but I'm having trouble with multiple ControlGroups. According to the example code, NgControlGroup should be used inside <div> elements and NgForm should be used in the <form> element. So here's my markup:
<form ng-form="main" (ng-submit)="handleSubmit()">
<div ng-control-group="group1" >
<input ng-control="c1"><br /><br />
<input ng-control="c2"><br /><br />
</div>
<div ng-control-group="group2">
<input ng-control="c3"><br /><br />
<input ng-control="c4"><br /><br />
</div>
<input type="submit" value="Submit">
</form>
Here's the code for the component's constructor:
constructor() {
this.group1 = new ControlGroup({
"c1": new Control("first"),
"c2": new Control("second")});
this.group2 = new ControlGroup({
"c3": new Control("third"),
"c4": new Control("fourth")});
this.main = new ControlGroup({
"g1": this.group1, "g2": this.group2});
}
I don't get any errors when I launch the component, but the controls don't take their initial values ("first", "second", and so on). That means the HTML elements aren't being properly bound to the Controls. Any thoughts? Are there any good examples that use NgForm and NgControlGroup?

I figured out what was wrong. The NgModelForm directive should be used instead of NgModel. Also, the <input> elements should mention the proper names of the ControlGroups. Here's the result:
<form [ng-form-model]="main" (ng-submit)="handleSubmit()">
<div ng-control-group="g1" >
<input ng-control="c1"><br /><br />
<input ng-control="c2"><br /><br />
</div>
<div ng-control-group="g2">
<input ng-control="c3"><br /><br />
<input ng-control="c4"><br /><br />
</div>
<input type="submit" value="Submit">
</form>

Related

How prevent form submit in google apps script

I am importing a form written in GoogleApps Script into an iframe on a page built with Squarespace but for the life of me cannot prevent the form from submitting. I am using:
window.addEventListener( 'load', preventFormSubmit );
as suggested in GAS documentation but this does not seem to be triggering the preventFormSubmit function. Instead, when the submit button is clicked the form submits and goes to a blank google page. Also the alerts in the preventFormSubmit function (now commented out) never display which suggests that the form is never called.
I have spent days on this, cannot find an answer anywhere and can no longer see the woods for the trees. Any clues as to what I am doing wrong?
Squarespace is a website builder which enables one to embed code, in this case as an iframe.
My code:
js.html:
<script>
function preventFormSubmit() {
//alert( "prevent form submit triggered" );
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
//alert( "forms prevented from submitting: " = forms.length );
}
window.addEventListener( "ready", preventFormSubmit );
function handleFormSubmit(formObject) {
google.script.run
.withSuccessHandler( showSuccess )
.withFailureHandler( showError )
.processForm_1( formObject );
}
</script>
html:
<!DOCTYPE html >
<head>
<base target="_top" >
<?!= include('css'); ?>
<?!= include('js'); ?>
</head>
<body>
<div id="formDiv" class="card" >
<h2 id="title" >Alternative Booking form</h2>
<form id="alternative-booking-form-1" onsubmit="handleFormSubmit(this)" >
<fieldset>
<legend>About You</legend>
<p>Please tell us a bit about yourself.
</p>
<input type="text" id="firstName" name="firstName" form="alternative-booking-form-1"
placeholder="your first name" value="" required
/><br />
<input type="text" id="lastName" name="lastName" form="alternative-booking-form-1"
placeholder="your last name" value="" required
/><br />
<input type="text" id="title" name="title" form="alternative-booking-form-1"
placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>
</fieldset>
<fieldset>
<legend>Your Contact Details</legend>
<p>We will only use your contact details in case we need to contact you with regard to
this booking, unless you consent
to further communications, as offered later in this booking process.</p>
<input type="email" id="email" name="email" form="alternative-booking-form-1"
placeholder="your email address" value=""
required /><br />
<input type="tel" id="phone" name="phone" form="alternative-booking-form-1"
placeholder="phone" value="" required /><br />
</fieldset>
<fieldset>
<input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1" />
<br />
<input type="submit" id="submit" name="submit" form="alternative-booking-form-1"
class="red" value="Next →" />
<br />
<br />
</fieldset>
</form>
</div>
<div id="output" name="output" ></div>
</body>
<!DOCTYPE html >
<head>
<base target="_top" >
<?!= include('css'); ?>
<?!= include('js'); ?>
</head>
<body>
<div id="formDiv" class="card" >
<h2 id="title" >Alternative Booking form</h2>
<form id="alternative-booking-form-1" >
<fieldset>
<legend>About You</legend>
<p>Please tell us a bit about yourself.
</p>
<input type="text" id="firstName" name="firstName" form="alternative-booking-form-1"
placeholder="your first name" value="" required
/><br />
<input type="text" id="lastName" name="lastName" form="alternative-booking-form-1"
placeholder="your last name" value="" required
/><br />
<input type="text" id="title" name="title" form="alternative-booking-form-1"
placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>
</fieldset>
<fieldset>
<legend>Your Contact Details</legend>
<p>We will only use your contact details in case we need to contact you with regard to
this booking, unless you consent
to further communications, as offered later in this booking process.</p>
<input type="email" id="email" name="email" form="alternative-booking-form-1"
placeholder="your email address" value=""
required /><br />
<input type="tel" id="phone" name="phone" form="alternative-booking-form-1"
placeholder="phone" value="" required /><br />
</fieldset>
<fieldset>
<input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1" />
<br />
<input type="button" id="submit" name="submit" form="alternative-booking-form-1"
class="red" value="Next →" />
<br />
<br />
</fieldset>
</form>
</div>
<div id="output" name="output" ></div>
<script>
window.onload = function() {
document.getElementById("alternative-booking-form-1").addEventListener( "ready", handleFormSubmit );
}
function handleFormSubmit(formObject) {
google.script.run
.withSuccessHandler( showSuccess )
.withFailureHandler( showError )
.processForm_1( formObject );
}
</script>
</body>

Jsoup multiple forms with controls with same name

All this html on same page:
<form name="name1" ....>
<input name="tName"type="text">
<input value="Search" type="submit">
</form>
<form name="name2"....>
<input name="tName" type="text">
<input value="Search" type="submit">
</form>
doc = Jsoup.connect("addr")
.data("tName", "foo")
.userAgent("Mozilla")
.post();
Now, how do I know which form I submited ? Where to specify to which of 2 forms inputed data belogs to, in jsoup code ?
<form name="name1" ....>
<input name="tName"type="text">
<input value="Search" type="submit">
<input name="formNumber" value="form1" type="hidden">
</form>
<form name="name2"....>
<input name="tName" type="text">
<input value="Search" type="submit">
<input name="formNumber" value="form2" type="hidden">
</form>
doc = Jsoup.connect("addr")
.data("tName", "foo")
.userAgent("Mozilla")
.post();
If you can't get the name of the form itself, you'll be able to get the values of the hidden inputs.

IE9/8 Form issue

I am having issues with a form in IE9 and IE8.
I have put the code into JS Fiddle (http://jsfiddle.net/EV3N5/) and it comes up with the error '< / FORM >' in red.
<FORM NAME="myform" ACTION="" METHOD="GET">
<p>Window Width
<br />
<input name="inputWidth" />
</p>
<p>Window Height
<br />
<input name="inputHeight" />
</p>
<p>Glass Thickness
<br />
<input name="inputGlass" />
</p>
<p>Sash Density
<br />
<input name="inputDensity" />
</p>
<INPUT TYPE="button" NAME="button" Value="Submit" onClick="testResults(this.form)">
</FORM>
Could someone go over it and point out my stupid mistake?
Thank you.
The red form is because the input tag is not closed:
<INPUT TYPE="button" NAME="button" Value="Submit" onClick="testResults(this.form)">
To
<INPUT TYPE="button" NAME="button" Value="Submit" onClick="testResults(this.form)"/>
As to why it is not working in IE, you need to be more specific about what is not working.

jQuery mobile form submit without triggering page Naviagion

How can I navigate to a new page without triggering the jQuery mobile form handling, which creates a ajax request and then loads the page with it's speczial funcationallity (I don't know the name for it)
Add this attribute to the form:
data-ajax="false"
Example:
<form action="demo.php" method="post" id="check-user" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
<fieldset>
<div data-role="fieldcontain">
<label for="username">Enter your username:</label>
<input type="text" value="" name="username" id="username"/>
</div>
<div data-role="fieldcontain">
<label for="password">Enter your password:</label>
<input type="password" value="" name="password" id="password"/>
</div>
<input type="button" data-theme="b" name="submit" id="submit" value="Submit">
</fieldset>
</form>

Zend_Form_Element_Checkbox issue

I want to achieve the example html markup using Zend_Form_Element_Checkbox. Encounter problems, however, for example page_actions[]. I want to have this in a attribute and receive array in request. I know I must do it in the init of the form, but I lack knowledge of Zend Framework. I tried several options. For example, this:
// In for loop in init method
$element = new Zend_Form_Element_Checkbox('test['.$i.']');
$element->setIsArray(true)->setBelongsTo('checkbox_name');
$this->addElement($element);
This is the markup I would like to achieve:-
<div>
<div>
Some bold message for this group
</div>
<div>
<span>
<input type="checkbox" id="qf_18" value="12" name="page_actions[]"/><label for="qf_18">Test 18</label><br/>
<input type="checkbox" id="qf_20" value="13" name="page_actions[]"/><label for="qf_20">Test 20</label><br/>
<input type="checkbox" id="qf_22" value="14" name="page_actions[]"/><label for="qf_22">Test 22</label><br/>
<input type="checkbox" id="qf_24" value="15" name="page_actions[]"/><label for="qf_24">Test 24</label><br/>
<input type="checkbox" id="qf_26" value="16" name="page_actions[]"/><label for="qf_26">Test 26</label>
</span>
</div>
</div>
<div>
<div style="font-weight: bold;">
Some bold message for this other group
</div>
<div>
<span>
<input type="checkbox" id="qf_28" value="17" name="page_actions[]"/><label for="qf_28">Test 28</label><br/>
<input type="checkbox" id="qf_30" value="18" name="page_actions[]"/><label for="qf_30">Test 30</label><br/>
<input type="checkbox" id="qf_32" value="19" name="page_actions[]"/><label for="qf_32">Test 32</label><br/>
<input type="checkbox" id="qf_34" value="20" name="page_actions[]"/><label for="qf_34">Test 34</label><br/>
<input type="checkbox" id="qf_36" value="21" name="page_actions[]"/><label for="qf_36">Test 35</label>
</span>
</div>
</div>
You can use view script to do that. I think zend form decorator is not support to this kind of form.