0

次のコンポーネントを含む、日付のリストを必要とする「新しいアイテム」フォームがあります。

  • 入力<rich:calendar>;
  • 選択した日付をバッキング Bean の に追加<a4j:commandButton>する。List<Date> chosenDates
  • 属性に設定されて<rich:dataTable>いるA ;valueList<Date> chosenDates
  • から日付を削除する<a4j:commandButton>dataTable 行ごとList<Date> chosenDates

フォーム送信時のリストのサイズ (作成プロセス)を検証する方法(JSF の検証フェーズ) は?chosenDates

RichFaces 4、JSF 2.1 (モハラ)。

4

3 に答える 3

1

JSFPhaseListenerを使用したよりクリーンなアプローチをお勧めします。検証が失敗した場合、JSF処理は他のフェーズのスキップを停止します。モデルの更新/呼び出しアクションフェーズではなく、検証フェーズでリストPhaseListenerサイズを検査するを作成します。このようなものを試してください

  1. 検証フェーズのフェーズリスナーを作成します

    public class TestPhaseListener implements PhaseListener {
    
       @Override
       public void afterPhase(PhaseEvent event) {
          throw new UnsupportedOperationException("Not supported yet.");
       }
    
       @Override
       public void beforePhase(PhaseEvent event) {
    
          if(event.getPhaseId().equals(PhaseId.PROCESS_VALIDATIONS)){
            FacesContext ctx = event.getFacesContext();
            YourBeanClass theBeanClass = ctx.getApplication().evaluateExpressionGet(ctx, "#{someBean}", YourNeanClass.class); //obtain a reference to the backing bean containing the list
    /*
       inspect the size of the list here and based on that throw the exception below
     */
           throw new ValidatorException(new FacesMessage("Too many dates","Too Many Dates"));
          }
       }
    
        @Override
        public PhaseId getPhaseId() {
           throw new UnsupportedOperationException("Not supported yet.");
        }
     } 
    
  2. 新しいリスナーをfaces_config.xmlファイルに登録します

    <lifecycle>
       <phase-listener>your.package.structure.TestPhaseListener</phase-listener>
    </lifecycle>
    

編集:コメントに基づいて、別の方法として、<f:event/>タグとpreValidateorpostValidateイベントを使用してコンポーネントのライフサイクルにフックすることができます(好みに応じて)

  1. コンポーネントへのリスナータグ

       <rich:dataTable>
           <f:event type="preValidate" listener="#{yourBean.listener}"/>
       </rich:dataTable>
    
  2. 定義したイベントごとに実行するリスナーメソッドをバッキングBeanで定義します。メソッドシグネチャは、タイプの引数を取る必要がありますComponentSystemEvent

        public void preCheck(ComponentSystemEvent evt){
           //You're in your backing bean so you can do pretty much whatever you want. I'd advise you mark the request as validation failed and queue FacesMessages. Obtain a reference to FacesContext and:
    
            facesContext.validationFailed();
    
    
         }
    
于 2013-01-26T15:47:14.083 に答える
0

次のよう #{yourBean.chosenDates.size()} にします。chosenDates リストを返す getChosenDates というゲッターがあるとします。

于 2013-01-25T14:08:52.733 に答える
0

あなたの「検証に関する懸念」について:

ValidateBean 内にメソッドを作成して、のリストを返すことができますValidationMessages。サンプルを以下に示します。コードで使用したものです。

public List<ValidationMessage> validate() {
    List<ValidationMessage> validations = new ArrayList<ValidationMessage>();
    int curSampleSize = sampleTable.getDataModel().getRowCount();

    if(getNumberOfSamples() != null) {
        size += getNumberOfSamples();
    } else {
        validations.add(new ValidationMessage("Please enter the no of samples to continue."));
        return validations;
    }           

    return validations;
}

ValidationMessages次に、送信時に、次のようにしているかどうかを確認できます。

List<ValidationMessage> errs = validate();

if(errs.size()>0) {
    FacesValidationUtil.addFacesMessages(errs);
    return null;
}

お役に立てれば!

于 2013-01-25T15:22:51.587 に答える