Multiple Form elements pointing to a single path in Spring - forms

I did a bit of searching, and it doesn't appear that this specific question has been asked (if I am mistaken, I apologize).
The application I'm working on processes reports on a set of data, and the reports are separated by client and the report category.
The issue I have is that the number of clients and categories varies for each job, so I need to dynamically generate the following form elements based on the requirements for each job.
<!-- CLIENT NAME -->
Inclusion Client Name:
<form:errors path="clientName" cssClass="error"/><br />
<form:input path="clientName" />
<br /><br />
<!-- REPORTING CCATEGORIES -->
Reporting Categories:
<form:errors path="reportingCategories" cssClass="error"/><br />
<form:input path="reportingCategories" />
<br /><br />
<!-- comma separated list of categories -->
In the process of testing some theories, I learned that you can point multiple form elements to the same path, and Spring will concatenate the values into a comma separated list. My only concern is the order in which these form elements are read.
<!-- CLIENT NAME A -->
Inclusion Client Name:
<form:errors path="clientName" cssClass="error"/><br />
<form:input path="clientName" />
<br /><br />
<!-- REPORTING CATEGORIES A -->
Reporting Categories:
<form:errors path="reportingCategories" cssClass="error"/><br />
<form:input path="reportingCategories" />
<br /><br />
<!-- comma separated list of categories -->
<!-- CLIENT NAME B -->
Inclusion Client Name:
<form:errors path="clientName" cssClass="error"/><br />
<form:input path="clientName" />
<br /><br />
<!-- REPORTING CATEGORIES B -->
Reporting Categories:
<form:errors path="reportingCategories" cssClass="error"/><br />
<form:input path="reportingCategories" />
<br /><br />
<!-- comma separated list of categories -->
In the second code snippet above, my test case reads CLIENT NAME A before CLIENT NAME B (Essentially working down the page from top to bottom). Will this always be the case?

Yes, the order will always be the order in which the form fields are defined in the form.
When defining multiple fields with the same path, you also can bind them as an array in your request method like:
#Controller
public class YourController {
#RequestMapping(....)
public String foo(#RequestParam String clientName[], #RequestParam String reportingCategories[], BindingResult result) {
...
}
}

Related

Loading foreach using core methods in ZK MVVM

I would like to read some values from my vm. Here's an example of what I've done so far
<div forEach="${vm.activityData.activityList}">
<label
value="#load(c:cat3('vm.activitiData.', each, '.organizer' ))" />
</div>
But it seems like it is unable to read the each inside the cat3. My objective is to get the organizer for each activity. Any idea how to achieve this ?
Update
For example, I want to simplify from this codes :
<div>
<label value="#load(vm.activityData.A.name)" />
<label value="#load(vm.activityData.ASponsor)" />
<label value="#load(vm.activityData.AOrganizer)" />
<separator/>
<label value="#load(vm.activityData.B.name)" />
<label value="#load(vm.activityData.BSponsor)" />
<label value="#load(vm.activityData.BOrganizer)" />
<separator/>
</div>
into
<div forEach="${vm.activityData.activityList}">
<label value="#load(c:cat3('vm.activityData.', each, '.name'))" />
<label value="#load(c:cat3('vm.activityData.', each, 'Sponsor'))" />
<label value="#load(c:cat3('vm.activityData.', each, 'Organizer'))" />
<separator/>
</div>
Basically what I want to do is to dynamically combine part of strings into one string. And then use #load(string).
Today I had the time to test it and I prepared a fiddle for you.
Your ZK version is good but I did forget to add one more thing.
You have to make use of the custom-attributes otherwise it wouldn't work.
Solution :
<div forEach="${vm.activityData.activityList}">
<custom-attributes field="${each}"/>
<label value="#load(vm.activityData[field].name)" />
<label value="#load(vm.activityData[c:cat(field, 'Sponsor')]" />
<label value="#load(vm.activityData[c:cat(field, 'Organizer')]" />
<separator/>
</div>
I created fast a working fiddle where you can test or look how it works.

Usage of Float and Integer in DynaActionForm properties and retrieving them with struts-html.tld tags

I have a DynaActionForm element in my struts-config.xml like:
<form-bean name="myActionForm" type="org.apache.struts.action.DynaActionForm">
<!-- Control Params -->
<form-property name="action" type="java.lang.String" />
<form-property name="list" type="java.lang.String" initial="master_document_list.data"/>
<!-- Business params -->
<form-property name="code" type="java.lang.String"/>
<form-property name="name" type="java.lang.String"/>
<form-property name="description" type="java.lang.String"/>
<form-property name="sequenceNumber" type="java.lang.Float"/>
</form-bean>
And the jsp page where I am trying to use it:
<!-- struts-html.tld imported with prefix html -->
<html:form>
<table>
<tr>
<td>Sequence No.</td>
<td><html:text property="sequenceNumber" maxlength="15" style="width:75%"/></td>
</tr>
<table>
</html:form>
but when i do this I am getting a JspException saying "No getter method for property sequenceNumber. I am quite sure that the name is correct. Is it the type that is not getting accepted then? I thought the DynaActionForm allowed types are all major java types including the Thread-Safe Wrappers (e.g. Float, Integer, Short, Long, etc.).
N.B. I am using struts1
After digging apache documentation, i figured out that
<html:text> tag has got the following settings:
name= Name of the form bean
property= Name of the property associated to the form-property tag for the bean above
Althernatively, using ${myBean.map.myProp} will point me to the right direction.
Thanks all,

Validate only specific parts of the form instead of whole form

I have a whole form with a lot of components including a p:tab
When I click on p:commandButton id=c1 to submit the whole form content:
I need to validate the whole form required messages but I do need to ignore the p:tab required messages fields.
If I click on p:commandButton id=c2 inside the p:tab, I need to validate only the required messages fields inside the p:tab.
What is the best solution for this ? Thanks in advance.
You seem to be using the "God Form" anti-pattern. Everything is thrown together in a single <h:form>. This is a poor design/practice. Most sensible way would be to put the fields and buttons in separate forms so that only the related fields and buttons are in its own form so that the form submit doesn't unnecessarily submit/process/convert/validate irrelvant data in other forms.
See also:
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
If that's not possible due to some (strange?) design restrictions, then there are at least 2 other ways:
If you're using ajax, then you can make use of the process attribute. It defaults to #form which would process the entire form. It accepts a space separated string of (relative) client IDs of input field which you'd like to process during submit.
<p:inputText id="field1" ... required="true" />
<p:inputText id="field2" ... required="true" />
...
<p:inputText id="field3" ... required="true" />
<p:inputText id="field4" ... required="true" />
...
<p:commandButton id="c1" ... process="field1 field2" />
...
<p:commandButton id="c2" ... process="field3 field4" />
See also: Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
If you're not using ajax, or desire a non-ajax fallback, then just check in the required attribute which button has been pressed. This is easy by checking the presence of the button's client ID in the request parameter map.
<p:inputText id="field1" ... required="#{not empty param[c1.clientId]}" />
<p:inputText id="field2" ... required="#{not empty param[c1.clientId]}" />
...
<p:inputText id="field3" ... required="#{not empty param[c2.clientId]}" />
<p:inputText id="field4" ... required="#{not empty param[c2.clientId]}" />
...
<p:commandButton id="c1" binding="#{c1}" ... />
...
<p:commandButton id="c2" binding="#{c2}" ... />
(note: no additional bean properties necessary for c1 or c2! the code is as-is)
See also How to let validation depend on the pressed button?
You can refactor this somewhat with a more self-documenting variable name:
<c:set var="c1ButtonPressed" value="#{not empty param[c1.clientId]}" />
<c:set var="c2ButtonPressed" value="#{not empty param[c2.clientId]}" />
...
<p:inputText id="field1" ... required="#{c1ButtonPressed}" />
<p:inputText id="field2" ... required="#{c1ButtonPressed}" />
...
<p:inputText id="field3" ... required="#{c2ButtonPressed}" />
<p:inputText id="field4" ... required="#{c2ButtonPressed}" />
...
<p:commandButton id="c1" binding="#{c1}" ... />
...
<p:commandButton id="c2" binding="#{c2}" ... />
FYI you can also process the id of a panel containing the controls you want to validate - example :
<p:outputPanel id="thisPanel">
<p:inputText id="field1" ... required="#{not empty param[c1.clientId]}" />
<p:inputText id="field2" ... required="#{not empty param[c1.clientId]}" />
<p:commandButton id="c2" ... process="thisPanel" />

Spring sf form tags how to change type?

This gives me error prone HTML code, because Spring automatically generates text type, how do I change this behaviour?
<td><sf:input type="password" path="password" size="20" maxlength="30" /><br />
<sf:errors path="password"cssClass="error" />
</td>
html code generated:
… name="password" **type="password" type="text"** value="" size="20" maxlength="30" …
Use the password tag
This tag renders an HTML 'input' tag with type 'password'
<form:password path="password" />
For details
The password tag

Why the form id prefix in the h:message output?

I'm currently trying simple validation using required="true"
<h:form>
<h:messages globalOnly="true"/>
<h:panelGrid>
<h:panelGrid columns="3">
<f:facet name="header">
Login Application Sample
</f:facet>
<h:outputLabel for="UserId" value="User Id" />
<h:inputText id="UserId" value="#{userBean.userId}" required="true" />
<h:message for="UserId"/>
<h:outputLabel for="Password" value="Password" />
<h:inputSecret id="Password" value="#{userBean.password}" required="true" />
<h:message for="Password" />
<f:facet name="footer">
<h:commandButton value="Login" action="#{userBean.login}"/>
<h:commandButton type="reset" value="Reset"/>
</f:facet>
</h:panelGrid>
</h:panelGrid>
</h:form>
Leaving the fields blank, and then clickin on the login button, these error messages will display on the right side of each field :
j_idt7:UserId: Validation Error: Value is required.
j_idt7:Password: Validation Error: Value is required.
This is what I expected, but I don't want to display the form id prefix of 'j_idt7:'. I read book examples, they don't output the form id prefix. What I want is :
UserId: Validation Error: Value is required.
Password: Validation Error: Value is required.
What should I do to skip displaying the form id prefix in the component specific messages ?
I'm currently testing JSF 2 in glassfish v3.
The message label defaults to component's client ID, exactly the one as you can see in generated HTML output via rightclick, View Source. That j_id7 is in this particular case the client ID of the parent <form> element. If you give the JSF component a fixed ID like <h:form id="login"> then the labels will become login:UserId and login:Password respectively.
You can however use the input component's label attribute to override it altogether so that the message label will be shown exactly as you intented.
<h:inputText ... label="User ID" />
<h:inputSecret ... label="Password" />
If the input component's label attribute is present, then it will be used instead of the client ID. Using prependId="false" as suggested by other answers has disadvantages. Don't do that.
A completely different alternative is to use requiredMessage (or converterMessage or validatorMessage) attribute for this, but this doesn't allow parameterizing messages and thus you'd have to hardcode the labels and such.
<h:inputText ... label="User ID is required." />
<h:inputSecret ... label="Password is required." />
See also:
Change the default message "Validation Error: Value is required" to just "Value is required"
Getting the component id on the error validation message
How to parameterize requiredMessage attribute in composite component?
Noted should be that it's indeed awkward to have labels duplicated like this:
<h:outputLabel for="userId" value="User ID" ... />
<h:inputText id="userId" ... label="User ID" />
<h:outputLabel for="password" value="Password" ... />
<h:inputSecret id="password" ... label="Password" />
If you happen to use JSF utility library OmniFaces, then you can use <o:outputLabel> to let JSF transparently set the label attribute of the associated component:
<o:outputLabel for="userId" value="User ID" ... />
<h:inputText id="userId" ... />
<o:outputLabel for="password" value="Password" ... />
<h:inputSecret id="password" ... />
If you use MyFaces and the bean validation framework (JSR303) try to define a MessageBundle with a key javax.faces.validator.BeanValidator.MESSAGE
faces-config.xml
<application>
<message-bundle>ApplicationMessages</message-bundle>
</application>
ApplicationMessages.properties
#javax.faces.validator.BeanValidator.MESSAGE={1}: {0}
javax.faces.validator.BeanValidator.MESSAGE={0}
Details
You need to override these messages from JSF.
You can have a messages.properties file in your classpath
Messages.properties
javax.faces.component.UIInput.REQUIRED=Field must be entered.
Faces-config.xml
<application>
<message-bundle>Messages</message-bundle>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
</application>
Have a look at this Article
If you care to see the HTML source from your browser, you will find out that the id of your input field is <form-id>+":"+<input-field-id>, in your case, j_idt7:UserId:. Try to give your <h:form> some meaningful id in order to make some sense out of it. You can read about JSF IDs here. In case you don't like it, you can turn it off by modifying your form tag to something like this,
<h:form prependId = false> // its true by default.
But that might turn out to be problematic, as pointed out by BalusC here.
Furthermore, it seems like you have never configured any validation messages yourself. Which in turn ends up with this message. Hence, a message.properties file is needed to have a control over message and show something more appropriate. Even then the field name should not be the part of the message, to make those validation message generic to avoid repetition. See BalusC's answer regarding the use of label attribute inside the <h:inputText>.
Where you are writing required = true, there you can define requiredMessage = "My Message" for any input type.
e.g.
<h:inputText autocomplete="false" id = "CellNumber" label="Cell Number" required="true" requiredMessage="Cell Number Required" maxlength="10" value="#{userManagedBean.cellNumber}" >