myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Robinson" <andrew.rw.robin...@gmail.com>
Subject Re: required=true?
Date Sun, 15 Apr 2007 21:07:20 GMT
Required works as follows:

When a UIInput control is validated, after the value is converted it
checks to see if a value is required and if it has been set (usually
in the decode phase for almost all components). This means, that if
your rendered is true and required is true inside the
PROCESS_VALIDATORS phase, then it will process.

So, if your code is:

<h:inputText required="true" rendered="#{bean.something}" />
<h:selectBooleanCheckBox value="#{bean.something}" />

First render:
Something=true
Therefore, the text box is rendered, and the check box is checked

User un-checks the check box & submits the form (text is not set)
1) RESTORE_VIEW (view restored, nothing special here)
2) APPLY_REQUEST_VALUES
Submitted value of input text is set in decode
Submitted value of check box is set in decode (getSubmittedValue() ==
Boolean.TRUE)
3) PROCESS_VALIDATIONS
Value of text box is not set, but value is required, ValidationError
thrown here in the validateValue method of UIInput. Render response is
called, skipping update phase

The way to fix this is that you have to apply the update of the
checkbox *BEFORE* the validation of the text box. The problem is that
there is no built in way to do this (immediate would move the
validation up, but not the processUpdate call).

The optional validation framework was built to handle this:
http://wiki.apache.org/myfaces/OptionalValidationFramework

The other way is to use binding. With binding, you have access to the
code of the component, and can call methods on it. So:

<h:inputText required="true" rendered="#{bean.something}" />
<h:selectBooleanCheckbox value="#{bean.something}" binding="#{bean.checkbox}" />

public class Bean {
  private boolean something = true;
  private HtmlSelectBooleanCheckbox checkbox;
  public boolean isSomething() { return this.something; }
  public void setSomething(boolean something) { this.something = something; }
  public HtmlSelectBooleanCheckbox getCheckbox() {
    if (this.checkbox == null) {
      this.checkbox = new HtmlSelectBooleanCheckbox()
      {
        public void decode(FacesContext context) {
          super.decode();
          if (this.isValid())
            this.processValidators(context);
          if (this.isValid())
            this.processUpdates(context);
        }
      }
    }
  }
  ...
}

Yes this is messy, but should work.

The other messy way is to override the isRequired code in the text
box, to look up the checkbox and use the local value or submitted
value instead of the actual value of the control.



On 4/15/07, Bjørn T Johansen <btj@havleik.no> wrote:
> I have small problem and I was wondering how people get around this...
>
> I have a radiobutton with yes or no as the two choices that decides if a text field should
be rendered or not (choosing yes or no
> will submit the page)... Also if this text field is rendered, it is a mandatory field
and need some input..
>
> If I use required = true on the text field, then when the text field is rendered and
I choose the other choice of the
> radiobuttons (that would hide the text field again), I get an error saying that an input
is required in the text field (which is
> normally correct but not this time, because I am just trying to hide the text field again)
> If I use immediate = true, then the value of the radiobutton is not set and the text
field is never rendered...
>
>
> I guess this is a normal problem but not sure how to best solve this?
>
>
> Regards,
>
> BTJ
>
> --
> -----------------------------------------------------------------------------------------------
> Bjørn T Johansen
>
> btj@havleik.no
> -----------------------------------------------------------------------------------------------
> Someone wrote:
> "I understand that if you play a Windows CD backwards you hear strange Satanic messages"
> To which someone replied:
> "It's even worse than that; play it forwards and it installs Windows"
> -----------------------------------------------------------------------------------------------
>

Mime
View raw message