Hi so i recently, made some text field with some wrapper around with some
bootstrap class also. Im using this in alot of places, so for that i
created a component for it. Here is the java class and html page

public abstract class FormComponentTextField extends Panel
{
    private final String propertyExpression;
    private final Form<?> form;
    private final Class<?> resourceClazz;
    private final String displayName;

    protected FormComponentTextField(String id, Form<?> form)
    {

        this(id, form, (Class<?>) null);
    }

    protected FormComponentTextField(String id, Form<?> form, String
propertyExpression)
    {

        this(id, form, null, id, propertyExpression);
    }

    protected FormComponentTextField(String id, Form<?> form, Class<?>
resourceClazz)
    {
        this(id, form, resourceClazz, id, id);
    }

    protected FormComponentTextField(String id, Form<?> form, Class<?>
resourceClazz, String displayName, String propertyExpression)
    {
        super(id);
        this.form = form;
        this.resourceClazz = resourceClazz == null ?
this.getClass().getNestHost() : resourceClazz;
        this.displayName = displayName;
        this.propertyExpression = propertyExpression;
        init();
    }

    protected void init()
    {
        add(createTextFieldLabel());
        add(createTextField());
    }

    protected Label createTextFieldLabel()
    {
        Label label = new Label("fieldLabel", getLabel());
        if (isRequiredTextField()) {
            label.add(AttributeModifier.append("class", "mandatory-field"));
        }

        return label;
    }

    protected TextField<String> createTextField()
    {
        TextField<String> textField = isRequiredTextField() ? new
RequiredTextField<>("textField") :
                                              new TextField<>("textField");

        textField.setLabel(getLabel());
        textField.setOutputMarkupId(true);
        textField.add(getMaximumLength());
        textField.setDefaultModel(new
PropertyModel<>(form.getDefaultModel(), propertyExpression));
        getValidators().forEach(textField::add);

        return textField;
    }

    protected boolean isRequiredTextField()
    {
        return false;
    }

    protected Model<String> getLabel()
    {
        return Model.of(getPropertyValue(displayName));
    }

    public String getPropertyValue(String property)
    {
        return StringResourceUtils.getString(resourceClazz, property);
    }

    protected StringValidator getMaximumLength()
    {
        return StringValidator.maximumLength(255);
    }

    protected List<IValidator<String>> getValidators()
    {
        return List.of();
    }
}



<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"; xmlns:wicket="
http://wicket.apache.org";>
<body>
<wicket:panel>
  <div class="mb-3 row">
    <span class="col-sm-3 col-form-label" wicket:id="fieldLabel"></span>
    <div class="col-sm-9">
      <input class="form-control" type="text" wicket:id="textField"/>
    </div>
  </div>
</wicket:panel>
</body>
</html>


I wanna ask whether this is the best way to do, or is there a better way to
do it

Reply via email to