Directory

← Back

FormBuilders

FormBuilder - tool for build form through annotations in model class

Author

Rating

Popularity

<100

-Using annotation to build form -Simple validation -Set fields width for all form -Can build couple forms basic on single object -Can build form with relation -Disable selected fields -List and multiselect -Build any Object extends com.vaadin.ui.AbstractComponent

Extension use default vaadin itembinder to bind object

More deep documentation and help on github

Sample code

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);
		
		//Create formbuilder object with empty User object
		FormBuilder<User> fb = new FormBuilder<User>(User.class, new User());

		//Set field width for all fields
		fb.setFieldWidth(300, Unit.PIXELS);

		//Builds two separate forms through name
		HorizontalLayout hl = new HorizontalLayout(fb.buildForm(),fb.buildForm(FormName.COL2));
		layout.addComponent(hl);
		layout.addComponent(new Button("Save",e->saveItem(fb)));

		//Add external listener for field
		((TextField)fb.getField("street")).addValueChangeListener(e->eventChange(e));

		//Set disabled fields and rebuild form
		fb.setDisableField(Arrays.asList("city"));
		fb.refreshForm();
		
	}
	private void eventChange(ValueChangeEvent e) {
		Notification.show("Street was change");
	}

	private void saveItem(FormBuilder<User> fb) {
		if(fb.validAndCommit()){
			User saved = fb.getItemDataSource();
			Notification.show("Item was saved");
		}else{
			Notification.show("Fill form first");
		}
	}
public class User {

	public User(){
		this.group=new Group(); //every object in form can't be null
	}
	@FormField
	@Nullable(message="Can not be empty",value=false)
	private String name;
	@FormField
	@Regex(message="must contains rr",regex=".*rr.*")
	@Nullable
	private String surname;
	@FormField
	private int age;
	@FormField
	private Date brithday;
	@FormField("Another field name")
	private BigDecimal price;
	@FormField(name="Discription",type=FieldType.TEXTAREA)
	private String discription;
	@FormField(name="password",disable=true)
	private String password;
	
	@FormField(name="Street",formName=FormName.COL2)
	private String street;
	@FormField(name="City",formName=FormName.COL2)
	private String city;
	@FormField(name="Code",formName=FormName.COL2)
	private String code;
	
	@InnerForm
	private Group group;
	
	private Group group2;
	
	/**   Getters and Setters - must exists **/
}
public class Group {

	@FormField("group name")
	private String name;
	
	@FormField("description")
	private String description;

        public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}
public class Form{
       	@FormField(value="Group select",componentClass=GroupListSelectGroup.class)
	private Group group;

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}
}

public class GroupListSelectGroup extends ComboBox{

	public GroupListSelectGroup() {
		super("Grupy", createExampleDataSource());
		setItemCaptionPropertyId("NAME");
		
	}
	@SuppressWarnings("unchecked")
	private static Container createExampleDataSource() {
		Container ct = new IndexedContainer();
		ct.addContainerProperty("NAME", String.class, "");
		Group c = new Group(1l,"First");
		Item it = ct.addItem(c);
		it.getItemProperty("NAME").setValue(c.getDescription());
		Group cc = new Group(2l,"Second");
		it = ct.addItem(cc);
		it.getItemProperty("NAME").setValue(cc.getDescription());
		return ct;
	}
}
public class Form{
	@FormField(value="grupy",componentClass=GroupListSelectLong.class)
	private Set<Long> groupIds;
	public Set<Long> getGroupId() {
		return groupId;
	}

	public void setGroupId(Set<Long> groupId) {
		this.groupId = groupId;
	}
}

public class GroupListSelectLong extends ListSelect{

	public GroupListSelectLong() {
		super("Grupy", createDataSource());
		setItemCaptionPropertyId("NAME");
		setMultiSelect(true);
	}

	private static Container createDataSource() {
		Container ct = new IndexedContainer();
		ct.addContainerProperty("NAME", String.class, "");
		Group c = new Group(1l,"First");
		Item it = ct.addItem(c.getId());
		it.getItemProperty("NAME").setValue(c.getDescription());
		Group cc = new Group(2l,"Second");
		it = ct.addItem(cc.getId());
		it.getItemProperty("NAME").setValue(cc.getDescription());
		return ct;
	}
}
FormBuilder<User> fb = new FormBuilder<User>(User.class, new User());
layout.addComponent(fb.buildForm());

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

  • Add new construct to class FormBuilder with param boolean called superClass. This provides creating fields from parent class.
  • Fixed bug with creating InnerForm in layer deeper than 2
Released
2016-07-14
Maturity
STABLE
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.0+
Browser
Browser Independent

FormBuilders - Vaadin Add-on Directory

FormBuilder - tool for build form through annotations in model class FormBuilders - Vaadin Add-on Directory
-Using annotation to build form -Simple validation -Set fields width for all form -Can build couple forms basic on single object -Can build form with relation -Disable selected fields -List and multiselect -Build any Object extends com.vaadin.ui.AbstractComponent Extension use default vaadin itembinder to bind object More deep documentation and help on github
Online