RValPerez
RValPerez

Reputation: 1

Struts2 How to parse a string to int in validator expression

I'm sorry, I've been researching for examples for hours but I haven't found one. I would like to add a validation rule in my page using this formula:

if((WorkedHours - RestHours) >= 6 && RestHours == 0.75) //returns error 2708
if((WorkedHours - RestHours) >= 8 && RestHours == 1)    //returns error 2709

I've already created something like this in my xxx-validation.xml:

<validator type="expression">
    <param name="expression"><![CDATA[((((workRequest.WorkStartHour + (workRequest.WorkStartMin/60)) + (workRequest.WorkEndHour + (workRequest.WorkEndMin/60))) - 
                              ((workRequest.RestStartHour + (workRequest.RestStartMin/60)) + (workRequest.RestEndHour + (workRequest.RestEndMin/60)))) >= 6 && 
                              ((workRequest.RestStartHour + (workRequest.RestStartMin/60)) + (workRequest.RestEndHour + (workRequest.RestEndMin/60))) == 0.75)]]>
    </param>
    <message key="ERR2708"/>
</validator>

But the "variables" (ex. workRequest.WorkStartHours, etc.) are of type String in my Data Transfer Object (DTO) file and I cannot perform the "+", "-", "/", ">=", and "==" operations properly if they are not of integer type.

Any help and suggestion would be greatly appreciated.

Upvotes: 0

Views: 735

Answers (1)

Pigueiras
Pigueiras

Reputation: 19356

If you have to do complex operations in validation, you can use a validate method in your action.

public class MyAction extends ActionSupport {
    ...

    public void validate() {
        if ( /* Your condition */) {
            addFieldError("userName", getText ( /* yourKey */);
        } 
    }

}

Official documentation about validate method: http://struts.apache.org/2.3.4/docs/form-validation.html

Upvotes: 2

Related Questions