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