Class StrutsHtmlConstraintProvider

java.lang.Object
org.apache.struts2.components.StrutsHtmlConstraintProvider
All Implemented Interfaces:
HtmlConstraintProvider

public class StrutsHtmlConstraintProvider extends Object implements HtmlConstraintProvider
Default HtmlConstraintProvider.

Governed by one rule: never false-reject. A constraint is emitted only when the browser cannot reject input the server would accept. In particular this implementation never sets or changes an input's type — switching a field to type="number" would reject 1234,50, which the framework's locale-aware conversion accepts in a comma-decimal locale, and the browsers' email/url grammars differ from the framework's validators. Range constraints are therefore emitted only on a control the developer already made numeric.

Since:
7.4.0
  • Constructor Details

    • StrutsHtmlConstraintProvider

      public StrutsHtmlConstraintProvider()
  • Method Details

    • constraintsFor

      public Map<String,String> constraintsFor(List<Validator> validators, HtmlControlType control, Object action, Object value)
      Specified by:
      constraintsFor in interface HtmlConstraintProvider
      Parameters:
      validators - the field's validators; may be null or empty
      control - the kind of control being rendered
      action - the object the field's validators run against, used to resolve i18n validator messages: the action, or the visited object for a field reached through a visitor validator; may be null
      value - the field's current value as the tag resolved it — what the page renders and, for a control that omits its parameter when left empty, what the server will validate on such a submit; may be null
      Returns:
      attribute name to value; never null, possibly empty
    • rejectsBlank

      protected boolean rejectsBlank(List<Validator> validators)
      Only a trimming requiredstring fails a whitespace-only value server-side; with trim=false it counts as non-empty and falls through to the other validators.
    • admitBlankInPattern

      protected void admitBlankInPattern(Map<String,String> attributes)
      RegexFieldValidator skips a value that trims to empty before it looks at trim, while the browser skips pattern only for the empty string. Unless another validator on the field rejects blank input, the pattern therefore gets a whitespace-only alternative so that a single space is not blocked client-side and accepted server-side.
    • addConstraints

      protected void addConstraints(Map<String,String> attributes, Validator validator, HtmlControlType control, Object value)
    • addRequiredString

      protected void addRequiredString(Map<String,String> attributes, HtmlControlType control)
      requiredstring fails on null, empty and (by default) blank, so the browser's required can only reject what the server would also reject. Safe on any text-entry control.
    • addRequiredField

      protected void addRequiredField(Map<String,String> attributes, RequiredFieldValidator validator, HtmlControlType control, Object value)
      required fails only on null, an empty array or an empty collection. A control that submits an empty string rather than omitting the parameter therefore passes server-side while the browser blocks it — an empty text input, a select with an empty-valued header option, and an unticked checkbox (CheckboxInterceptor substitutes "false") are all in that group. Only RADIO and FILE omit the parameter entirely when empty, so only they agree with the browser.

      An omitted parameter leaves the property at whatever it already holds, which is the value being rendered — a primitive's 0, or what prepare() loaded. The validator itself decides whether that value would fail, so the attribute is emitted only when the two sides agree on it.

    • addLength

      protected void addLength(Map<String,String> attributes, StringLengthFieldValidator validator, HtmlControlType control)
    • addPattern

      protected void addPattern(Map<String,String> attributes, RegexFieldValidator validator, HtmlControlType control)
    • addRange

      protected void addRange(Map<String,String> attributes, RangeValidatorSupport<?> validator, HtmlControlType control)
    • addDoubleRange

      protected void addDoubleRange(Map<String,String> attributes, DoubleRangeFieldValidator validator, HtmlControlType control)
    • addMessage

      protected void addMessage(Map<String,String> attributes, Validator validator, Object action)