1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package tonPackage ;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;
public class ValidMultiFields
{
public static boolean validateMultiFields(Object bean,ValidatorAction validatorAction,Field field,ActionMessages errors,HttpServletRequest request)
{
boolean isValid = true;
// Récupération des valeurs des champs du formulaire
String field4 = ValidatorUtils.getValueAsString(bean, field.getProperty());
String field1 = ValidatorUtils.getValueAsString(bean, "XFab1");
String field2 = ValidatorUtils.getValueAsString(bean, "XFab2");
String field3 = ValidatorUtils.getValueAsString(bean, "XSav1");
// Règles de validation
if ( (field1 != null && field1.length() != 0 )
& (field2 != null && field2.length() != 0 )
& (field3 == null || field3.length() == 0 )
& (field4 == null || field4.length() == 0 )
)
{
isValid=true;
}
else if ( (field3 != null && field3.length() != 0 )
& (field4 != null && field4.length() != 0 )
& (field1 == null || field1.length() == 0 )
& (field2 == null || field2.length() == 0 )
)
{
isValid=true ;
}
else
{
// Erreur de validation
isValid=false;
validatorAction.setMsg("errors.validMultiFields");
errors.add("XSav2", Resources.getActionMessage(request, validatorAction, field));
}
return isValid;
}
} |
Partager