Note that there are some explanatory texts on larger screens.

plurals
  1. POReusing Struts2 core validations in a custom validator
    text
    copied!<p>We are developing a new struts2 custom validator.</p> <p>This validator checks, if user account number has a valid format. For this validator, the account number must not be <code>null</code> and integer.</p> <p>There are two approaches:</p> <ol> <li><p>We develop the validator and assume that user use <code>@IntRangeFieldValidator</code> and <code>@RequiredFieldValidator</code> before using our validator</p> <pre><code>@IntRangeFieldValidator(type = ValidatorType.FIELD, min = "1", key = "validate.int.min") @RequiredFieldValidator(type = ValidatorType.SIMPLE, key = "validate.required") @CustomValidator(type = "accountNumber", message = "Price must be a number") public void setAccount(Integer accountNumber){} </code></pre> <p>In this way, the validation will be like as shown below</p> <pre><code>public class AccountNamuberValidator extends FieldValidatorSupport { String fieldName = getFieldName(); String val = (String) getFieldValue(fieldName, object); if (val == null &amp;&amp; (value instanceof String ) &amp;&amp;... ) { //we are assumption is not correct we just return return; } } </code></pre> <p>As you can see we do not deal ourselves with any validations that we assume to be done by integer validation or required validation. If any thing goes wrong we return and do not add any filed error.</p></li> <li><p>Next approach is that we develop a sophisticated account number validation which call integer validation and required validation before starting his own validation check. Here we can, some how, chain validation to each other.</p> <pre><code>public class AccountNamuberValidator extends FieldValidatorSupport { String fieldName = getFieldName(); String val = (String) getFieldValue(fieldName, object); //Call struts required validaitor //Call struts integer validaitor //Go on with account number validation } </code></pre> <p>So using this validator will be as:</p> <pre><code>@CustomValidator(type = "accountNumber", message = "Price must be a number") public void setAccount(Integer accountNumber){} </code></pre></li> </ol> <hr> <p>Now here are the questions:</p> <ul> <li>Is it possible to do second approch (I could not find how can I call Struts2 validators from my custom validator)? </li> <li>Which approach is better? (As I found that Strut uses first approach)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload