gpt4 book ai didi

java - Wicket- AjaxEditableLabel 的 RequiredFieldValidator

转载 作者:行者123 更新时间:2023-11-29 03:57:22 27 4
gpt4 key购买 nike

起初我想说,虽然 RequiredFieldValidator 在 .NET 中使用,但我将这个术语用于 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);
}
}
}

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

最佳答案

你可以用一个行为来代替标签卡在身边

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

关于java - Wicket- AjaxEditableLabel 的 RequiredFieldValidator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5554717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com