-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#26 Added studio validators for forms
- Loading branch information
1 parent
6fb2cb6
commit a4fef95
Showing
16 changed files
with
988 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
...tudio-lib/src/main/java/com/tallence/formeditor/studio/validator/FormActionValidator.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
...tudio-lib/src/main/java/com/tallence/formeditor/studio/validator/FormEditorValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2018 Tallence AG | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.tallence.formeditor.studio.validator; | ||
|
||
import com.coremedia.cap.content.Content; | ||
import com.coremedia.cap.struct.Struct; | ||
import com.coremedia.rest.cap.validation.ContentTypeValidatorBase; | ||
import com.coremedia.rest.validation.Issues; | ||
import com.coremedia.rest.validation.Severity; | ||
import com.tallence.formeditor.contentbeans.FormEditor; | ||
import com.tallence.formeditor.studio.validator.field.FieldValidator; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Validates, that a form with form action "mailAction" does not have a fileUpload-field and has a mail-address entered. | ||
*/ | ||
public class FormEditorValidator extends ContentTypeValidatorBase { | ||
|
||
@Autowired | ||
private List<FieldValidator> fieldValidators; | ||
|
||
private Map<String, List<FieldValidator>> fieldValidatorsByType = new HashMap<>(); | ||
|
||
@PostConstruct | ||
public void init() { | ||
fieldValidators.forEach(validator -> { | ||
validator.resonsibleFor().forEach(type -> { | ||
if (!fieldValidatorsByType.containsKey(type)) { | ||
fieldValidatorsByType.put(type, new ArrayList<>()); | ||
} | ||
fieldValidatorsByType.get(type).add(validator); | ||
}); | ||
}); | ||
} | ||
|
||
@Override | ||
public void validate(Content content, Issues issues) { | ||
|
||
Struct formData = content.getStruct(FormEditor.FORM_DATA); | ||
String action = content.getString(FormEditor.FORM_ACTION); | ||
|
||
// Validate form fields | ||
if (formData.get(FormEditor.FORM_ELEMENTS) != null) { | ||
Struct formElements = formData.getStruct(FormEditor.FORM_ELEMENTS); | ||
|
||
formElements.getProperties().values() | ||
.stream() | ||
.filter(Struct.class::isInstance) | ||
.map(Struct.class::cast) | ||
.forEach(fieldStruct -> { | ||
String type = (String) fieldStruct.get("type"); | ||
Optional.ofNullable(fieldValidatorsByType.get(type)).ifPresent(fieldValidatorsForType -> { | ||
for (FieldValidator fieldValidator : fieldValidatorsForType) { | ||
fieldValidator.validateField(fieldStruct, action, issues); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Further validations | ||
if (FormEditor.MAIL_ACTION.equals(action) && !StringUtils.hasText(content.getString(FormEditor.ADMIN_MAILS))) { | ||
issues.addIssue(Severity.ERROR, FormEditor.FORM_ACTION, "form_action_mail"); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...udio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/FieldValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2018 Tallence AG | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.tallence.formeditor.studio.validator.field; | ||
|
||
import com.coremedia.cap.struct.Struct; | ||
import com.coremedia.rest.validation.Issues; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Defines a common interface for form editor fild validators. | ||
*/ | ||
public interface FieldValidator { | ||
|
||
/** | ||
* Returns the field type(s) this validator acts on. | ||
*/ | ||
List<String> resonsibleFor(); | ||
|
||
/** | ||
* Validate a single field configuration. | ||
* | ||
* @param fieldData the sub struct of formElements for a field. | ||
* @param action the current action of the form (some validators need this). | ||
* @param issues the Issues object for the current validation request. | ||
*/ | ||
void validateField(Struct fieldData, String action, Issues issues); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...b/src/main/java/com/tallence/formeditor/studio/validator/field/NameNotEmptyValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2018 Tallence AG | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.tallence.formeditor.studio.validator.field; | ||
|
||
import com.coremedia.cap.struct.Struct; | ||
import com.coremedia.rest.validation.Issues; | ||
import com.coremedia.rest.validation.Severity; | ||
import com.tallence.formeditor.contentbeans.FormEditor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Validates, that all form elements have a name. | ||
*/ | ||
@Component | ||
public class NameNotEmptyValidator implements FieldValidator { | ||
@Override | ||
public List<String> resonsibleFor() { | ||
return Arrays.asList("TextField", "NumberField", "RadioButtons", "CheckBoxes", "SelectBox", "TextArea", "UsersMail", | ||
"FileUpload", "ConsentFormCheckBox"); | ||
} | ||
|
||
@Override | ||
public void validateField(Struct fieldData, String action, Issues issues) { | ||
if (!StringUtils.hasText(fieldData.getString("name"))) { | ||
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "missing_name"); | ||
} | ||
} | ||
} |
Oops, something went wrong.