Skip to content
Snippets Groups Projects
Commit 76a9a5e0 authored by rhaag's avatar rhaag
Browse files

OLAT-6169 improve form validation

--HG--
branch : uzhFixes711
parent abba07e7
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package org.olat.portfolio.model.restriction; package org.olat.portfolio.model.restriction;
import org.olat.core.commons.persistence.PersistentObject; import org.olat.core.commons.persistence.PersistentObject;
import org.olat.core.util.StringHelper;
/** /**
* Description:<br> * Description:<br>
...@@ -129,4 +130,19 @@ public class CollectRestriction extends PersistentObject { ...@@ -129,4 +130,19 @@ public class CollectRestriction extends PersistentObject {
} }
return false; return false;
} }
/**
* @see org.olat.core.commons.persistence.PersistentObject#toString()
*/
@Override
public String toString() {
return "key: " + getKey() + " Restriction: " + getRestriction() + " amount: " + getAmount() + " of type: " + getArtefactType();
}
// basic check for validity. do not save invalid restrictions!
public boolean isValid(){
if (StringHelper.containsNonWhitespace(getRestriction()) && StringHelper.containsNonWhitespace(getArtefactType()) && getAmount() > 0) return true;
return false;
}
} }
...@@ -253,14 +253,16 @@ public class EPStructureDetailsController extends FormBasicController { ...@@ -253,14 +253,16 @@ public class EPStructureDetailsController extends FormBasicController {
} }
usedTypes.add(restriction.getArtefactType()); usedTypes.add(restriction.getArtefactType());
if (!(StringHelper.containsNonWhitespace(restriction.getRestriction()) boolean hasRestriction = StringHelper.containsNonWhitespace(restriction.getRestriction());
&& StringHelper.containsNonWhitespace(restriction.getArtefactType()) && restriction.getAmount() > 0)) { boolean hasArtType = StringHelper.containsNonWhitespace(restriction.getArtefactType());
boolean hasAmount = restriction.getAmount() > 0;
boolean isValid = restriction.isValid();
if (!isValid && (hasRestriction || hasArtType || hasAmount)) {
StaticTextElement thisErrorEl = errorElements.get(i); StaticTextElement thisErrorEl = errorElements.get(i);
thisErrorEl.setVisible(true); thisErrorEl.setVisible(true);
thisErrorEl.setValue(translate("collect.restriction.incomplete")); thisErrorEl.setValue(translate("collect.restriction.incomplete"));
hasError = true; hasError = true;
} }
i++; i++;
} }
return !hasError; return !hasError;
...@@ -290,10 +292,7 @@ public class EPStructureDetailsController extends FormBasicController { ...@@ -290,10 +292,7 @@ public class EPStructureDetailsController extends FormBasicController {
setCollectRestrictions(); setCollectRestrictions();
for(SingleSelection restrictionElement:restrictionElements) { for(SingleSelection restrictionElement:restrictionElements) {
CollectRestriction restriction = (CollectRestriction)restrictionElement.getUserObject(); CollectRestriction restriction = (CollectRestriction)restrictionElement.getUserObject();
if(StringHelper.containsNonWhitespace(restriction.getRestriction()) && if(restriction.isValid()) {
StringHelper.containsNonWhitespace(restriction.getArtefactType()) &&
restriction.getAmount() >= 0) {
CollectRestriction cr = new CollectRestriction(restriction); CollectRestriction cr = new CollectRestriction(restriction);
editStructure.getCollectRestrictions().add(cr); editStructure.getCollectRestrictions().add(cr);
} }
...@@ -324,32 +323,25 @@ public class EPStructureDetailsController extends FormBasicController { ...@@ -324,32 +323,25 @@ public class EPStructureDetailsController extends FormBasicController {
} }
protected void setCollectRestrictions() { protected void setCollectRestrictions() {
if(restrictionElements == null || restrictionElements.isEmpty()) { if (restrictionElements == null || restrictionElements.isEmpty()) { return; }
return;
} for (int i = 0; i < restrictionElements.size(); i++) {
for(int i=0; i<restrictionElements.size(); i++) {
SingleSelection restrictionElement = restrictionElements.get(i); SingleSelection restrictionElement = restrictionElements.get(i);
SingleSelection restrictToArtefactElement = restrictToArtefactElements.get(i); SingleSelection restrictToArtefactElement = restrictToArtefactElements.get(i);
TextElement amountElement = amountElements.get(i);
if(restrictionElement.getSelected() > 0 || restrictToArtefactElement.getSelected() > 0) { CollectRestriction cr = (CollectRestriction) restrictionElement.getUserObject();
TextElement amountElement = amountElements.get(i); String restriction = restrictionElement.getSelectedKey();
CollectRestriction cr = (CollectRestriction)restrictionElement.getUserObject(); String artefactType = restrictToArtefactElement.getSelectedKey();
String restriction = restrictionElement.getSelectedKey(); String amount = amountElement.getValue();
String artefactType = restrictToArtefactElement.getSelectedKey();
String amount = amountElement.getValue(); cr.setRestriction(restriction);
if(StringHelper.containsNonWhitespace(restriction)) { cr.setArtefactType(artefactType);
cr.setRestriction(restriction); if (StringHelper.containsNonWhitespace(amount)) {
} try {
if(StringHelper.containsNonWhitespace(artefactType)) { cr.setAmount(Integer.parseInt(amount));
cr.setArtefactType(artefactType); } catch (NumberFormatException e) {
} logWarn("Wrong format for number", e);
if(StringHelper.containsNonWhitespace(amount)) {
try {
cr.setAmount(Integer.parseInt(amount));
} catch (NumberFormatException e) {
logWarn("Wrong format for number", e);
}
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment