Wicket-AjaxEditableLabel 的必需字段验证器

发布于 2024-10-30 20:16:38 字数 4565 浏览 0 评论 0原文

首先我想说,虽然在.NET中使用了RequiredFieldValidator,但我使用这个术语来表示wicket,因为我想表示一个标签(颜色:红色和文本:*),当编辑AjaxEditableLabel时,它将显示在AjaxEditableLabel旁边将是空白的。我已经设置了 AjaxEditableLabel.setRequired(true) 并且它正在工作,即表单无法提交。但我无法跟踪 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);
        }
    }
}

希望我能解释一下这个场景。任何与此相关的信息都会对我非常有帮助。谢谢。

At first I want to say, although RequiredFieldValidator is used in .NET but I use this term for wicket as I want to mean a Label (color: red and text: *) which will be be displayed beside AjaxEditableLabel when the editor of the AjaxEditableLabel will be blank. I have set AjaxEditableLabel.setRequired(true) and it is working, i.e., the Form cannot be submitted. But I am not able to track that red star Label beside the AjaxEditableLabel. What I did so far is :

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);
        }
    }
}

Hope I can explain the scenario. Any information regarding this will be very helpful to me. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时间海 2024-11-06 20:16:38

您可以使用行为来代替标签挂在周围,

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);
                }
            }
        };

Instead of having a label hanging around, you could do it with a behaviour

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>");
        }
    }
}

}

This will run each time the component is rendered, it will check if its a form component and if the required check is not met it will render the star.

EDIT response to question:

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);
                }
            }
        };
罪歌 2024-11-06 20:16:38

@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);

@Tnem your solution worked perfectly. After a little tweak what I did I am showing, it might be helpful to future users:

                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);
碍人泪离人颜 2024-11-06 20:16:38

尝试:

在 init 上将 star 标签设置为隐藏:

star.setVisible(false);

提交表单时,根据 input 显示 star (并添加到 AjaxRequestTarget):

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

Try:

Set the star label as hidden on init:

star.setVisible(false);

When the form is submitted, show star based on input (and add to AjaxRequestTarget):

String input = (String) getModelObject(); // From your code
star.setVisible("".equals(input));
target.add(star); // Or is it addComponent()? Can't remember :-S
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文