Wicket- RequiredFieldValidator for AjaxEditableLabel

后端 未结 3 1275
孤独总比滥情好
孤独总比滥情好 2021-01-16 11:25

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 di

3条回答
  •  太阳男子
    2021-01-16 11:35

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

    }

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

提交回复
热议问题