Rich Text in form

Hi there,

I am pretty new to vaadin and had a look at the following example
I tried to reproduce it but with the RichText in a form component.

In the original example, the inherited replaceComponent() method is used to toggle between Label (non editable) and RichTextArea (editable).

The Form class does not have a replaceComponent() method, is there a best practice to solve that ?

thanks in advance


I got one solution in mind but I don’t know if it is the most optimal.

You can do if-clauses in a FormFieldFactory that checks if the field should be a label or a richtextarea. The beanitem could have a field for this. Then you just have to trigger a run of the fieldfactory. One way is to give the beanitem to the form again after the change. I’d guess there are other ways but can’t come up with any right now.

Not the neatest solution.

If your form doesn’t contain that many components I would suggest ditching the form altogether and manually building the components.

Hi Jens,

tanks for your answer but I am not sure to get your point …

I agree I could put a test in the factory to decide what should be returned based on any variable but I cannot return a Label because it is not a Field.
Even by building the form myself (without a factory) I won’t be able to put a Label in there.

Do you have any other solution ?


Some questions for your form, (compare your needs to Samplers demo):
Do you need more flexible layout? Not just components stacked vertically. Maybe captions in custom positions, sections in form, pages perhaps…
Do you need different validation? Maybe showing more info next to invalid field or you want to disable some parts before some inputs are valid.
Do you need custom help inside form? Maybe showing help-buttons on form sections or just text as in “help within form”.
Do you need dialogs in form?
Do you need to hide irrelevant fields based on user roles perhaps or some input in other fields?

If you answered yes to any question above then I suggest you start with

“MyForm extends CustomComponent” and forget Form.

IMO: The built in Form component is too simple in most cases. It’s very suitable for just adding few textfields with save button but mostly that isn’t enough. I’ve used to writing a lot of custom forms, in every Vaadin project I’ve participated. I think it’s the best way of implementing forms. Customizing built-in Form takes more time.

I agree with Mauno that it in complex cases can be easier to skip the Form and just make a custom component. You need to do a bit more by hand in the beginning but have more flexibility. For a simpler case you can use the Form and create a read-only field. For instance a readonly TextField will be displayed as a label.