2011-04-05 54 views
1

起初我想说的是,虽然RequiredFieldValidator在.NET中使用,但是我使用这个术语来指示wicket,因为我想表示标签(color:red和text:*),它将被显示当AjaxEditableLabel的编辑器将为空时,在AjaxEditableLabel旁边。我已经设置了AjaxEditableLabel.setRequired(true)并且它正在工作,即表单不能被提交。但是我无法跟踪AjaxEditableLabel旁边的红色星标。我到目前为止所做的是:Wicket-RequiredFieldValidator for AjaxEditableLabel

private class TaskTypeSettingsForm extends Form { 

    private static final long serialVersionUID = 10058L;   
    private FeedbackMessageFilter filter; 

    public TaskTypeSettingsForm(String id) { 
     super(id); 
     FeedbackPanel feedback = new FeedbackPanel("feedback"); 
     filter = new FeedbackMessageFilter(); 
     feedback.setFilter(filter); 
     add(feedback); 

     setOutputMarkupId(true);    
     final TaskTypeSettingsFormModel taskTypeSettingsFormModel = new TaskTypeSettingsFormModel(); 

     IModel model = new BoundCompoundPropertyModel(taskTypeSettingsFormModel); 
     setModel(model); 

     final WebMarkupContainer div = new WebMarkupContainer("div"); 
     div.setOutputMarkupId(true); 
     final ListView listView = new ListView("listView", new PropertyModel(taskTypeSettingsFormModel, "taskTypeList")) { 

      @Override 
      protected void populateItem(ListItem item) { 
       final String value = (String) item.getModelObject(); 
       final int index = item.getIndex(); 
       final Label star = new Label("star", "*");  
       //this label is always displaying, I need to 
       //display it when the editor is blank and hide when 
       //it contain valid text   
       star.setOutputMarkupId(true); 
       final AjaxEditableLabel label = new AjaxEditableLabel("value", new Model(value)) { 

        @Override 
        public void onSubmit(AjaxRequestTarget target) { 
         super.onSubmit(target); 
           //here I also try to get the editor 
           //and add a SimpleAttributeModifier 
           //with a javaScript for onBlur 
           //event, but that script is not 
           //working as I am not able to 
           //append that script to the 
           //editor's existing ajax 
         String input = (String) getModelObject(); 
         if (input != null) {         
          taskTypeSettingsFormModel.getTaskTypeList().set(index, input);        
         }             
        }              
       };     
       label.setRequired(true);  

       item.add(star); 
       label.setOutputMarkupId(true); 
       label.add(new SimpleAttributeModifier("style", "cursor: pointer; cursor: hand;")); 
       label.add(new AbstractValidator() { 

        @Override 
        protected void onValidate(IValidatable validatable) { 
         String value = (String) validatable.getValue(); 
         Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+$"); 
         Matcher matcher = pattern.matcher(value); 
         if (!matcher.matches()) { 
          error(validatable); 
         } 
        } 

        @Override 
        protected String resourceKey() {      
         return "task_type_settings_form.error.regexFailure"; 
        }  
       }); 
       item.add(label); 
       item.add(removeLink("removeLink", item));     
       item.add(moveUpLink("up", item)); 
       item.add(moveDownLink("down", item)); 
      } 
     }; 

     listView.setOutputMarkupId(true); 
     listView.setReuseItems(true); 
     div.add(listView); 

     //some code  
    } 

    @Override 
    protected void validate() { 
     filter.reset(); 
     super.validate(); 
    } 

    @Override 
    public void onSubmit() { 
     TaskTypeSettingsFormModel taskTypeSettingsFormModel = (TaskTypeSettingsFormModel) getModelObject(); 
     for (String str : taskTypeSettingsFormModel.getTaskTypeList()) { 
      System.out.println(str); 
     } 
    } 
} 

希望我能解释一下这种情况。任何有关这方面的信息对我都很有帮助。谢谢。

+0

仅供参考,您不需要包括“Wicket-”在你的标题;在这个网站上,标签可以达到这个目的。另外,我们通常不希望在这里使用“hello/thanks”这一行。特别是,Hellos在搜索结果中占用了预测空间。 – Pops 2011-04-06 16:10:44

+0

@ Lord Torgamus我会记住你的建议。我在标题中包含了Wicket,因为我在我的问题中使用了术语RequiredFieldValidator,这个术语与Java而不是ASP.NET相关。 – 2011-04-06 16:28:48

+1

我认为在每种可以想象的Web技术中都有一个RequiredFieldValidator(或类似的):-) – 2011-04-07 15:43:44

回答

3

而不是挂在周围的标签,你可以做到这一点的行为

public class RequiredStarBevaviour extends AbstractBehavior { 

@Override 
public void beforeRender(final Component component) { 
    super.beforeRender(component); 
    if (component instanceof FormComponent<?>) { 
     if (!((FormComponent<?>) component).checkRequired()) { 
      component.getResponse() 
        .write("<span class='redclass'>*</span>"); 
     } 
    } 
} 

}

这将每个组件渲染的时候运行,它会检查其形式组件,如果必要的检查不符合它将使明星。

编辑回应质疑:

final AjaxEditableLabel label = new AjaxEditableLabel("value", 
       new Model(value)) { 

      @Override 
      protected FormComponent newEditor(final MarkupContainer parent, 
        final String componentId, final IModel model) { 
       final FormComponent newEditor = super.newEditor(parent, 
         componentId, model); 
       newEditor.add(new RequiredStarBevaviour()); 
       return newEditor; 
      } 

      @Override 
      public void onSubmit(final AjaxRequestTarget target) { 
       super.onSubmit(target); 
       // here I also try to get the editor 
       // and add a SimpleAttributeModifier 
       // with a javaScript for onBlur 
       // event, but that script is not 
       // working as I am not able to 
       // append that script to the 
       // editor's existing ajax 
       final String input = (String) getModelObject(); 
       if (input != null) { 
        taskTypeSettingsFormModel.getTaskTypeList().set(index, 
          input); 
       } 
      } 
     }; 
+0

@Tnem谢谢。所以我可以将这种行为添加到AjaxEditableLabel ?.我想说的一件事是,我向该AjaxEditableLabel添加了AjaxFormComponentUpdatingBehavior,但它引发了异常,表明它只能添加到FormComponent,AjaxEditableLabel是FormComponent? – 2011-04-05 17:11:36

+0

@Tapas Bose不,这是一个面板,但它包含一个表单组件_editor_,因此您可以将该行为添加到'label.getEditor()' – Tnem 2011-04-05 17:18:06

+0

@Tnem AjaxEditableLabel.getEditor()是一种受保护的方法。不是吗?我怎样才能调用label.getEditor()? – 2011-04-05 17:24:25

0

尝试:

设置star标签初始化为隐藏:

star.setVisible(false); 

当表单提交,显示star基于input(添加到AjaxRequestTarget):

String input = (String) getModelObject(); // From your code 
star.setVisible("".equals(input)); 
target.add(star); // Or is it addComponent()? Can't remember :-S 
+0

感谢您的回复。在onSubmit方法中,如果输入为空或等于“”,则显示警报。但添加setRequired真正的AJ AJAXEditableLabel它停止工作。我无法获得空白TextField的getModelObject。 – 2011-04-05 16:24:51

+0

@Tapas Bose我无法解释,因为自从我上次在Wicket项目上工作以来已经有相当长的一段时间了。这是值得一试的,但我真的不记得内部的形式和验证行为,恐怕。 – jensgram 2011-04-06 06:26:58

+0

没问题,它解决了:)。 – 2011-04-06 16:21:05

1

@Tnem您的解决方案完美地工作。一个小调整我做什么我显示后,可能有助于未来的用户:

   AjaxEditableLabel taskTypeEditableLabel = new AjaxEditableLabel("taskTypeEditableLabel", new Model(value)) { 

        private static final long serialVersionUID = 10061L; 

        @Override 
        public void onSubmit(AjaxRequestTarget target) { 
         super.onSubmit(target); 
         String input = (String) getModelObject(); 
         if (input != null) {         
          taskTypeSettingsFormModel.getTaskTypeList().set(index, input);        
         }             
        }  

        @Override 
        protected FormComponent newEditor(MarkupContainer parent, String componentId, IModel model) { 
         FormComponent editor = super.newEditor(parent, componentId, model); 

         editor.add(new AbstractBehavior() {        

          private static final long serialVersionUID = 10062L; 

          @Override 
          public void beforeRender(final Component component) { 
           super.beforeRender(component); 
           if (component instanceof FormComponent) { 
            if (!((FormComponent) component).checkRequired()) { 
             component.getResponse().write("<span style='color: red; margin-right: 5px;'>*</span>"); 
            } 
           } 
          } 

          @Override 
          public void onComponentTag(Component component, ComponentTag tag) { 
           super.onComponentTag(component, tag); 
           if (component instanceof FormComponent) { 
            tag.put("style", "width: 400px"); 
            if (!((FormComponent) component).isValid()) {           
             tag.put("style", "width: 400px; border: 1px solid #CC2200;");           
            } 
           } 
          } 
         }); 

         editor.add(new AbstractValidator() { 

          private static final long serialVersionUID = 10063L; 

          @Override 
          protected void onValidate(IValidatable validatable) { 
           String value = (String) validatable.getValue(); 
           Pattern pattern = Pattern.compile("([A-Z]+)((([\\w\\s-//]*)[\\w&&[^_]]+)?)"); 
           Matcher matcher = pattern.matcher(value); 
           if (!matcher.matches()) { 
            error(validatable); 
           } 
          } 

          @Override 
          protected String resourceKey() {      
           return "task_type_settings_form.error.regexFailure"; 
          } 
         }); 
         editor.setRequired(true); 
         return editor; 
        } 

        @Override 
        protected Component newLabel(MarkupContainer parent, String componentId, IModel model) { 
         Component label = super.newLabel(parent, componentId, model); 
         label.add(new AbstractBehavior() { 

          private static final long serialVersionUID = 10064L; 

          @Override 
          public void onComponentTag(Component component, ComponentTag tag) { 
           super.onComponentTag(component, tag); 
           if (component instanceof Component) { 
            tag.put("style", "cursor: pointer; cursor: hand;"); 
           } 
          }        
         }); 

         return label; 
        } 

       };        
       taskTypeEditableLabel.setOutputMarkupId(true);