2012-01-16 35 views
2

私はdateTimeFieldとListViewを持つフォームを持っています。 リストビューは、次のようになります。Apache wicket:検証エラー後にモデルを更新する方法

final ListView<String> countryView = new ListView<String>("country", model.<List<String>>bind("country")) { 
      @Override 
      protected void populateItem(final ListItem<String> item) { 
        final String country = item.getModelObject(); 
        item.add(new ValidationDisplayableLabel("country", country, new String[] { modelPath })); 
        item.add(new AjaxLink("deleteLink") { 
         @Override 
         public void onClick(AjaxRequestTarget target) { 
          model.getObject().getCountry().remove(country); 
          if (issPeriod) { 
           addButton.setVisible(true); 
           countryTextField.setVisible(true); 
           findButton.setVisible(true); 
          } 
          if (target != null) 
           target.addComponent(rowPanel); 
         } 
        }); 
      } 
     }; 
     countryTextField = new ValidationDisplayableTextField("countryCodeInput", model.bind("oneCountry"), "job.country.value"); 

     **countryView.setReuseItems(true);** 
     rowPanel.add(countryView); 
     rowPanel.add(countryTextField); 
     addButton.setOutputMarkupPlaceholderTag(true); 
     rowPanel.add(addButton); 

そしてaddButtonは以下のようになります。

AjaxSubmitLink addButton = new AjaxSubmitLink(LinkNames.addCountry.toString()) { 
     @Override 
     public void onSubmit(AjaxRequestTarget target, Form form) { 
      if (model.getObject().getOneCountry() != null) 
       addCountry(); 
       if (target != null) 
        target.addComponent(rowPanel); 
       target.addComponent(form.getPage().get("feedbackPanel")); 
     } 
     @Override 
     protected void onError(AjaxRequestTarget target, Form<?> form) 
     { 
      onSubmit(target, form); 
     } 
    }; 

事は、私は私のDateTimeField型(例えば100に時間を設定する)失敗した場合、countryTextFieldに国コードを入力することであり、 addButtonを押すと、フィードバックパネルに検証メッセージが表示され、その時間範囲は間違っていますが、国を追加しません。私のモデルは更新されていないからです。おそらく手動で更新する方法がありますか?検証メッセージが表示されますが、国リストビューはまだ更新可能ですか?

フォーム全体の提出は他のボタン上にありますので、論理的にdateTimeFieldに検証エラーがあっても国を追加するのは正常です。

ありがとうございます!

P.S.私は同様の問題に関する多くの記事を読んだが、それらのほとんどは.setReuseItems(true)で解決されたが、私の場合はうまくいきません。

P.P.S Apacheの改札1.4.17

答えて

5

私は私が見つけた回避策は、特別なビジターを使用していた、私のプロジェクトで同様の問題に直面しました。入力された入力が無効であってもモデルを更新します。

public class VisitorUpdateModelWithoutValidation implements FormComponent.IVisitor { 

public Object formComponent(IFormVisitorParticipant formComponent) { 
     if (formComponent instanceof FormComponent) { 
      final FormComponent<?> formComponent1 = (FormComponent<?>) formComponent; 
      boolean required = formComponent1.isRequired(); 
      if (required) { 
       formComponent1.setRequired(false); 
      } 
      formComponent1.modelChanging(); 
      formComponent1.validate(); 
      formComponent1.updateModel(); 
      formComponent1.modelChanged(); 
      if (required) { 
       formComponent1.setRequired(true); 
      } 
     } 

     return Component.IVisitor.CONTINUE_TRAVERSAL; 
    } 
} 

は、単にあなたの行動のonSubmit方法でそれを使用する:この回答への更新としてgetForm().visitFormComponents(new VisitorUpdateModelWithoutValidation());

+1

私にとってこれは非常に壊れていて、あなたのコードではありませんが、フォームが検証されない限り、モデルは更新されません。 –

+0

私はあなたの意見は表示されません。バリデータの目的は、モデルオブジェクトに無効なデータが含まれないようにすることです。それらを更新することはあまり意味がありません。 –

+0

モデルの更新の制限は、フォームが送信される場合(通常は)重要です。このウィケットの挙動は、提出時にモデルの更新を制限するだけでなく、他のイベントでも更新されます。たとえば、他のコンポーネントイベントのMy Check Boxesモデルのアップデートでは、検証エラーの後で画面に現在のステータスが表示されることはありません。私にとっては、それは "邪悪な"ウィケットの行動です。 – will824

5

、ウィケット6に、あなたはフォームでのonError()をオーバーライドすることによって、これを達成することができます

@Override 
    protected void onError() { 
    super.onError(); 
    this.updateFormComponentModels(); 
} 
+0

これは私のために非常に良い仕事をした。ありがとう – will824

2

アップデートを対象とする前に、アップデート対象のフィールドにfield.clearInput()を発行することができます。

関連する問題