Using a datalist in a form in Spring-MVC - spring-mvc

I'm attempting to convert an input form from a to a but does not accept the list attribute, does anyone know a workaround? This is my first time using Spring and I'm at a loss here.
<tr>
<th><form:label path="livrable.composant"><s:message code="composant"/></form:label></th>
<td>
<form:input path="livrables.composants" list="composants">
<c:if test = "${not empty composants}">
<datalist id="composants">
<c:forEach var="composantValue" items="${composants}">
<option value="${composantValue.nom}"/>
</c:forEach>
</datalist>
</c:if>
<br/>
<form:errors path="livrable.composant" cssClass="error"/>
</td></tr>

Nevermind I'm an idiot, I didn't close the tag it should be:
<form:input path="livrables.composants" list="composants"/>

Related

Form Submit Not Working on the integration of HDIV and Spring MVC

I managed to integrate HDIV and Spring MVC. Now HDIV generated security URLs for the static links. But when I tries to submit a link with a parameter, I always get error message. I know the reason is when the URL of a form is generated, the parameter is not a part of the URL. But I cannot find a workaround. Please help. Thanks a lot.
The form part is like this:
<c:url var="url" value="/contract/report/report" />
<form:form action="${url}" method="get">
<table >
<tr>
<td><label>Name:</label></td>
<td><select id="nameId" name="nameId">
<c:forEach var="c" items="${Users}">
<option value='${c.id}'> ${c.name}</option>
</c:forEach>
</select> </td>
<td><Button type="submit" >Submit</Button> </td>
</tr>
</table>
</form:form>
Update:
I found the workaround is I have to rewrite the select options with spring options like this:
<form:select path="contractId">
<c:forEach var="c" items="${Users}">
<form:option value="${c.id}" label="${c.name}"></form:option>
<c:forEach var="c" items="${Users}">
</form:select>
Thanks everyone.
If you want to add a parameter to the URL then this might work.
<c:url var="url" value="/contract/report/report"><c:param name="parameter" value="value" /></c:url>
Don't use c:url for forms, you don't need it:
<form:form action="${pageContext.servletContext.contextPath}/contract/report/report" method="get">
...
</form:form>
I found the workaround is I have to rewrite the select options with spring options like this:
<form:select path="contractId">
<c:forEach var="c" items="${Users}">
<form:option value="${c.id}" label="${c.name}"></form:option>
<c:forEach var="c" items="${Users}">
</form:select>
Thanks everyone.

Spring MVC Binding: How to find if bind action was successful

I have a Spring MVC form:select whose form:options are bind with a List<Custom_Object>. The List<Custom_Object> is named as LOCALIZATION_LIST in the code below.
The path attribute of form:select is used to set the selected option.
<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
<table>
<tr>
<td>Data Type</td>
<td>
<form:select path="datatype" onchange="" cssClass="large" id="datatypes">
<c:if test="${! empty LOCALIZATION_LIST}">
<form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
</c:if>
</form:select>
</td>
</tr>
</table>
</form:form>
Now my problem is that there might be a scenario when editElementDetails.datatype may contain a value which is not there in LOCALIZATION_LIST at all. So currently Spring MVC shows the first element of LOCALIZATION_LIST as selected.
Is there a way I can figure out whether the bind action for setting the selected object in form:options of form:select was successful or not?
So that when the binding was not successful, I can then add one extra form:option with the new value in the form.
Note: Answering my own question
I figured it out for now by manually searching for the incoming command object field value in the LOCALIZATION_LIST and if not found, adding an extra form:option corresponding to it.
I was hoping to find a JSTL/Spring-MVC out-of-box solution for this though.
Here is the code:
<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
<table>
<tr>
<td>Data Type</td>
<td>
<form:select path="datatype" onchange="" cssClass="large" id="datatypes">
<c:if test="${! empty LOCALIZATION_LIST}">
<c:set var="contains" value="false" />
<c:forEach var="item" items="${LOCALIZATION_LIST}">
<c:if test="${item eq editElementDetails.datatype}">
<c:set var="contains" value="true" />
</c:if>
</c:forEach>
<c:if test="${ !contains }">
<form:option label="${editElementDetails.datatype}" value="${editElementDetails.datatype}"/>
</c:if>
<form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
</c:if>
</form:select>
</td>
</tr>
</table>
</form:form>
What you want to achieve is the process of validating a form submission that is what I understood (maybe got you all wrong).You can use Spring-Validator here is one nice tutorial about using it.
Spring Form validation . Hope this helps.

Spring <form:select initial value

Is there a way of setting a initial value (One of x in the list) for a
<form:select drop down similar to the text area default value?
i.e.
<td valign="top">
<form:select id="${appNameFormId}" path="metadata.appName" items="${dbList}"
disabled="true" itemValue="appName" itemLabel="appName">
</form:select>
</td>
<td valign="top">
<TEXTAREA id="${nameFormId}" name=jobName ROWS="1" COLS="25" >${job.jobName}
</TEXTAREA>
</td>
Because SELECT value comes from command object during rendering and go back to command object after submission I suggest to assign default value to metadata.appName inside Controller (manually or by using method with #ModelAttribute annotation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib).

How does MVC Client Side validation work exactly?

I have used MVC MVC 2.0 Client Side validation, but it does not work as expected. Now I am trying to find out, what I did wrong.
How does it work?
I have this rendered form:
<form method="post" action="/Sprint/Edit/68d4886b-a86a-4f0b-b713-39219febddf3">
<fieldset>
<legend>Sprint</legend>
<table>
<tbody><tr>
<td><label for="sprint_Title">Title</label></td>
<td><input type="text" value="Wichtige Private Erledigungen" name="sprint.Title" id="sprint_Title" class="input-box t-input"></td>
<td><span class="error"><span id="sprint_Title_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
<tr>
<td><label for="sprint_Date">Date</label></td>
<td>
<div id="sprint_Date" class="date-picker t-input t-widget t-datepicker"><input value="12.05.2010" name="sprint.Date" id="sprint_Date-input" class="t-input" autocomplete="off"><a title="Open the calendar" tabindex="-1" href="#" class="t-link t-icon t-icon-calendar">select date</a></div>
</td>
<td><span class="error"><span id="sprint_Date_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
<tr>
<td><label for="sprint_Description">Description</label></td>
<td><textarea rows="10" name="sprint.Description" id="sprint_Description" cols="80" class="t-input">dsfs1</textarea></td>
<td><span class="error"><span id="sprint_Description_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
</tbody></table>
</fieldset>
<input type="submit" value="Submit" name="Submit" id="Submit" class="t-button t-state-default">
</form>
And this script is rendered:
<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"sprint.Title","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Title_validationMessage","ValidationRules":[{"ErrorMessage":"Title is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"sprint.Date","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Date_validationMessage","ValidationRules":[{"ErrorMessage":"Das Feld \"Date\" ist erforderlich.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"sprint.Description","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Description_validationMessage","ValidationRules":[]}],"FormId":null,"ReplaceValidationSummary":false});
//]]>
</script>
But to my surprise the breakpoint only stops at page load. I think it should be also if a field is tabbed? At least, after I press the post button.
Why does client side validation not work for me?
This just sets up the handlers, it won't actually hit this line when the form is submitted, nor when you tab between fields.
You will get validation errors upon losing focus and/or form submission, however if you have other javascript errors on the page it can cause this to fail.
Also, make sure you've reference the required javascript files in your header:
MicrosoftAjax.js
MicrosoftMvcValidation.js

Can't Get ASP.NET MVC Form To Post

I am having problems getting my form to Post to my Save method in a controller. I am new to MVC, and have followed several examples to try to get this to work. Here is my html markup:
<form action="Edit/Save" method="post">
<fieldset>
<legend>Personal Information</legend>
<table class="editGrid">
<tr>
<td><label for="txtFirstName">First Name:</label></td>
<td><input type="text" id="txtFirstName" value="<%=user.FirstName %>" name="FirstName" /></td>
</tr>
<tr>
<td><label for="txtLastName">Last Name:</label></td>
<td><input type="text" id="txtLastName" value="<%=user.LastName %>" name="LastName" /></td>
</tr>
<tr>
<td><label for="txtNtLogin">NT Login:</label></td>
<td><input type="text" id="txtNtLogin" value="<%=user.NtLogin %>" name="NtLogin" /></td>
</tr>
<tr>
<td><label for="txtHireDate">Hire Date:</label></td>
<td><input type="text" id="txtHireDate" value="<%=string.Format("{0:d}",user.HireDate) %>" name="HireDate" /></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Job Information</legend>
<table class="editGrid">
<tr>
<td><label for="CostCenters">Cost Center:</label></td>
<td><%=Html.DropDownList("CostCenters")%></td>
</tr>
<tr>
<td><label for="Managers">Manager:</label></td>
<td><%=Html.DropDownList("Managers")%></td>
</tr>
<tr>
<td><label for="Responsibilities">Responsibility:</label></td>
<td><%=Html.DropDownList("Responsibilities")%></td>
</tr>
<tr>
<td><label for="Departments">Department:</label></td>
<td><%=Html.DropDownList("Departments")%></td>
</tr>
<tr>
<td><label for="Active">Active:</label></td>
<td><%=Html.CheckBox("Active",user.Active) %></td>
</tr>
<tr>
<td><label for="txtHireDate">Hire Date:</label></td>
<td><%=Html.TextBox("txtHireDate",string.Format("{0:d}",user.HireDate)) %></td>
</tr>
<tr>
<td><label for="txtReleaseDate">Release Date:</label></td>
<td><%=Html.TextBox("txtReleaseDate",string.Format("{0:d}",user.ReleaseDate)) %></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Save Changes" />
</form>
This form routes to a Save method in my EditController. Here is the code for my EditController's Save method:
public class EditController : Controller
{
public ActionResult Save()
{
//Save code goes here
}
I have tried using the html form tag, and also the Html helper code:
using (Html.BeginForm("Save", "Edit"))
Here is the entry from my RegisterRoutes method in the Global.asax file:
routes.MapRoute("EditSave", "{controller}/Save",
new { controller = "Edit", action = "Save" });
No matter what I do, the submit button does not trigger the Save method. Yet, if I manually key in the Url, The code breaks right into the Save method.
Edit:
Per Craig Stuntz's comment, I checked the source of the page. The page actually contains 2 forms, although only 1 is coded on the page by myself: Here is the HTML that appears prior to my form tag:
<form name="aspnetForm" method="post" action="44" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjM3OTAyNTUzZGQrHhVn9+t78aHxN0vHvKUJ8DQWlQ==" />
</div>
<div id="nav">
<span id="navLinks">
Placeholder Link
</span>
<span id="userName">
<span id="ctl00_lblUserName" class="UserName">Welcome, Test User</span>
</span>
</div>
<div id="Content">
<div id="formContainer">
<form action="Edit/Save" method="post">
I didn't think MVC generated viewstate or additional form tags. I am pulling data and populating it into this form using another method from the same controller. Am I doing something wrong here?
Ok, got the answer. And thanks Craig for having me look at the HTML again! My master page had generated a form tag in it without my knowing it, so I essentially had nested forms on the same page. After I removed the form from the Master Page, everything worked perfectly.

Resources