Data Validation: Inheritance and lists - struts2

I am used WebWork and I am not able to validate my data properly.
The main two things I don't know how to do are:
-Validate a field with the validator of its super class.
Example: Say we have class Person and Class Employee. I want to validate an attribute of class Employee with my Person-validation.xml. Is it possible?
-Validate the length of a list:
I have an attribute that is a list, how could I check the length of the list and afterwards check every item within the list with its appropriate validator?
I tried:
<field name="list">
<field-validator type="visitor">
<message />
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">
list.size() < 2
</param>
<message key="too much items"/>
</field-validator>
</field>
but it is now working.
Thanks

For you "-Validate the length of a list" problem:
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[2 > list.size]]></param>
<message key="too much items"/>
</field-validator>

If someone has the same problem: At the end it is done automatically!!!
The validator of the super class is called by default :)
and use
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[2 > list.size]]></param>
<message key="too much items"/>
</field-validator>
for the list

Related

How to build an edit form for a list of strings in Grails?

I have a Grails domain class with a list of strings in it, and I want to edit these strings. For the sake of simplicity let's assume that the list is fixed size. Here's what I have:
MyEntity.groovy:
class MyEntity {
String name
List variables = ['one', 'two', 'three']
static hasMany = [
variables: String,
]
}
_fields/myEntity/variables/_widget.gsp:
<g:textField name="variables[0]" value="${value[0]}" />
<g:textField name="variables[1]" value="${value[1]}" />
<g:textField name="variables[2]" value="${value[2]}" />
This renders text fields for each element in the list that are prefilled with the correct values. However, when I edit the values and sumbit the form my edits get discarded. What am I missing?
Ok, I found it myself. The input fields all need to have the name of the domain property, without any index:
<g:textField id="variables[0]" name="variables" value="${value[0]}" />
<g:textField id="variables[1]" name="variables" value="${value[1]}" />
<g:textField id="variables[2]" name="variables" value="${value[2]}" />

How to validate boolean with XML using Expression

I have boolean field in action
testMeAction.class
private boolean testMe;
// with setter and getter.
testMeAction-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="expression" short-circuit="true">
<param name="expression"><![CDATA[(testMe == false)]]></param>
<message key="user.message" />
</validator>
</validators>
I would like to validate and display message when testMe = false;
But for some reason, it always displaying message, what ever value of testMe is.
Please can someone advice, where I am going wrong.

Struts2 - Can an action name be repeated in the struts.xml using the same action class but for different methods?

My idea is to perform actions using Struts2 the below way using a single Action class and multiple methods in it:
View roles action: manage/roles.action?method%3Aview=View
Add role action: manage/roles.action?method%3Aadd=Add
The URLs are called through invoking submit buttons as shown below from test.jsp:
<s:form namespace="/manage/" action="roles" method="get" >
<s:submit method="view" value="View" />
<s:submit method="add" value="Add" />
<s:submit method="edit" value="Edit" />
<s:submit method="delete" value="Delete" />
</s:form>
In struts.xml, I configured:
<package name="portal" namespace="/manage/" extends="struts-default">
<action name="home">
<result>/WEB-INF/jsp/manage/roles/test.jsp</result>
</action>
<action name="roles" class="struts2.actions.RoleAction" method="view">
<result name="success">/WEB-INF/jsp/manage/roles/viewRoles.jsp</result>
</action>
<action name="roles" class="struts2.actions.RoleAction" method="add">
<result name="input">/WEB-INF/jsp/manage/roles/addRole.jsp</result>
<result name="success">/WEB-INF/jsp/manage/roles/viewRoles.jsp</result>
</action>
Unfortunately, when I'm pressing View button, it's showing the result JSP of "add" method. Why?
Because you have two actions with the same name, roles.
How would you differentiate between... /roles and /roles?
They should have different names–different URLs. Since they're the same URL the last one configured will win; you're overwriting your own definitions. How about giving them different names, like /addrole and /viewrole, or namespacing them, like /roles/add and /roles/view, etc.?

Empty input value does not triggger #NotNull but it triggers #NotBlank

i have this entity:
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
#Entity
#XmlRootElement
#Table
public class Supplier extends AbstractEntity
{
#Column
#NotNull
private String name;
#Column
#NotBlank
private String representative;
...
}
and this jsf form:
<h:form>
<p:commandButton value="save" action="#{supplierController.create}" ajax="false"/>
<h:panelGrid columns="3" cellpadding="4">
<h:outputLabel value="#{bundle['Name']}" for="name"/>
<p:inputText id="name" value="#{supplierController.value.name}"/>
<p:message for="name"/>
<h:outputLabel value="#{bundle['Representative']}" for="representative"/>
<p:inputText id="representative" value="#{supplierController.value.representative}"/>
<p:message for="representative"/>
</h:panelGrid>
</h:form>
so, why is #NotBlank triggered and #NotNull is not?
i'm using mojarra-2.1.3_01 (ships hibernate-validator-4.2.0.Final?) under glassfish-3.1.1
Due to the nature of HTTP, empty request parameters are retrieved as empty string instead of null. So the #NotNull validator will never be triggered. It will only be triggered when the field is missing in the form or when it is disabled.
You can however configure JSF 2.x to interpret empty submitted values as null by adding the following context paramter to the web.xml:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
This way the #NotNull will be triggered for empty fields.

h:selectManyCheckbox with POJO problem

I tried some ways to achieve this simple thing :
Show a collection of POJOs in form of checkboxes
When i click on one of the checkboxes, a method should be executed
The method will be able to access the pojo/checkbox that was clicked
I tried to implement it this way :
<h:selectManyCheckbox id="groupUsers" layout="pageDirection" value="#{timetableBean.selectedUsers}">
<f:ajax listener="#{timetableBean.processUserEvents}" render="#this" />
<f:selectItems value="#{timetableBean.group.users}"
var="user" itemLabel="#{user.userId} - #{user.name}" itemValue="#{user}">
<f:attribute name="user" value="#{user}" />
</f:selectItems>
</h:selectManyCheckbox>
And my method is :
public void processUserEvents(AjaxBehaviorEvent e) {
User user = (User) e.getComponent().getAttributes().get("user");
...
}
The user fetched from e.getComponent().getAttributes().get("user"); is sadly null
In what way can i accomplish this ?
Thank you !

Resources