Pre-populating a Spring MVC form - forms

I am looking for a way to pre-populate a Spring MVC form from values stored in a session-scoped bean. (using this namespace: http://www.springframework.org/tags/form).
For example, say I have added a queryInfo object to the uiModel.
How do I display the name instance variable from the queryInfo object?
<form:input path="queryInfo.name" />
Is this possible? If so how?

In your request mapping, add the bean to the model:
model.addAttribute("queryInfo", queryInfo);
Then use modelAttribute in the form tag to bind it to the form:
<form:form id="some-form" modelAttribute="queryInfo">
...
Now name will display (provided there is a getter in your object appropriately named) when you do this:
<form:input path="name" />
Keep in mind form:input is a child tag of form:form. It is not meant to be used on its own.

#skel625's solution for form:input is perfect, but in the case of the form:select, with different option how do I set attributes? because in this way only works for the form:input
My form:select is like this:
<form:select path="dolorefastidio">
<option value="1"><spring:message code="questionnaire.compile.label.paindiscomfort.one"/></option>
<option value="2"><spring:message code="questionnaire.compile.label.paindiscomfort.two"/></option>
<option value="3"><spring:message code="questionnaire.compile.label.paindiscomfort.three"/></option>
<option value="4"><spring:message code="questionnaire.compile.label.paindiscomfort.four"/></option>
<option value="5"><spring:message code="questionnaire.compile.label.paindiscomfort.five"/></option>
</form:select>

the solution is put in a value attribute the bean attribute like this
Exam Register Form
<div class="form-group has-success">
<label for="code" class="col-lg-3 control-label">Exam Code</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="cod" placeholder="code" value="${editExam.cod}"/>
<form:errors path="cod" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="name" class="col-lg-3 control-label">Exam Name</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="name" placeholder="name" value="${editExam.name}"/>
<form:errors path="name" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="teachNme" class="col-lg-3 control-label">Teacher Name</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="teachName"
placeholder="Teacher Name" value="${exams.teachName}" />
<form:errors path="teachName" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="vote" class="col-lg-3 control-label">Final Grade</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="vote"
placeholder="Vote" value="${exams.vote}" />
<form:errors path="vote" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="cfu" class="col-lg-3 control-label">Credits (CFU)</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="cfu"
placeholder="Credits" value="${editExam.cfu}" />
<form:errors path="cfu" cssClass="error" />
</div>
</div>
<input type="submit" value="Save"
class="btn btn-primary pull-right">
</fieldset>
</form:form>

Related

ASP.NET Core and Bootstrap : how to put dropdown inside form group?

I'm using <label asp-for and <input asp-for tags inside my form to carry out "Create Form" action as well as Bootstrap for looks. How do I fix this bug, it's working with asp-for, I can create and edit successfully through these fields but its looks rather twisted than user friendly.
<div class="form-group">
<label asp-for="City" class="control-label"></label>
<input asp-for="City" class="form-control" />
<span asp-validation-for="City" class="text-danger"></span>
</div>
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with dropdown button">
<div class="input-group-append">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</button>
<div class="dropdown-menu">
<label asp-for="Department" class="dropdown-item">HR</label>
<input asp-for="Department" class="form-control" />
<label asp-for="Department" class="dropdown-item">Sales</label>
<input asp-for="Department" class="form-control" />
</div>
<span asp-validation-for="Department" class="text-danger"></span>
</div>
</div>
Here is the result:
Before select
During select
After select
As you can see its not populating inside the text field but its there, I tested it. Maybe there is other way to do it with HTML tags?
Do you want something like this, you can use the <select> tag to implement the dropdown:
<div class="form-group">
<label asp-for="City" class="control-label"></label>
<input asp-for="City" class="form-control" />
<span asp-validation-for="City" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Department" class="control-label"></label>
<select asp-for="Department" class="form-control">
<option selected>Choose Department</option>
<option value="HR">HR</option>
<option value="Sales">Sales</option>
<option value="Other">Other</option>
</select>
</div>
Result:

Unable to get submit action to occur in TYPO3 Fluid contact form

We are trying to manage to get a simple contact form running. But regardless of what we try, we get nothing at all.
The form itself is on the front-end and configured like this :
<f:section name="Main">
<div class="ext-frame container">
<h2>{record.header}</h2>
<f:form action="submit" name="order" object="{order}" class="form-horizontal">
<f:for each="{settings.offer}" as="obj" key="id">
<div class="row offer">
<f:if condition="{obj.offer.img}">
<f:then>
<div class="col-sm-4 col-md-4 offer-img-container">
<img src="../{obj.offer.img}" class="offer-image" alt="{obj.offer.img.alternative}" />
</div>
</f:then>
</f:if>
<div class="col-sm-8 col-md-8 {f:if(condition: '{obj.offer.img} == \'\'', then: 'col-sm-offset-4 col-md-offset-4')}">
<label class="offer-label">
<f:form.radio property="offer" value="{id}" />
<div class="pull-right">
<p class="lead">
{obj.offer.title}<br/>
<small>{obj.offer.subtitle}</small>
</p>
<p class="small">{obj.offer.description}</p>
</div>
</label>
</div>
</div>
<hr />
</f:for>
<div class="form-group row">
<label for="salut" class="col-sm-3 control-label">Anrede*</label>
<div class="col-sm-5">
<f:form.select property="salut" options="{herr: 'Herr', frau: 'Frau'}" class="form-control" />
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-3 control-label">Vorname*</label>
<div class="col-sm-5">
<f:form.textfield property="name" value="" class="form-control" />
</div>
</div>
<div class="form-group row">
<label for="surname" class="col-sm-3 control-label">Nachname*</label>
<div class="col-sm-5">
<f:form.textfield property="surname" value="" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-sm-3 cusnr">
<label for="cusNr" class="control-label">Kundennummer</label>
<span id="helpBlock" class="help-block">(wenn bekannt)</span>
</div>
<div class="col-sm-5">
<f:form.textfield property="cusNr" value="" class="form-control" />
</div>
</div>
<div class="form-group row">
<label for="addr" class="col-sm-3 control-label">Straße/Hausnr.*</label>
<div class="col-sm-5">
<f:form.textfield property="addr" value="" class="form-control" />
</div>
</div>
<div class="form-group row">
<label for="zip" class="col-sm-3 control-label">PLZ*</label>
<div class="col-sm-3">
<div class="input-group">
<span class="input-group-addon">D-</span>
<f:form.textfield property="zip" value="" class="form-control" type="number" maxlength="5" />
</div>
</div>
</div>
<div class="form-group row">
<label for="city" class="col-sm-3 control-label">Ort*</label>
<div class="col-sm-5">
<f:form.textfield property="city" value="" class="form-control" />
</div>
</div>
<div class="form-group row">
<label for="mail" class="col-sm-3 control-label">E-Mail*</label>
<div class="col-sm-5">
<f:form.textfield property="mail" value="" class="form-control" type="email" />
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-sm-3 control-label">Telefon</label>
<div class="col-sm-5">
<f:form.textfield property="phone" value="" class="form-control" type="tel" />
</div>
</div>
<div class="form-group row"><div class="col-sm-offset-3 col-sm-5">
<f:form.select property="know" options="{
0: 'a?*',
1: 'a',
2: 'b',
3: 'c',
4: 'd',
5: 'e',
6: 'f',
7: 'g'
}" class="form-control" />
</div></div>
<div class="form-group row">
<div class="col-sm-offset-3 col-sm-9">
<div class="checkbox">
<label>
<f:form.checkbox name="term" value="1" />
Ich habe die AGB gelesen und erkläre mich mit
ihnen einverstanden. Ich wurde über mein <a href="widerruf.php" target="_blank">
Widerrufsrecht</a> informiert.
</label>
</div>
</div>
</div>
<div class="form-group row">
<f:form.submit name="submit" value="Send" class="col-xs-12 col-sm-5 col-sm-offset-3 btn" />
</div>
</f:form>
</div>
</f:section>
In our ContentController.php there is also a submitAction given but it seems that this Action is never called :
public function submitAction() {
echo 'Testoutput';exit;
}
Where does the form lead you after submitting? Check out the final HTML action="" if everything is correct.
Try to set the form "method" to POST. I've just had some problems with forms using GET because the target PID got lost for unknown reasons so I had to add a <input type="hidden" name="id" value="{targetPid}" /> to get it working.

Bootstrap 3 form Alignment

I am using Bootstrap 3, what I want cover whole form on full page like attached image, in the image I am using kendo UI popup window. pop up window width is 1000px
Here is my HTML code,
<section class="container-fluid padding-rl">
<div class="row">
<div class="col-sm-3">
<div class="profile-box">
<img class="profile-picture" src="" alt="" />
</div>
</div>
<div class="col-sm-8">
<div class="row">
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-1">First Name:</label>
<div class="col-sm-3">
<input type="text" class="form-control" data-bind="value: selectedRecord.FirstName" />
</div>
<label class="control-label col-sm-1 col-sm-offset-1">Last Name:</label>
<div class="col-sm-3">
<input type="text" class="form-control" data-bind="value: selectedRecord.LastName" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1">Email:</label>
<div class="col-sm-3" style="">
<input type="text" class="form-control" data-bind="value: selectedRecord.Email" />
</div>
<label class="control-label col-sm-1 col-sm-offset-1 ">Mobile:</label>
<div class="col-sm-3" style="">
<input type="text" class="form-control" data-bind="value: selectedRecord.Mobile" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1">Phone:</label>
<div class="col-sm-3" style="">
<input type="text" class="form-control" data-bind="value: selectedRecord.OfficePhone" />
</div>
<label class="control-label col-sm-1 col-sm-offset-1 ">Account:</label>
<div class="col-sm-3" style="">
<input type="text" id="txtContactAccount" class="form-control" data-bind="value: selectedRecord.ContactAccount" />
</div>
</div>
</div>
</div>
</div>
</div>
Attached image
Thank you in advance
Unfortunately I don't see your code in your question. From the sample picture I'm guessing what you are looking to do is create a modal?
Take a look at bootstrap's documentation here:
Bootstrap JavaScript
Scroll down a bit to the part about modals, they have nice samples there too.

Spring Tags Form Input

i have some probelm in spring tag form input..
if i use <form:input>..there will be error displayed
java.io.IOException: JspException when evaluating the body but if i used html tag, it's can't connect to database.
<form:form method="POST" action="/sampling/insert"
modelAttribute="sampling">
<div class="form-row control-group row-fluid">
<label class="control-label span3" name="populasi" for="normal-field">Population</label>
<div class="controls span9">
<form:input path="populasi" type="text" id="normal-field"
class="row-fluid" />
</div>
</div>
<div class="form-row control-group row-fluid">
<label class="control-label span3" for="normal-field">Error
Rate</label>
<div class="controls span9">
<form:input path="tKesalahan" name="tKesalahan" type="text"
id="normal-field" class="row-fluid" />
</div>
</div>
<input type="submit" class="btn btn-success" rel="tooltip"
data-placement="top" value="Submit" />
</form:form>
Try this:
name, type are not valid attributes
change the tag attributes:
<form:input path="tKesalahan" id="normal-field" cssClass="row-fluid" />
Reference: spring input tag attributes

Responsive search form in Bootstrap

I am trying to build a simple horizontal search & filtering form using Twitter Bootstrap as can be seen in the image attached.
I am trying to build it so it adapts to different screen sizes as shown in the image. The results that I receive from my code aren't according my expectations.
<div id="search_block" class="panel-body">
<form role="form" class="form-horizontal" method="post" action="">
<div class="form-group">
<label class="col-xs-2 col-sm-2 col-md-1 control-label" for="v_search">Search:</label>
<div class="col-xs-8 col-sm-8 col-md-4">
<input type="text" placeholder="Enter Search Keywords" value="" name="v_search" id="v_search" class="form-control">
</div>
<div class="button-group col-xs-8 col-sm-8 col-md-2">
<button class="btn btn-primary" value="search" name="search" type="submit">Search</button>
<button class="btn btn-default" value="reset" name="reset" type="reset">Reset</button>
</div>
</div>
<div class="form-group">
<label class="col-xs-2 col-sm-2 col-md-1 control-label" for="filters">Filters:</label>
<div class="col-xs-8 col-sm-8 col-md-2" id="filters">
<select class="form-control" name="section_id">
<option value="all">All Sections</option>
<option value="all">Test</option>
</select>
</div>
<div class="col-xs-8 col-sm-8 col-md-2">
<select class="form-control" name="category_id">
<option value="all">All Categories</option>
<option value="71">Test</option>
</select>
</div>
</div>
</form>
My code can be found at: http://jsfiddle.net/NB89d/
You can use the col-*-offset classes to achieve what you want.
Demo: http://www.bootply.com/126439