Search in sources :

Example 1 with ValidationResult

use of org.jboss.hal.ballroom.form.ValidationResult in project console by hal.

the class ConfigurePasswordStep method onShow.

@Override
protected void onShow(PasswordContext context) {
    AddressTemplate template = metadata.getTemplate();
    Metadata passwordMetadata = metadata.forOperation(SET_PASSWORD).forComplexAttribute(context.type.name);
    LabelBuilder labelBuilder = new LabelBuilder();
    header.textContent = labelBuilder.label(context.type.name);
    description.textContent = passwordMetadata.getDescription().getDescription();
    String id = Ids.build(template.lastName(), SET_PASSWORD, FORM);
    ModelNodeForm.Builder<ModelNode> builder = new ModelNodeForm.Builder<>(id, passwordMetadata).onSave((form1, changedValues) -> this.changedValues = changedValues);
    passwordMetadata.getDescription().getAttributes(ATTRIBUTES).forEach(attr -> {
        if (ModelType.BYTES.equals(attr.getValue().get(TYPE).asType())) {
            builder.customFormItem(attr.getName(), desc -> {
                TextBoxItem saltItem = new TextBoxItem(attr.getName(), labelBuilder.label(attr.getName()));
                saltItem.setRequired(desc.getValue().get(REQUIRED).asBoolean());
                saltItem.addValidationHandler(value -> {
                    ValidationResult result = ValidationResult.OK;
                    // accordingly to the :set-password operation, the salt must be exactly 16 bytes
                    if (value.length() != 16) {
                        result = ValidationResult.invalid(resources.messages().invalidLength());
                    }
                    return result;
                });
                return saltItem;
            });
        }
    });
    form = builder.build();
    HTMLElement formElement = form.element();
    form.attach();
    form.edit(new ModelNode());
    // as the form is dynamically added to the section, we must remove the previous form element
    if (section.childElementCount > 2) {
        section.removeChild(section.lastChild);
    }
    section.appendChild(formElement);
}
Also used : AddressTemplate(org.jboss.hal.meta.AddressTemplate) TextBoxItem(org.jboss.hal.ballroom.form.TextBoxItem) HTMLElement(elemental2.dom.HTMLElement) Metadata(org.jboss.hal.meta.Metadata) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder) ModelNode(org.jboss.hal.dmr.ModelNode) ValidationResult(org.jboss.hal.ballroom.form.ValidationResult) ModelNodeForm(org.jboss.hal.core.mbui.form.ModelNodeForm)

Example 2 with ValidationResult

use of org.jboss.hal.ballroom.form.ValidationResult in project console by hal.

the class ExactlyOneAlternativeValidation method validate.

@Override
public ValidationResult validate(final Form<T> form) {
    LabelBuilder labelBuilder = new LabelBuilder();
    Set<String> emptyItems = requiredAlternatives.stream().filter(name -> {
        FormItem formItem = form.getFormItem(name);
        boolean empty = formItem != null && formItem.isEmpty();
        // there is a special case for SwitchItem of Boolean type, the SwitchItem.isEmpty() tests if the value is
        // null, but for this validation case we must ensure the value is set to false to allow the validation
        // to work
        boolean switchItemFalse = false;
        if (formItem != null && formItem.getClass().equals(SwitchItem.class)) {
            Object value = formItem.getValue();
            switchItemFalse = value != null && !Boolean.parseBoolean(value.toString());
        }
        return empty || switchItemFalse;
    }).collect(toSet());
    if (requiredAlternatives.size() == emptyItems.size()) {
        // show an error on each related form item
        requiredAlternatives.forEach(alternative -> {
            FormItem<Object> formItem = form.getFormItem(alternative);
            if (formItem.isEmpty()) {
                formItem.showError(messages.exactlyOneAlternativeError(labelBuilder.enumeration(requiredAlternatives, constants.or())));
            }
        });
        // return overall result
        return ValidationResult.invalid(messages.exactlyOneAlternativesError(labelBuilder.enumeration(requiredAlternatives, constants.or())));
    } else {
        return ValidationResult.OK;
    }
}
Also used : ModelNode(org.jboss.hal.dmr.ModelNode) Iterables(com.google.common.collect.Iterables) SortedSet(java.util.SortedSet) Messages(org.jboss.hal.resources.Messages) Set(java.util.Set) TreeSet(java.util.TreeSet) FormValidation(org.jboss.hal.ballroom.form.FormValidation) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder) FormItem(org.jboss.hal.ballroom.form.FormItem) SwitchItem(org.jboss.hal.ballroom.form.SwitchItem) Constants(org.jboss.hal.resources.Constants) ValidationResult(org.jboss.hal.ballroom.form.ValidationResult) Collectors.toSet(java.util.stream.Collectors.toSet) Form(org.jboss.hal.ballroom.form.Form) FormItem(org.jboss.hal.ballroom.form.FormItem) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder)

Example 3 with ValidationResult

use of org.jboss.hal.ballroom.form.ValidationResult in project console by hal.

the class OtherSettingsPresenter method addCredentialStore.

// -------------------------------------------- Credential Store
void addCredentialStore() {
    Metadata metadata = metadataRegistry.lookup(CREDENTIAL_STORE_TEMPLATE);
    SafeHtml typeHelp = SafeHtmlUtils.fromString(metadata.getDescription().get(ATTRIBUTES).get(TYPE).get(DESCRIPTION).asString());
    Metadata crMetadata = metadata.forComplexAttribute(CREDENTIAL_REFERENCE, true);
    crMetadata.copyComplexAttributeAttributes(asList(STORE, ALIAS, TYPE, CLEAR_TEXT), metadata);
    TextBoxItem typeItem = new TextBoxItem("type-", resources.constants().type());
    String id = Ids.build(Ids.ELYTRON_CREDENTIAL_STORE, Ids.ADD);
    NameItem nameItem = new NameItem();
    ModelNodeForm<ModelNode> form = new ModelNodeForm.Builder<>(id, metadata).addOnly().unboundFormItem(nameItem, 0).include(CREATE, PATH, RELATIVE_TO, STORE, ALIAS, TYPE, CLEAR_TEXT).unboundFormItem(typeItem, 3, typeHelp).unsorted().build();
    form.getFormItem(RELATIVE_TO).registerSuggestHandler(new PathsAutoComplete());
    form.addFormValidation(new RequireAtLeastOneAttributeValidation<>(asList(STORE, CLEAR_TEXT), resources));
    form.addFormValidation(form1 -> {
        ValidationResult result = ValidationResult.OK;
        String typeValue = typeItem.getValue();
        FormItem<String> locationAttr = form1.getFormItem(PATH);
        boolean invalidLocation = locationAttr.isEmpty() && (typeItem.isEmpty() || Collections.binarySearch(FILE_BASED_CS, typeValue) > -1);
        if (invalidLocation) {
            form1.getFormItem(PATH).showError(resources.constants().requiredField());
            result = ValidationResult.invalid(resources.messages().pathRequired());
        }
        return result;
    });
    new AddResourceDialog(resources.messages().addResourceTitle(Names.CREDENTIAL_STORE), form, (name, model) -> {
        if (model != null) {
            move(model, STORE, CREDENTIAL_REFERENCE + "/" + STORE);
            move(model, ALIAS, CREDENTIAL_REFERENCE + "/" + ALIAS);
            move(model, TYPE, CREDENTIAL_REFERENCE + "/" + TYPE);
            move(model, CLEAR_TEXT, CREDENTIAL_REFERENCE + "/" + CLEAR_TEXT);
        }
        if (!typeItem.isEmpty()) {
            model.get(TYPE).set(typeItem.getValue());
        }
        ResourceAddress address = CREDENTIAL_STORE_TEMPLATE.resolve(statementContext, nameItem.getValue());
        crud.add(Names.CREDENTIAL_STORE, name, address, model, (n, a) -> reload(CREDENTIAL_STORE, nodes -> getView().updateResourceElement(CREDENTIAL_STORE, nodes)));
    }).show();
}
Also used : AddResourceDialog(org.jboss.hal.core.mbui.dialog.AddResourceDialog) FinishRemove(org.jboss.hal.ballroom.form.Form.FinishRemove) StatementContext(org.jboss.hal.meta.StatementContext) AddResourceDialog(org.jboss.hal.core.mbui.dialog.AddResourceDialog) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) TextBoxItem(org.jboss.hal.ballroom.form.TextBoxItem) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FinishReset(org.jboss.hal.ballroom.form.Form.FinishReset) RequireAtLeastOneAttributeValidation(org.jboss.hal.core.mbui.form.RequireAtLeastOneAttributeValidation) Metadata(org.jboss.hal.meta.Metadata) Names(org.jboss.hal.resources.Names) ModelNodeHelper.asNamedNodes(org.jboss.hal.dmr.ModelNodeHelper.asNamedNodes) CrudOperations(org.jboss.hal.core.CrudOperations) ModelNodeHelper.move(org.jboss.hal.dmr.ModelNodeHelper.move) NameToken(com.gwtplatform.mvp.client.annotations.NameToken) ComplexAttributeOperations(org.jboss.hal.core.ComplexAttributeOperations) FinderPath(org.jboss.hal.core.finder.FinderPath) PathsAutoComplete(org.jboss.hal.core.configuration.PathsAutoComplete) List(java.util.List) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder) MetadataRegistry(org.jboss.hal.meta.MetadataRegistry) ModelDescriptionConstants(org.jboss.hal.dmr.ModelDescriptionConstants) Finder(org.jboss.hal.core.finder.Finder) ValidationResult(org.jboss.hal.ballroom.form.ValidationResult) SafeHtml(com.google.gwt.safehtml.shared.SafeHtml) SupportsExpertMode(org.jboss.hal.core.mvp.SupportsExpertMode) ModelNode(org.jboss.hal.dmr.ModelNode) ProxyPlace(com.gwtplatform.mvp.client.proxy.ProxyPlace) Inject(javax.inject.Inject) ModelNodeForm(org.jboss.hal.core.mbui.form.ModelNodeForm) NameItem(org.jboss.hal.core.mbui.dialog.NameItem) SafeHtmlUtils(com.google.gwt.safehtml.shared.SafeHtmlUtils) Requires(org.jboss.hal.spi.Requires) ProxyCodeSplit(com.gwtplatform.mvp.client.annotations.ProxyCodeSplit) Operation(org.jboss.hal.dmr.Operation) Ids(org.jboss.hal.resources.Ids) AddressTemplates(org.jboss.hal.client.configuration.subsystem.elytron.AddressTemplates) FinderPathFactory(org.jboss.hal.core.finder.FinderPathFactory) EventBus(com.google.web.bindery.event.shared.EventBus) Consumer(java.util.function.Consumer) Dispatcher(org.jboss.hal.dmr.dispatch.Dispatcher) FormItem(org.jboss.hal.ballroom.form.FormItem) NameTokens(org.jboss.hal.meta.token.NameTokens) MbuiPresenter(org.jboss.hal.core.mbui.MbuiPresenter) Resources(org.jboss.hal.resources.Resources) SuggestCapabilitiesAutoComplete(org.jboss.hal.ballroom.autocomplete.SuggestCapabilitiesAutoComplete) MbuiView(org.jboss.hal.core.mbui.MbuiView) NamedNode(org.jboss.hal.dmr.NamedNode) Collections(java.util.Collections) Form(org.jboss.hal.ballroom.form.Form) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) SafeHtml(com.google.gwt.safehtml.shared.SafeHtml) PathsAutoComplete(org.jboss.hal.core.configuration.PathsAutoComplete) LabelBuilder(org.jboss.hal.ballroom.LabelBuilder) Metadata(org.jboss.hal.meta.Metadata) ValidationResult(org.jboss.hal.ballroom.form.ValidationResult) TextBoxItem(org.jboss.hal.ballroom.form.TextBoxItem) NameItem(org.jboss.hal.core.mbui.dialog.NameItem) ModelNode(org.jboss.hal.dmr.ModelNode)

Example 4 with ValidationResult

use of org.jboss.hal.ballroom.form.ValidationResult in project console by hal.

the class ClusteringPresenter method showBroadcastOrDiscoveryGroupAddDialog.

void showBroadcastOrDiscoveryGroupAddDialog(ServerSubResource ssr, Consumer<ModelNodeForm.Builder<ModelNode>> formBuilderAugmenter, Consumer<Form<ModelNode>> formAugmenter) {
    Metadata metadata = metadataRegistry.lookup(ssr.template);
    NameItem nameItem = new NameItem();
    ModelNodeForm.Builder<ModelNode> formBuilder = new ModelNodeForm.Builder<>(Ids.build(ssr.baseId, Ids.ADD), metadata).unboundFormItem(nameItem, 0).fromRequestProperties().include(JGROUPS_CLUSTER, SOCKET_BINDING).unsorted();
    formBuilderAugmenter.accept(formBuilder);
    Form<ModelNode> form = formBuilder.build();
    formAugmenter.accept(form);
    // validation that requires one of jgroups-cluster and socket-binding to be configured
    FormValidation<ModelNode> jgroupsOrSocketValidation = new FormValidation<ModelNode>() {

        @Override
        public ValidationResult validate(Form<ModelNode> form) {
            if (form.getFormItem(JGROUPS_CLUSTER).isEmpty() && form.getFormItem(SOCKET_BINDING).isEmpty()) {
                return ValidationResult.invalid(resources.messages().jgroupsClusterOrSocketBindingMustBeSet());
            }
            return ValidationResult.OK;
        }
    };
    form.getFormItem(SOCKET_BINDING).registerSuggestHandler(new ReadChildrenAutoComplete(dispatcher, statementContext, SOCKET_BINDING_TEMPLATE));
    form.addFormValidation(jgroupsOrSocketValidation);
    new AddResourceDialog(resources.messages().addResourceTitle(ssr.type), form, (name, model) -> {
        name = nameItem.getValue();
        ResourceAddress address = SELECTED_SERVER_TEMPLATE.append(ssr.resource + "=" + name).resolve(statementContext);
        crud.add(ssr.type, name, address, model, (n, a) -> reload());
    }).show();
}
Also used : AddResourceDialog(org.jboss.hal.core.mbui.dialog.AddResourceDialog) SupportsExpertMode(org.jboss.hal.core.mvp.SupportsExpertMode) ModelNode(org.jboss.hal.dmr.ModelNode) StatementContext(org.jboss.hal.meta.StatementContext) ProxyPlace(com.gwtplatform.mvp.client.proxy.ProxyPlace) AddResourceDialog(org.jboss.hal.core.mbui.dialog.AddResourceDialog) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) Inject(javax.inject.Inject) ModelNodeForm(org.jboss.hal.core.mbui.form.ModelNodeForm) FormValidation(org.jboss.hal.ballroom.form.FormValidation) AddressTemplate(org.jboss.hal.meta.AddressTemplate) Arrays.asList(java.util.Arrays.asList) NameItem(org.jboss.hal.core.mbui.dialog.NameItem) Metadata(org.jboss.hal.meta.Metadata) Names(org.jboss.hal.resources.Names) ModelNodeHelper.asNamedNodes(org.jboss.hal.dmr.ModelNodeHelper.asNamedNodes) Requires(org.jboss.hal.spi.Requires) CrudOperations(org.jboss.hal.core.CrudOperations) ProxyCodeSplit(com.gwtplatform.mvp.client.annotations.ProxyCodeSplit) AddressTemplates(org.jboss.hal.client.configuration.subsystem.messaging.AddressTemplates) NameToken(com.gwtplatform.mvp.client.annotations.NameToken) Ids(org.jboss.hal.resources.Ids) FinderPathFactory(org.jboss.hal.core.finder.FinderPathFactory) EventBus(com.google.web.bindery.event.shared.EventBus) ReadChildrenAutoComplete(org.jboss.hal.ballroom.autocomplete.ReadChildrenAutoComplete) FinderPath(org.jboss.hal.core.finder.FinderPath) Consumer(java.util.function.Consumer) List(java.util.List) Dispatcher(org.jboss.hal.dmr.dispatch.Dispatcher) MetadataRegistry(org.jboss.hal.meta.MetadataRegistry) NameTokens(org.jboss.hal.meta.token.NameTokens) Resources(org.jboss.hal.resources.Resources) ModelDescriptionConstants(org.jboss.hal.dmr.ModelDescriptionConstants) MbuiView(org.jboss.hal.core.mbui.MbuiView) Finder(org.jboss.hal.core.finder.Finder) ValidationResult(org.jboss.hal.ballroom.form.ValidationResult) NamedNode(org.jboss.hal.dmr.NamedNode) Form(org.jboss.hal.ballroom.form.Form) ModelNodeForm(org.jboss.hal.core.mbui.form.ModelNodeForm) Form(org.jboss.hal.ballroom.form.Form) ResourceAddress(org.jboss.hal.dmr.ResourceAddress) Metadata(org.jboss.hal.meta.Metadata) ModelNodeForm(org.jboss.hal.core.mbui.form.ModelNodeForm) NameItem(org.jboss.hal.core.mbui.dialog.NameItem) FormValidation(org.jboss.hal.ballroom.form.FormValidation) ModelNode(org.jboss.hal.dmr.ModelNode) ReadChildrenAutoComplete(org.jboss.hal.ballroom.autocomplete.ReadChildrenAutoComplete)

Example 5 with ValidationResult

use of org.jboss.hal.ballroom.form.ValidationResult in project console by hal.

the class ConfigurationStep method addFormFieldValidator.

private void addFormFieldValidator(BooleanSupplier condition, String type, String attribute) {
    form.<String>getFormItem(attribute).addValidationHandler(fieldValue -> {
        ValidationResult validationResult = ValidationResult.OK;
        boolean validate = condition.getAsBoolean() && existingResources.get(type) != null && existingResources.get(type).contains(fieldValue);
        if (validate) {
            validationResult = ValidationResult.invalid(resources.messages().duplicateResource(type));
        }
        return validationResult;
    });
}
Also used : ValidationResult(org.jboss.hal.ballroom.form.ValidationResult)

Aggregations

ValidationResult (org.jboss.hal.ballroom.form.ValidationResult)5 ModelNode (org.jboss.hal.dmr.ModelNode)4 LabelBuilder (org.jboss.hal.ballroom.LabelBuilder)3 Form (org.jboss.hal.ballroom.form.Form)3 ModelNodeForm (org.jboss.hal.core.mbui.form.ModelNodeForm)3 Metadata (org.jboss.hal.meta.Metadata)3 EventBus (com.google.web.bindery.event.shared.EventBus)2 NameToken (com.gwtplatform.mvp.client.annotations.NameToken)2 ProxyCodeSplit (com.gwtplatform.mvp.client.annotations.ProxyCodeSplit)2 ProxyPlace (com.gwtplatform.mvp.client.proxy.ProxyPlace)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Inject (javax.inject.Inject)2 FormItem (org.jboss.hal.ballroom.form.FormItem)2 FormValidation (org.jboss.hal.ballroom.form.FormValidation)2 CrudOperations (org.jboss.hal.core.CrudOperations)2 Finder (org.jboss.hal.core.finder.Finder)2 FinderPath (org.jboss.hal.core.finder.FinderPath)2 FinderPathFactory (org.jboss.hal.core.finder.FinderPathFactory)2