Struts2 validator expression
Can a conditional expression be checked ?:
(invitation.id==null and (newText==null or newText.isEmpty()))
I tried several ways several times but didn't get it. This version works, but on the server side, and ignores if the prompt. Null is null or null ... any ideas ???:
<field name="newText">
<field-validator type="fieldexpression">
<param name="expression">!(invitation.id eq null and (newText eq null or newText.empty))</param>
<message>${getText("validation.required")}</message>
</field-validator>
</field>
http://struts.apache.org/release/2.2.x/docs/fieldexpression-validator.html
what is wrong with the expression ??? Thank!
source to share
Ok finally i used jquery client side validation width emulating struts2.
I don't like this for compatibility reasons, please, if there is a way to do this in a standard way, I would be grateful for any help.
Removing rack check:
<!--<field name="newText">
<field-validator type="fieldexpression">
<param name="expression">!((invitation.id==null or invitation.id.empty) and (newText==null or newText.empty))</param>
<message>${getText("validation.required")}</message>
</field-validator>
</field>-->
Adding js / jquery code:
function mySubmit() {
if ((invitationId==null || invitationId<0) && $("#text").val().trim()=='') {
var trStrutsFieldError='<tr errorfor="text">'+
'<td colspan="2" align="center" valign="top"><span class="errorMessage">'+strValidationRequired+'</span></td>'+
'</tr>';
$(trStrutsFieldError).insertBefore($("#text").parent().parent());
return false;
}
return true;
}
$(document).ready(function(){
$("form").submit(function(event) {
$.each($("form").find("span.errorMessage"), function() { /* <tr errorfor="title"><td colspan="2" align="center"><span classname="errorMessage" class="errorMessage">Campo requerido */
return false;
});
if ($(mySubmit).exists()) {
if (!mySubmit())
return false;
}
return true;
});
});
source to share
Your problem is when you are trying to check another field ( invitation.id
) in a field parser newText
(I don't think this is possible, but I'm not sure).
However, you can split it into two validators by raising a failure message, which is more true imho;
<field name="invitation.id">
<field-validator type="required">
<message>${getText("validation.invitation.id.required")}</message>
</field-validator>
</field>
<field name="newText">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[
newText != null && !newText.trim().empty())
]]>
</param>
<message>${getText("validation.newText.required")}</message>
</field-validator>
</field>
if you need to trim it, otherwise it might just become
<field name="invitation.id">
<field-validator type="required">
<message>${getText("validation.invitation.id.required")}</message>
</field-validator>
</field>
<field name="newText">
<field-validator type="requiredString">
<message>${getText("validation.newText.required")}</message>
</field-validator>
</field>
Note that required
for all non-text fields, a requiredString
is for text fields only.
Expression Validator is very powerful, but it should be used for more complex purposes:
for example, if you want to check a date against another dynamically read (via Getter) from your Action; let's say you have previously selected a user and you need to check the date from the page for a custom start and end time interval; but you also want to pass validation if no date is inserted because it is already being processed by the required validator (so you won't be raising two messages):
<field name="inputDate">
<field-validator type="required">
<message><![CDATA[ Input Date is mandatory ]]></message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[
inputDate==null ||
(inputDate >= chosenUser.startValidity
&&
inputDate <= chosenUser.EndValidity
)
]]>
</param>
<message>
<![CDATA[Input Date must be included in the User Validity interval
(from ${chosenUser.startValidity} to ${chosenUser.endValidity} )
]]>
</message>
</field-validator>
</field>
where chosenUser
is the user object from your action ( public User getChosenUser()
) and startValidity and endValidity are properties of the User ( public Date getEndValidity()
) object .
And as you can see, dynamic reading can be done in messages too ... that's how powerful the expression syntax is :)
source to share
First of all, the expression is not a valid parameter. If you really want client side validation, try using java script or jquery to validate. But you should also check the inputs on the server side as sometimes the user can disable javascript . So in this case you can use struts2 check.
for detailed explanation see http://viralpatel.net/blogs/struts2-validation-framework-tutorial-example/
source to share