1

検証するユーザー名があるとします。この場合、検証が失敗し、エラー メッセージが表示されたときに、ユーザー名 outputText とユーザー名 inputText フィールドを赤色で表示する必要があります。

検証が失敗した場合にすべてのフィールドが影響を受けるように、これらすべてをパネルグループにバインドしようとしました。しかし、単に panelgroup を置くだけでは機能しません。

私のバッキング Bean バリデーター

public void emailValidate(FacesContext context,
        UIComponent componentToValidate,
        Object value)
        throws ValidatorException {


    String email = value.toString();


    if (!Validator.isEmailAddress(email))
    {
        FacesMessage message =
                new FacesMessage(FacesMessage.SEVERITY_ERROR,"Email","Please enter valid email address");
                throw new ValidatorException(message);
    }


}

私のJSF

<h:panelGroup>
<h:outputText value="Email"/>
<h:message for="emailInput/>
<h:inputText id="emailInput" value="#{mybean.email}" validator="#{mybean.emailValidate}"/>
</h:panelGroup>
4

2 に答える 2

10

binding属性を介して入力コンポーネントをビューにバインドします。UIInputELのコンポーネント参照として利用できるようになるのでUIInput#isValid()styleClass属性で使用できます。

<h:outputLabel for="emailInput" value="Email" 
    styleClass="#{emailInput.valid ? '' : 'error'}" />

<h:inputText id="emailInput" binding="#{emailInput}" ... 
    styleClass="#{emailInput.valid ? '' : 'error'}" />

(ラベルを実際のラベルに修正したことに注意してください。cubbuk の回答で示唆されているように、Bean プロパティをまったく作成する必要がないことにも注意してください)

はい、これにより、ビューにかなりの数の非DRYボイラープレート コードが生成される可能性があります。これは、フェーズ リスナーまたはシステム イベント リスナーで抽象化できます。すべてのジョブを透過的に実行するOmniFaces コンポーネントを使用することもできます。ライブデモ<o:highlight>もご覧ください。

于 2013-01-22T11:50:50.273 に答える
1

バッキング Bean で検証が失敗したことを表すフィールドが必要です。そして、その検証フィールドの条件に従って、以下に示すように uiComponents の css を変更できます。

public void emailValidate(FacesContext context,
                UIComponent componentToValidate,
                Object value)
                throws ValidatorException
    {
       String email = value.toString();
       if (!Validator.isEmailAddress(email))
            {
                FacesMessage message =
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Email", "Please enter valid email address");
                validationFailed = true;
                throw new ValidatorException(message);
            }
    }

public Boolean getValidationFailed()
{
    return validationFailed;
}

<style>
   .errorClass
   {
       background-color: red;
   }
   </style>
   <h:panelGroup>
      <h:outputText value="Email" styleClass="#{ozetPageBean.validationFailed ? 'errorClass' : ''}"/>
      <h:message for="emailInput"/>
      <h:inputText id="emailInput" 
                   value="#{ozetPageBean.email}" 
                   validator="#{ozetPageBean.emailValidate}"
                   styleClass="#{ozetPageBean.validationFailed ? 'errorClass' : ''}"/>
   </h:panelGroup>
于 2013-01-22T10:16:09.963 に答える