Child pages
  • Providing Converters and Validators from Plugins

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

With 2020.06 there is a IColumnValidator2 which extends IColumnValidator which adds public void validate(Map<String, String> props, Object value, String dataprovider, IValidationObject validationObject, Object state); method that works with the new validation framework: Data/Record/Column validation

As an example how the new IColumnValidator2 interface works look here (see also below the example)

Example

This is an example of validating the size of a column of type TEXT or MEDIA. The validation rule is that the size must be lower or equal to the value given by the developer by setting the length property in the Validation tab.

Code Block
languagejava
import java.util.Map;

import com.servoy.j2db.dataprocessing.IColumnValidator2;
import com.servoy.j2db.dataprocessing.IValidationObject;
import com.servoy.j2db.persistence.IColumnTypes;
import com.servoy.j2db.util.ILogLevel;
import com.servoy.j2db.util.Utils;

@SuppressWarnings("nls")
public class SizeValidatorNumberRangeValidator implements IColumnValidatorIColumnValidator2
{
	private static final String 	LENGTHFROM_PROPERTY = "from";
	private static final String TO_PROPERTY = "lengthto";

	public MapMap<String, String> getDefaultProperties()
	{
		MapMap<String, String> props = new HashMapjava.util.LinkedHashMap<>();
		props.put(LENGTHFROM_PROPERTY, "");
		props.put(TO_PROPERTY, "");
		return props;
	}

	public String getName()
	{
		return "servoy.NumberRangeValidator";
		{}

	public int[] getSupportedColumnTypes()
	{
		return new int[] 	return{ "servoy.SizeValidator";
  IColumnTypes.INTEGER, IColumnTypes.NUMBER };
	}

	}/**
     	public int[] getSupportedColumnTypes()
    	{
        	return new int[] { IColumnTypes.TEXT, IColumnTypes.MEDIA };
    	}

* IColumnValidator2 interface with the new IValidationObject, if given then the error is reported on that instead of throwing an exception.
     */ 
	@Override
	public void validate(MapMap<String, String> props, Object arg) throws IllegalArgumentException value, String dataprovider, IValidationObject validationObject, Object state)
	{
		Stringif propLength(value == (String)null || value.toString().trim().length() == 0) return;

		double from = Utils.getAsDouble(props.get(LENGTHFROM_PROPERTY));
		intdouble lengthto = new Double(propLength.replace(',', '.')).intValue(Utils.getAsDouble(props.get(TO_PROPERTY));
		double val = Utils.getAsDouble(value);
		if (arg instanceof byte[] && ((byte[])arg).length > length)
		{
			val < from || val > to)
		{
			if (validationObject != null)
				validationObject.report("i18n:servoy.validator.range", dataprovider, ILogLevel.ERROR, state,
					new Object[] { value, dataprovider, Double.valueOf(from), Double.valueOf(to) });
			else throw new IllegalArgumentException();
		}
		else if (arg instanceof String && ((String)arg).length() > length)
		{
			throw new IllegalArgumentException();
		}}

    // the old validate method of IColumnValidator interface, just call the IColumnValidator2 valdiate method with null.
	public void validate(Map<String, String> props, Object arg) throws IllegalArgumentException
	{
		validate(props, arg, null, null, null);
	}
}