use of com.vaadin.data.validator.StringLengthValidator in project Activiti by Activiti.
the class NewUserPopupWindow method initInputFields.
protected void initInputFields() {
// Input fields
form.addField("id", new TextField(i18nManager.getMessage(Messages.USER_ID)));
// Set id field to required
form.getField("id").setRequired(true);
form.getField("id").setRequiredError(i18nManager.getMessage(Messages.USER_ID_REQUIRED));
form.getField("id").focus();
// Set id field to be unique
form.getField("id").addValidator(new Validator() {
public void validate(Object value) throws InvalidValueException {
if (!isValid(value)) {
throw new InvalidValueException(i18nManager.getMessage(Messages.USER_ID_UNIQUE));
}
}
public boolean isValid(Object value) {
if (value != null) {
return identityService.createUserQuery().userId(value.toString()).singleResult() == null;
}
return false;
}
});
// Password is required
form.addField("password", new PasswordField(i18nManager.getMessage(Messages.USER_PASSWORD)));
form.getField("password").setRequired(true);
form.getField("password").setRequiredError(i18nManager.getMessage(Messages.USER_PASSWORD_REQUIRED));
// Password must be at least 5 characters
StringLengthValidator passwordLengthValidator = new StringLengthValidator(i18nManager.getMessage(Messages.USER_PASSWORD_MIN_LENGTH, 5), 5, -1, false);
form.getField("password").addValidator(passwordLengthValidator);
form.addField("firstName", new TextField(i18nManager.getMessage(Messages.USER_FIRSTNAME)));
form.addField("lastName", new TextField(i18nManager.getMessage(Messages.USER_LASTNAME)));
form.addField("email", new TextField(i18nManager.getMessage(Messages.USER_EMAIL)));
}
use of com.vaadin.data.validator.StringLengthValidator in project opennms by OpenNMS.
the class MibObjFieldFactory method createField.
/* (non-Javadoc)
* @see com.vaadin.ui.TableFieldFactory#createField(com.vaadin.data.Container, java.lang.Object, java.lang.Object, com.vaadin.ui.Component)
*/
@Override
public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
if (propertyId.equals("oid")) {
final TextField field = new TextField();
field.setSizeFull();
field.setRequired(true);
field.setImmediate(true);
field.addValidator(new RegexpValidator("^\\.[.\\d]+$", "Invalid OID {0}"));
return field;
}
if (propertyId.equals("instance")) {
final ComboBox field = new ComboBox();
field.setSizeFull();
field.setRequired(true);
field.setImmediate(true);
field.setNullSelectionAllowed(false);
field.setNewItemsAllowed(true);
field.setNewItemHandler(new NewItemHandler() {
@Override
public void addNewItem(String newItemCaption) {
if (!field.containsId(newItemCaption)) {
field.addItem(newItemCaption);
field.setValue(newItemCaption);
}
}
});
field.addItem("0");
field.addItem("ifIndex");
for (String rt : resourceTypes) {
field.addItem(rt);
}
return field;
}
if (propertyId.equals("alias")) {
final TextField field = new TextField();
field.setSizeFull();
field.setRequired(true);
field.setImmediate(true);
field.addValidator(new StringLengthValidator("Invalid alias. It should not contain more than 19 characters.", 1, 19, false));
return field;
}
if (propertyId.equals("type")) {
final TextField field = new TextField();
field.setSizeFull();
field.setRequired(true);
field.setImmediate(true);
field.addValidator(new // Based on NumericAttributeType and StringAttributeType
RegexpValidator(// Based on NumericAttributeType and StringAttributeType
"^(?i)(counter|gauge|timeticks|integer|octetstring|string)?\\d*$", "Invalid type {0}. Valid types are: counter, gauge, timeticks, integer, octetstring, string"));
return field;
}
return null;
}
use of com.vaadin.data.validator.StringLengthValidator in project tutorials by eugenp.
the class VaadinUI method init.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout verticalLayout = new VerticalLayout();
verticalLayout.setSpacing(true);
verticalLayout.setMargin(true);
final GridLayout gridLayout = new GridLayout(3, 2);
gridLayout.setSpacing(true);
gridLayout.setMargin(true);
final HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.setSpacing(true);
horizontalLayout.setMargin(true);
final FormLayout formLayout = new FormLayout();
formLayout.setSpacing(true);
formLayout.setMargin(true);
final GridLayout buttonLayout = new GridLayout(3, 5);
buttonLayout.setMargin(true);
buttonLayout.setSpacing(true);
final Label label = new Label();
label.setId("Label");
label.setValue("Label Value");
label.setCaption("Label");
gridLayout.addComponent(label);
final Link link = new Link("Baeldung", new ExternalResource("http://www.baeldung.com/"));
link.setId("Link");
link.setTargetName("_blank");
gridLayout.addComponent(link);
final TextField textField = new TextField();
textField.setId("TextField");
textField.setCaption("TextField:");
textField.setValue("TextField Value");
textField.setIcon(FontAwesome.USER);
gridLayout.addComponent(textField);
final TextArea textArea = new TextArea();
textArea.setCaption("TextArea");
textArea.setId("TextArea");
textArea.setValue("TextArea Value");
gridLayout.addComponent(textArea);
final DateField dateField = new DateField("DateField", new Date(0));
dateField.setId("DateField");
gridLayout.addComponent(dateField);
final PasswordField passwordField = new PasswordField();
passwordField.setId("PasswordField");
passwordField.setCaption("PasswordField:");
passwordField.setValue("password");
gridLayout.addComponent(passwordField);
final RichTextArea richTextArea = new RichTextArea();
richTextArea.setCaption("Rich Text Area");
richTextArea.setValue("<h1>RichTextArea</h1>");
richTextArea.setSizeFull();
Panel richTextPanel = new Panel();
richTextPanel.setContent(richTextArea);
final InlineDateField inlineDateField = new InlineDateField();
inlineDateField.setValue(new Date(0));
inlineDateField.setCaption("Inline Date Field");
horizontalLayout.addComponent(inlineDateField);
Button normalButton = new Button("Normal Button");
normalButton.setId("NormalButton");
normalButton.addClickListener(e -> {
label.setValue("CLICK");
});
buttonLayout.addComponent(normalButton);
Button tinyButton = new Button("Tiny Button");
tinyButton.addStyleName("tiny");
buttonLayout.addComponent(tinyButton);
Button smallButton = new Button("Small Button");
smallButton.addStyleName("small");
buttonLayout.addComponent(smallButton);
Button largeButton = new Button("Large Button");
largeButton.addStyleName("large");
buttonLayout.addComponent(largeButton);
Button hugeButton = new Button("Huge Button");
hugeButton.addStyleName("huge");
buttonLayout.addComponent(hugeButton);
Button disabledButton = new Button("Disabled Button");
disabledButton.setDescription("This button cannot be clicked");
disabledButton.setEnabled(false);
buttonLayout.addComponent(disabledButton);
Button dangerButton = new Button("Danger Button");
dangerButton.addStyleName("danger");
buttonLayout.addComponent(dangerButton);
Button friendlyButton = new Button("Friendly Button");
friendlyButton.addStyleName("friendly");
buttonLayout.addComponent(friendlyButton);
Button primaryButton = new Button("Primary Button");
primaryButton.addStyleName("primary");
buttonLayout.addComponent(primaryButton);
NativeButton nativeButton = new NativeButton("Native Button");
buttonLayout.addComponent(nativeButton);
Button iconButton = new Button("Icon Button");
iconButton.setIcon(FontAwesome.ALIGN_LEFT);
buttonLayout.addComponent(iconButton);
Button borderlessButton = new Button("BorderLess Button");
borderlessButton.addStyleName("borderless");
buttonLayout.addComponent(borderlessButton);
Button linkButton = new Button("Link Button");
linkButton.addStyleName("link");
buttonLayout.addComponent(linkButton);
Button quietButton = new Button("Quiet Button");
quietButton.addStyleName("quiet");
buttonLayout.addComponent(quietButton);
horizontalLayout.addComponent(buttonLayout);
final CheckBox checkbox = new CheckBox("CheckBox");
checkbox.setValue(true);
checkbox.addValueChangeListener(e -> checkbox.setValue(!checkbox.getValue()));
formLayout.addComponent(checkbox);
List<String> numbers = new ArrayList<String>();
numbers.add("One");
numbers.add("Ten");
numbers.add("Eleven");
ComboBox comboBox = new ComboBox("ComboBox");
comboBox.addItems(numbers);
formLayout.addComponent(comboBox);
ListSelect listSelect = new ListSelect("ListSelect");
listSelect.addItems(numbers);
listSelect.setRows(2);
formLayout.addComponent(listSelect);
NativeSelect nativeSelect = new NativeSelect("NativeSelect");
nativeSelect.addItems(numbers);
formLayout.addComponent(nativeSelect);
TwinColSelect twinColSelect = new TwinColSelect("TwinColSelect");
twinColSelect.addItems(numbers);
Grid grid = new Grid("Grid");
grid.setColumns("Column1", "Column2", "Column3");
grid.addRow("Item1", "Item2", "Item3");
grid.addRow("Item4", "Item5", "Item6");
Panel panel = new Panel("Panel");
panel.setContent(grid);
panel.setSizeUndefined();
Panel serverPushPanel = new Panel("Server Push");
FormLayout timeLayout = new FormLayout();
timeLayout.setSpacing(true);
timeLayout.setMargin(true);
currentTime = new Label("No TIME...");
timeLayout.addComponent(currentTime);
serverPushPanel.setContent(timeLayout);
serverPushPanel.setSizeUndefined();
ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
currentTime.setValue("Current Time : " + Instant.now());
};
scheduleExecutor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS);
FormLayout dataBindingLayout = new FormLayout();
dataBindingLayout.setSpacing(true);
dataBindingLayout.setMargin(true);
BindData bindData = new BindData("BindData");
BeanFieldGroup beanFieldGroup = new BeanFieldGroup(BindData.class);
beanFieldGroup.setItemDataSource(bindData);
TextField bindedTextField = (TextField) beanFieldGroup.buildAndBind("BindName", "bindName");
bindedTextField.setWidth("250px");
dataBindingLayout.addComponent(bindedTextField);
FormLayout validatorLayout = new FormLayout();
validatorLayout.setSpacing(true);
validatorLayout.setMargin(true);
HorizontalLayout textValidatorLayout = new HorizontalLayout();
textValidatorLayout.setSpacing(true);
textValidatorLayout.setMargin(true);
TextField stringValidator = new TextField();
stringValidator.setNullSettingAllowed(true);
stringValidator.setNullRepresentation("");
stringValidator.addValidator(new StringLengthValidator("String must have 2-5 characters lenght", 2, 5, true));
stringValidator.setValidationVisible(false);
textValidatorLayout.addComponent(stringValidator);
Button buttonStringValidator = new Button("Validate String");
buttonStringValidator.addClickListener(e -> {
try {
stringValidator.setValidationVisible(false);
stringValidator.validate();
} catch (InvalidValueException err) {
stringValidator.setValidationVisible(true);
}
});
textValidatorLayout.addComponent(buttonStringValidator);
validatorLayout.addComponent(textValidatorLayout);
verticalLayout.addComponent(gridLayout);
verticalLayout.addComponent(richTextPanel);
verticalLayout.addComponent(horizontalLayout);
verticalLayout.addComponent(formLayout);
verticalLayout.addComponent(twinColSelect);
verticalLayout.addComponent(panel);
verticalLayout.addComponent(serverPushPanel);
verticalLayout.addComponent(dataBindingLayout);
verticalLayout.addComponent(validatorLayout);
setContent(verticalLayout);
}
use of com.vaadin.data.validator.StringLengthValidator in project vaadin-samples by xpoft.
the class SimpleForm method PostConstruct.
@PostConstruct
public void PostConstruct() {
User user = new User();
setCaption("Simple form");
setSpacing(true);
TextField name = new TextField("Name");
name.setRequired(true);
name.setRequiredError("Please enter a Name!");
name.setWidth(COMMON_FIELD_WIDTH);
name.addValidator(new StringLengthValidator("It must be 3-25 characters", 3, 25, false));
name.setNullRepresentation("");
name.setImmediate(true);
TextField email = new TextField("Email:");
email.setRequired(true);
email.setRequiredError("Please enter a Email");
email.setWidth(COMMON_FIELD_WIDTH);
email.addValidator(new EmailValidator("Not valid email"));
email.setNullRepresentation("");
email.setImmediate(true);
final BeanFieldGroup<User> fieldGroup = new BeanFieldGroup<User>(User.class);
fieldGroup.setItemDataSource(new BeanItem<User>(user));
fieldGroup.bind(name, "name");
fieldGroup.bind(email, "email");
// The cancel / apply buttons
HorizontalLayout buttons = new HorizontalLayout();
buttons.setSpacing(true);
Button discardChanges = new Button("Reset", new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
fieldGroup.discard();
}
});
buttons.addComponent(discardChanges);
buttons.setComponentAlignment(discardChanges, Alignment.MIDDLE_LEFT);
Button apply = new Button("Save", new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
try {
fieldGroup.commit();
Notification.show("OK");
} catch (Exception e) {
Notification.show("Error: " + e.getMessage(), Notification.Type.WARNING_MESSAGE);
}
}
});
buttons.addComponent(apply);
addComponent(name);
addComponent(email);
addComponent(buttons);
}
Aggregations