Skip to content

Instantly share code, notes, and snippets.

@mkgl
Last active November 23, 2018 11:28
Show Gist options
  • Save mkgl/9b9acdc1070705aacb3a8411e22bd9dd to your computer and use it in GitHub Desktop.
Save mkgl/9b9acdc1070705aacb3a8411e22bd9dd to your computer and use it in GitHub Desktop.
Validating min/max entries in Multivalue fields in Magnolia 5.6+
package info.magnolia.dev.mgnlui_3527_multifield_validation;
import info.magnolia.ui.form.validator.definition.ConfiguredFieldValidatorDefinition;
import info.magnolia.ui.form.validator.definition.FieldValidatorDefinition;
import info.magnolia.ui.form.validator.factory.AbstractFieldValidatorFactory;
import info.magnolia.ui.form.validator.factory.FieldValidatorFactory;
import java.text.MessageFormat;
import javax.inject.Inject;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.Validator;
import com.vaadin.v7.data.validator.AbstractValidator;
import com.vaadin.v7.data.validator.IntegerRangeValidator;
/**
* Defines a range validator for the count of "property-entries" of a Vaadin {@link Item}.
*
* <p>This is especially suited for {@linkplain info.magnolia.ui.form.field.definition.MultiValueFieldDefinition multi-value fields},
* as they typically hold their entries as a plain {@link com.vaadin.v7.data.util.PropertysetItem PropertysetItem}.
*/
public class EntryCountValidatorDefinition extends ConfiguredFieldValidatorDefinition implements FieldValidatorDefinition {
private int minValue;
private int maxValue;
public int getMinValue() {
return minValue;
}
public void setMinValue(int minValue) {
this.minValue = minValue;
}
public int getMaxValue() {
return maxValue;
}
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
@Override
public Class<? extends FieldValidatorFactory> getFactoryClass() {
return EntryCountValidatorFactory.class;
}
/**
* The Magnolia ValidatorFactory.
*/
private static class EntryCountValidatorFactory extends AbstractFieldValidatorFactory<EntryCountValidatorDefinition> implements FieldValidatorFactory {
@Inject
public EntryCountValidatorFactory(EntryCountValidatorDefinition definition) {
super(definition);
}
@Override
public Validator createValidator() {
return new EntryCountValidator(getI18nErrorMessage(), definition.getMinValue(), definition.getMaxValue());
}
}
/**
* And the actual Vaadin Validator.
*/
private static class EntryCountValidator extends AbstractValidator<Item> {
private final IntegerRangeValidator delegate;
public EntryCountValidator(String errorMessage, Integer minValue, Integer maxValue) {
super(errorMessage);
MessageFormat messageFormat = new MessageFormat(errorMessage);
errorMessage = messageFormat.format(new Object[] { minValue, maxValue });
delegate = new IntegerRangeValidator(errorMessage, minValue, maxValue);
}
@Override
public void validate(Object value) throws InvalidValueException {
// isValidType ensures that value can safely be cast to TYPE
if (!isValidType(value)) {
throw new InvalidValueException(getErrorMessage());
}
Item item = (Item) value;
delegate.validate(item.getItemPropertyIds().size());
}
@Override
public String getErrorMessage() {
return super.getErrorMessage();
}
@Override
public Class<Item> getType() {
return Item.class;
}
@Override
protected boolean isValidValue(Item value) {
throw new IllegalStateException("#isValidValue() should not be used, use #validate() instead.");
}
}
}
label: MGNLUI-3527 Multivalue Field with validation
form:
label: Multivalue Field with validation
tabs:
- name: 3527
label: MGNLUI-3527
fields:
- name: ingredients
class: info.magnolia.ui.form.field.definition.MultiValueFieldDefinition
transformerClass: info.magnolia.ui.form.field.transformer.multi.DelegatingMultiValueFieldTransformer
# required: true
label: Ingredients
field:
class: info.magnolia.ui.form.field.definition.TextFieldDefinition
validators:
- class: info.magnolia.dev.mgnlui_3527_multifield_validation.EntryCountValidatorDefinition
minValue: 3
maxValue: 10
actions:
commit:
class: info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition
cancel:
class: info.magnolia.ui.admincentral.dialog.action.CancelDialogActionDefinition
samples.MGNLUI-3527-multifield-validation.3527.ingredients.validation.errorMessage=There must be {0} to {1} ingredients.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment