GridDDMFormFieldValueValidator implements DDMFormFieldValueValidator { @Override public void validate(DDMFormField ddmFormField, Value value) throws DDMFormFieldValueValidationException { DDMFormFieldOptions rows = (DDMFormFieldOptions)ddmFormField.getProperty("rows"); DDMFormFieldOptions columns = (DDMFormFieldOptions)ddmFormField.getProperty("columns"); if ((rows == null) || (columns == null)) { throw new DDMFormFieldValueValidationException( String.format( "Rows and columns must be set for grid field \"%s\"", ddmFormField.getName())); } Set<String> rowValues = rows.getOptionsValues(); Set<String> columnValues = columns.getOptionsValues(); if (rowValues.isEmpty() || columnValues.isEmpty()) { throw new DDMFormFieldValueValidationException( "Rows and columns must contain at least one alternative each"); } if (value == null) { return; } for (Locale availableLocale : value.getAvailableLocales()) { validateSelectedValue( ddmFormField, rowValues, columnValues, value.getString(availableLocale)); } }  @Override void validate(DDMFormField ddmFormField, Value value);  }
@Test(expected = DDMFormFieldValueValidationException.class) public void testValidationWithEmptyColumns() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "Grid", "Grid", "grid", "string", false, false, false); DDMFormFieldOptions ddmFormFieldRows = new DDMFormFieldOptions(); ddmFormFieldRows.addOptionLabel( "rowValue 1", LocaleUtil.US, "rowLabel 1"); ddmFormFieldRows.addOptionLabel( "rowValue 2", LocaleUtil.US, "rowLabel 2"); ddmFormField.setProperty("rows", ddmFormFieldRows); DDMFormFieldOptions ddmFormFieldColumns = new DDMFormFieldOptions(); ddmFormField.setProperty("columns", ddmFormFieldColumns); ddmForm.addDDMFormField(ddmFormField); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "Grid", new UnlocalizedValue("{\"rowValue 1\":\"columnValue 1\"}")); _gridDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }
@Test(expected = DDMFormFieldValueValidationException.class) public void testValidationWithInvalidColumnValue() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "Grid", "Grid", "grid", "string", false, false, false); DDMFormFieldOptions ddmFormFieldRows = new DDMFormFieldOptions(); ddmFormFieldRows.addOptionLabel( "rowValue 1", LocaleUtil.US, "rowLabel 1"); ddmFormFieldRows.addOptionLabel( "rowValue 2", LocaleUtil.US, "rowLabel 2"); ddmFormField.setProperty("rows", ddmFormFieldRows); DDMFormFieldOptions ddmFormFieldColumns = new DDMFormFieldOptions(); ddmFormFieldColumns.addOptionLabel( "columnValue 1", LocaleUtil.US, "columnLabel 1"); ddmFormFieldColumns.addOptionLabel( "columnValue 2", LocaleUtil.US, "columnLabel 2"); ddmFormField.setProperty("columns", ddmFormFieldColumns); ddmForm.addDDMFormField(ddmFormField); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "Grid", new UnlocalizedValue("{\"rowValue 1\":\"columnValue 4\"}")); _gridDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }
@Test(expected = DDMFormFieldValueValidationException.class) public void testValidationWithNoColumns() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "Grid", "Grid", "grid", "string", false, false, false); DDMFormFieldOptions ddmFormFieldRows = new DDMFormFieldOptions(); ddmFormFieldRows.addOptionLabel( "rowValue 1", LocaleUtil.US, "rowLabel 1"); ddmFormFieldRows.addOptionLabel( "rowValue 2", LocaleUtil.US, "rowLabel 2"); ddmFormField.setProperty("rows", ddmFormFieldRows); DDMFormFieldOptions ddmFormFieldColumns = null; ddmFormField.setProperty("columns", ddmFormFieldColumns); ddmForm.addDDMFormField(ddmFormField); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "Grid", new UnlocalizedValue("{\"rowValue 1\":\"columnValue 1\"}")); _gridDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }