-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nodeNameValidator to field jcrName on add/edit dialogs
- Loading branch information
eschleb
committed
Mar 1, 2024
1 parent
ba599c5
commit ea1baae
Showing
4 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...ain/java/com/namics/oss/magnolia/appbuilder/action/JcrNameValidatingOpenDialogAction.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,79 @@ | ||
package com.namics.oss.magnolia.appbuilder.action; | ||
|
||
import info.magnolia.i18nsystem.I18nizer; | ||
import info.magnolia.ui.UIComponent; | ||
import info.magnolia.ui.ValueContext; | ||
import info.magnolia.ui.api.i18n.I18NAuthoringSupport; | ||
import info.magnolia.ui.dialog.DialogDefinition; | ||
import info.magnolia.ui.dialog.DialogDefinitionRegistry; | ||
import info.magnolia.ui.dialog.FormDialogDefinition; | ||
import info.magnolia.ui.dialog.actions.OpenDialogAction; | ||
import info.magnolia.ui.dialog.actions.OpenDialogActionDefinition; | ||
import info.magnolia.ui.editor.LocaleContext; | ||
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition; | ||
import info.magnolia.ui.field.ConfiguredFieldDefinition; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.Node; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Adds a nodeNameValidator to the field jcrName if it is present in the dialog (must be unique). | ||
*/ | ||
public class JcrNameValidatingOpenDialogAction extends OpenDialogAction<Node> { | ||
private final NodeNameValidatorDefinition nodeNameValidatorDefinition; | ||
|
||
@Inject | ||
public JcrNameValidatingOpenDialogAction( | ||
final Definition definition, | ||
final LocaleContext localeContext, | ||
final ValueContext<Node> valueContext, | ||
final UIComponent parentView, | ||
final I18NAuthoringSupport<Node> i18NAuthoringSupport, | ||
final DialogDefinitionRegistry dialogDefinitionRegistry, | ||
final I18nizer i18nizer | ||
) { | ||
super(definition, localeContext, valueContext, parentView, i18NAuthoringSupport, dialogDefinitionRegistry, i18nizer); | ||
this.nodeNameValidatorDefinition = new NodeNameValidatorDefinition(); | ||
this.nodeNameValidatorDefinition.setMode(definition.getMode()); | ||
} | ||
|
||
@Override | ||
protected DialogDefinition getDialogDefinition(final DialogDefinitionRegistry dialogDefinitionRegistry, final I18nizer i18nizer) { | ||
final DialogDefinition dialogDefinition = super.getDialogDefinition(dialogDefinitionRegistry, i18nizer); | ||
addNodeNameValidatorToJcrNameField(dialogDefinition); | ||
return dialogDefinition; | ||
} | ||
|
||
protected void addNodeNameValidatorToJcrNameField(final DialogDefinition dialogDefinition) { | ||
Optional | ||
.of(dialogDefinition) | ||
.filter(FormDialogDefinition.class::isInstance) | ||
.map(definition -> (FormDialogDefinition<Node>) definition) | ||
.map(FormDialogDefinition::getForm) | ||
.flatMap(formDialog -> formDialog.getFieldDefinition("jcrName")) | ||
.filter(ConfiguredFieldDefinition.class::isInstance) | ||
.map(definition -> (ConfiguredFieldDefinition<Node>) definition) | ||
.ifPresent(field -> | ||
field.setValidators(Stream.concat( | ||
field.getValidators().stream(), | ||
Stream.of(nodeNameValidatorDefinition) | ||
).collect(Collectors.toList())) | ||
); | ||
} | ||
|
||
public static class Definition extends OpenDialogActionDefinition { | ||
private final NodeNameValidatorDefinition.Mode mode; | ||
|
||
public Definition(final NodeNameValidatorDefinition.Mode mode) { | ||
setImplementationClass(JcrNameValidatingOpenDialogAction.class); | ||
this.mode = mode; | ||
} | ||
|
||
public NodeNameValidatorDefinition.Mode getMode() { | ||
return mode; | ||
} | ||
} | ||
} |
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
6 changes: 4 additions & 2 deletions
6
.../java/com/namics/oss/magnolia/appbuilder/action/add/OpenCreateDialogActionDefinition.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
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