public <PT> BindingBuilder<T, PT> applyConfiguration(BindingBuilder<T, PT> bindingBuilder, FieldDefinition<PT> definition, Object... parameters) { if (definition.isRequired()) { bindingBuilder.asRequired(definition.getRequiredErrorMessage()); } bindingBuilder = definition.getValidators().stream() .map(this::newValidator) .reduce(bindingBuilder, BindingBuilder::withValidator, defaultCombiner()); final Converter converter = newConverter(definition, bindingBuilder.getField(), parameters); if (converter != null) { bindingBuilder = bindingBuilder.withConverter(converter); final Class<? extends FieldDefinition> definitionType = definition.getClass(); bindingBuilder = applyTextFieldNullValueRepresentationWorkaround(bindingBuilder, definitionType); } final PT fieldValue = (PT) bindingBuilder.getField().getValue(); if (converter == null && fieldValue != null && !definition.getType().isInstance(fieldValue)) { log.warn(String.format("Type mismatch for field named [%s]. Configured type is [%s] but bound component value type is [%s] and no suitable converter has been found. Please check your configuration.", definition.getName(), definition.getType(), fieldValue.getClass())); } Optional.ofNullable(definition.getDefaultValue()).ifPresent(bindingBuilder::withNullRepresentation); return bindingBuilder; }
@Override protected Component buildAndBindCustomComponent(String propertyId) { if ("id".equals(propertyId)) { TextField id = new TextField(getCaptionFromId(propertyId)); List<Validator> validators = getFieldFactory().getValidators(propertyId); Binder<SysConfig> binder = getBinder(); Binder.BindingBuilder builder = binder.forField(id) .asRequired(getApp().getMessage("errorMessage.req", StringUtils.capitalize(getCaptionFromId(propertyId)))) .withNullRepresentation("") .withConverter(userInputValue -> userInputValue , toPresentation -> removePrefix(toPresentation)); validators.forEach(builder::withValidator); builder.bind(propertyId); return id; } return null; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void setEditorBinding(P property, Column column, HasValue editor, boolean readOnly, PropertyColumn<?, ?> pc) { BindingBuilder builder = getGrid().getEditor().getBinder().forField(editor); if (pc.isRequired()) { final Localizable requiredMessage = (pc.getRequiredMessage() != null) ? pc.getRequiredMessage() : RequiredInputValidator.DEFAULT_REQUIRED_ERROR; builder.asRequired(context -> { return LocalizationContext.translate(requiredMessage, true); }); } // default validators getDefaultPropertyValidators(property).forEach(v -> builder.withValidator(new ValidatorWrapper<>(v))); // validators pc.getValidators().forEach(v -> builder.withValidator(v)); // bind column.setEditorBinding(builder.bind(getColumnId(property))); // set editable if not read-only column.setEditable(!readOnly); }
return ValidationResult.ok(); }) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(layer.getCaption()))) .bind("account"); binder.forField(layer) .withConverter(new ShortToLayerConverter(journal)) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(layer.getCaption()))) .bind("layer"); binder.forField(detail) .bind("tags"); binder.forField(amount) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(amount.getCaption()))) .withConverter(new StringToBigDecimalConverter(app.getMessage("errorMessage.invalidAmount"))) .withValidator(new BigDecimalRangeValidator(app.getMessage("errorMessage.invalidAmount"), new BigDecimal("1"), new BigDecimal("99999999999999")))
.asRequired(getApp().getMessage("errorMessage.req", currentPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getCurrentPasswordMatchValidator()) .bind(string->string,null); newPasswordField.setWidth("80%"); passwordBinder.forField(newPasswordField) .asRequired(getApp().getMessage("errorMessage.req",newPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getNewPasswordNotUsedValidator()) .bind(string->string,null); repeatPasswordField.setWidth("80%"); passwordBinder.forField(repeatPasswordField) .asRequired(getApp().getMessage("errorMessage.req", repeatPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getPasswordsMatchValidator(newPasswordField)) .bind(string->string,null);
/** * onstructs the entity edit form for the data entity * @param beanType The class of the data entity * @param idIsEditable Whether the id is editable */ public AbstractEntityEditForm( final Class<T> beanType, final boolean idIsEditable) { this.binder = new Binder<>(beanType); if (idIsEditable) { this.binder.forField(idField) //.withConverter(new StringToLongConverter("Unable to convert id")) .asRequired("The id field is required.") .bind("id"); } else { this.binder.forField(idField) //.withConverter(new StringToLongConverter("Unable to convert id")) .bind("id"); } this.binder.addStatusChangeListener(this::onBinderStatusChange); setSpacing(true); setMargin(false); setSizeFull(); Responsive.makeResponsive(this); idField.setReadOnly(!idIsEditable); idField.setWidth(100.0f, Unit.PERCENTAGE); }
/** * Sets the field to be required. This means two things: * <ol> * <li>the required indicator will be displayed for this field</li> * <li>the field value is validated for not being empty, i.e. that the * field's value is not equal to what {@link HasValue#getEmptyValue()} * returns</li> * </ol> * <p> * For localizing the error message, use * {@link #asRequired(ErrorMessageProvider)}. * * @see #asRequired(ErrorMessageProvider) * @see HasValue#setRequiredIndicatorVisible(boolean) * @see HasValue#isEmpty() * @param errorMessage * the error message to show for the invalid value * @return this binding, for chaining */ public default BindingBuilder<BEAN, TARGET> asRequired( String errorMessage) { return asRequired(context -> errorMessage); }
/** * Sets the field to be required. This means two things: * <ol> * <li>the required indicator will be displayed for this field</li> * <li>the field value is validated for not being empty, i.e. that the * field's value is not equal to what {@link HasValue#getEmptyValue()} * returns</li> * </ol> * <p> * For setting an error message, use {@link #asRequired(String)}. * <p> * For localizing the error message, use * {@link #asRequired(ErrorMessageProvider)}. * * @see #asRequired(String) * @see #asRequired(ErrorMessageProvider) * @see HasValue#setRequiredIndicatorVisible(boolean) * @see HasValue#isEmpty() * @return this binding, for chaining * @since 8.2 */ public default BindingBuilder<BEAN, TARGET> asRequired() { return asRequired(context -> ""); }