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);
}
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;
}
}
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();
}
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();
}
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;
});
}
Aggregations