/** * Creates a new binding for the given field. The returned builder may be * further configured before invoking {@link #bindInstanceFields(Object)}. * Unlike with the {@link #forField(HasValue)} method, no explicit call to * {@link BindingBuilder#bind(String)} is needed to complete this binding in * the case that the name of the field matches a field name found in the * bean. * * @param <FIELDVALUE> * the value type of the field * @param field * the field to be bound, not null * @return the new binding builder * * @see #forField(HasValue) * @see #bindInstanceFields(Object) */ public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forMemberField( HasValue<FIELDVALUE> field) { incompleteMemberFieldBindings.put(field, null); return forField(field); }
@Override public boolean save() { if (isOpen() && isBuffered()) { binder.validate(); if (binder.writeBeanIfValid(edited)) { refresh(edited); eventRouter.fireEvent(new EditorSaveEvent<>(this, edited)); return true; } } return false; }
/** * Creates a binder using a custom {@link PropertySet} implementation for * finding and resolving property names for * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and * {@link BindingBuilder#bind(String)}. * <p> * This functionality is provided as static method instead of as a public * constructor in order to make it possible to use a custom property set * without creating a subclass while still leaving the public constructors * focused on the common use cases. * * @see Binder#Binder() * @see Binder#Binder(Class) * * @param propertySet * the property set implementation to use, not <code>null</code>. * @return a new binder using the provided property set, not * <code>null</code> */ public static <BEAN> Binder<BEAN> withPropertySet( PropertySet<BEAN> propertySet) { return new Binder<>(propertySet); }
/** * Informs the Binder that a value in Binding was changed. This method will * trigger validating and writing of the whole bean if using * {@link #setBean(Object)}. If using {@link #readBean(Object)} only the * field validation is run. * * @param binding * the binding whose value has been changed * @param event * the value change event * @since 8.2 */ protected void handleFieldValueChange(Binding<BEAN, ?> binding, ValueChangeEvent<?> event) { changedBindings.add(binding); if (getBean() != null) { doWriteIfValid(getBean(), changedBindings); } else { binding.validate(); } }
/** * Clear all the bound fields for this binder. */ private void clearFields() { bindings.forEach(binding -> { binding.getField().clear(); clearError(binding.getField()); }); if (hasChanges()) { fireStatusChangeEvent(false); } changedBindings.clear(); }
if (getBean() == null && !validators.isEmpty()) { throw new IllegalStateException("Cannot validate binder: " + "bean level validators have been configured " + "but no bean is currently set"); List<BindingValidationStatus<?>> bindingStatuses = validateBindings(); bindingStatuses, Collections.emptyList()); } else { Map<Binding<BEAN, ?>, Object> beanState = getBeanState(getBean(), changedBindings); changedBindings .forEach(binding -> ((BindingImpl<BEAN, ?, ?>) binding) .writeFieldValue(getBean())); validationStatus = new BinderValidationStatus<>(this, bindingStatuses, validateBean(getBean())); restoreBeanState(getBean(), beanState); getValidationStatusHandler().statusChange(validationStatus); fireStatusChangeEvent(validationStatus.hasErrors());
panelContent.setExpandRatio(form, 1f); passwordBinder = new Binder<>(); passwordBinder.setReadOnly(true); binderIsReadOnly=true; if (selectedU.getId() != null) { currentPasswordField.setWidth("80%"); passwordBinder.forField(currentPasswordField) .asRequired(getApp().getMessage("errorMessage.req", currentPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getCurrentPasswordMatchValidator()) passwordBinder.forField(newPasswordField) .asRequired(getApp().getMessage("errorMessage.req",newPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getNewPasswordNotUsedValidator()) passwordBinder.forField(repeatPasswordField) .asRequired(getApp().getMessage("errorMessage.req", repeatPasswordField.getCaption())) .withValidator(((UsersHelper)getHelper()).getPasswordsMatchValidator(newPasswordField))
/** * 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); }
@Override public void write(T item) { binder.writeBeanIfValid(item); optionsField.getSelectedItem().ifPresent(selectedOptionDefinition -> subFormItemProvider(selectedOptionDefinition).read().ifPresent(t -> subForm(selectedOptionDefinition).write(t))); }
public void commit() throws ValidationException { this.binder.writeBean(this.value); }
private Button.ClickListener addEntryClickListener(Binder binder) { return event -> { if (binder.validate().isOk()) { if (binder.writeBeanIfValid(binder.getBean())) { GLEntry entry = (GLEntry) binder.getBean(); int count = 0; List<GLEntry> newEntriesList = new ArrayList<>(); BindingValidationStatus<?> result = (BindingValidationStatus<?>) binder.validate().getFieldValidationErrors().get(0); app.displayNotification(result.getResult().get().getErrorMessage());
public NewEntryForm (GLTransaction transaction, TransactionsHelper helper, EntryGrid entryGrid) { super(); try { this.transaction = transaction.clone(); } catch (CloneNotSupportedException e) { // Log error and ignore. Clone should be supported. QI.getQI().getLog().error(e); } this.journal = transaction.getJournal(); this.helper = helper; this.app = QI.getQI(); this.entryGrid = entryGrid; setStyleName(ValoTheme.LAYOUT_HORIZONTAL_WRAPPING); setSpacing(true); setWidthUndefined(); GLEntry entry = new GLEntry(); binder = new Binder (GLEntry.class); createAndBindFields(binder); binder.setBean(entry); binder.setReadOnly(true); addEntryBtn = new Button(app.getMessage("addEntry")); addEntryBtn.addClickListener(addEntryClickListener(binder)); addEntryBtn.setEnabled(false); addEntryBtn.setStyleName(ValoTheme.BUTTON_FRIENDLY); addComponent(addEntryBtn); setComponentAlignment(addEntryBtn, Alignment.BOTTOM_CENTER); }
TextField amount = createTextField("amount"); RadioButtonGroup credit = createCreditOrDebit(); binder.forField(accountCode) .withNullRepresentation("") .withConverter(new AccountConverter()) .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) .withValidator(new StringLengthValidator(app.getMessage("errorMessage.invalidField", detail.getCaption()), 0, 255)) .withValidator(new RegexpValidator(app.getMessage("errorMessage.invalidField", detail.getCaption()), TEXT_REGEX)) .bind("detail"); binder.forField(tags) .withValidator(new StringLengthValidator(app.getMessage("errorMessage.invalidField", tags.getCaption()), 0, 255)) .withValidator(new RegexpValidator(app.getMessage("errorMessage.invalidField", tags.getCaption()), TEXT_REGEX)) .withConverter(new StringToTagConverter()) .bind("tags"); binder.forField(amount) .asRequired(app.getMessage("errorMessage.req", StringUtils.capitalize(amount.getCaption()))) .withConverter(new StringToBigDecimalConverter(app.getMessage("errorMessage.invalidAmount"))) .withNullRepresentation(BigDecimal.ZERO) .bind("amount"); binder.bind(credit, "credit"); addComponent(accountCode);
field = makeFieldInstance( (Class<? extends HasValue<?>>) memberField.getType()); initializeField(objectWithMemberFields, memberField, field); forField(field).bind(property); return true; } else {
@Override public void setReadOnly(final boolean fieldsReadOnly) { this.readOnly = fieldsReadOnly; this.binder.setReadOnly(fieldsReadOnly); }
private void safeWrite(Binder<T> binder, T item) { try { if (binder.hasChanges()) { binder.writeBean(item); } } catch (ValidationException e) { log.error("{}", e.getMessage(), e); } }
/** * Sets a component and setter to use for editing values of this column * in the editor row. This is a shorthand for use in simple cases where * no validator or converter is needed. Use * {@link #setEditorBinding(Binding)} to support more complex cases. * <p> * <strong>Note:</strong> The same component cannot be used for multiple * columns. * * @param editorComponent * the editor component * @param setter * a setter that stores the component value in the row item * @return this column * * @see #setEditorBinding(Binding) * @see Grid#getEditor() * @see Binder#bind(HasValue, ValueProvider, Setter) */ public <C extends HasValue<V> & Component> Column<T, V> setEditorComponent( C editorComponent, Setter<T, V> setter) { Objects.requireNonNull(editorComponent, "Editor component cannot be null"); Objects.requireNonNull(setter, "Setter cannot be null"); Binding<T, V> binding = getGrid().getEditor().getBinder() .bind(editorComponent, valueProvider::apply, setter); return setEditorBinding(binding); }
protected boolean saveClick (Button.ClickEvent event, Layout formLayout) { if (passwordBinder != null && !binderIsReadOnly) { if (!passwordBinder.isValid()) { return false; } } if (super.saveClick (event, formLayout)) { if (resetPassBtn != null) resetPassBtn.setEnabled(false); if (changePassBtn != null) { passwordBinder.readBean(""); changePassBtn.setEnabled(false); hidePasswordPanel(); } return true; } return false; }
@Override public void statusChange(StatusChangeEvent event) { ET bean = (ET) event.getBinder().getBean(); if (bean == newInstance) { // God dammit, you can't rely on BeanValidationBinder.isValid ! // Returns true here even if some notnull fields are not set :-( // Thus, check also with own BeanValidation logi, this should // also give a bare bones cross field validation support for // elements final Binder<?> binder = event.getBinder(); final boolean valid = binder.isValid(); if (!valid || !isValidBean(bean)) { return; } getAndEnsureValue().add(newInstance); fireEvent( new ElementAddedEvent<>(AbstractElementCollection.this, newInstance)); setPersisted(newInstance, true); onElementAdded(); } // TODO fireValueChange(); }