You can take a look at the wicket-bootstrap integration/components project
for inspiration (or use that).

But I don't have many issues with your code.

I'd probably use TypedPanel (or is it GenericPanel) as a base class, which
takes the model object's class as a parameter, to make sure the textfield
uses a type converter for the value. I guess you can switch out the editor
field based on the type to be a numeric/date/text field or even an enum
field.

the init method is unnecessary, there is already an 'onInitialize()` method
you can override and should use.

That's about it.

Martijn


On Mon, Jun 9, 2025 at 11:29 AM Joel Jabez <joeldave...@gmail.com> wrote:

> 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
>


-- 
Become a Wicket expert, learn from the best: http://wicketinaction.com

Reply via email to