Example usage for org.apache.wicket.markup.html.form.validation AbstractFormValidator AbstractFormValidator

List of usage examples for org.apache.wicket.markup.html.form.validation AbstractFormValidator AbstractFormValidator

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html.form.validation AbstractFormValidator AbstractFormValidator.

Prototype

AbstractFormValidator

Source Link

Usage

From source file:com.doculibre.constellio.wicket.panels.admin.collectionPermission.AddEditCollectionPermissionPanel.java

License:Open Source License

public AddEditCollectionPermissionPanel(String id, CollectionPermission collectionPermission) {
    super(id, true);
    this.collectionPermissionModel = new ReloadableEntityModel<CollectionPermission>(collectionPermission);
    this.isCreation = collectionPermission.getId() == null;

    Form form = getForm();/*from   ww w . j  a  v  a2 s.  c  om*/
    form.setModel(new CompoundPropertyModel(collectionPermissionModel));

    IModel usersModel = new LoadableDetachableModel() {
        @Override
        protected Object load() {
            UserServices userServices = ConstellioSpringUtils.getUserServices();
            return new ArrayList<ConstellioUser>(userServices.list());
        }
    };
    IChoiceRenderer userRenderer = new ChoiceRenderer() {
        @Override
        public Object getDisplayValue(Object object) {
            ConstellioUser user = (ConstellioUser) object;
            return user.getFirstName() + " " + user.getLastName() + " (" + user.getUsername() + ")";
        }
    };
    userField = new DropDownChoice("constellioUser", usersModel, userRenderer);
    form.add(userField);
    userField.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            target.addComponent(groupField);
        }
    });

    IModel groupsModel = new LoadableDetachableModel() {
        @Override
        protected Object load() {
            GroupServices groupServices = ConstellioSpringUtils.getGroupServices();
            return new ArrayList<ConstellioGroup>(groupServices.list());
        }
    };
    IChoiceRenderer groupRenderer = new ChoiceRenderer("name");
    groupField = new DropDownChoice("constellioGroup", groupsModel, groupRenderer);
    form.add(groupField);
    groupField.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            target.addComponent(userField);
        }
    });

    form.add(new CheckBox("search"));
    form.add(new CheckBox("collaboration"));
    form.add(new CheckBox("admin"));

    form.add(new AbstractFormValidator() {
        @Override
        public void validate(Form form) {
            CollectionPermission collectionPermission = collectionPermissionModel.getObject();
            if (collectionPermission.getConstellioUser() == null
                    && collectionPermission.getConstellioGroup() == null) {
                error(userField, "userOrGroupRequired");
                error(groupField, "userOrGroupRequired");
            }
        }

        @Override
        public FormComponent[] getDependentFormComponents() {
            return null;
        }
    });

}

From source file:dk.frankbille.scoreboard.security.LoginPage.java

License:Open Source License

private void addLogin() {
    final LoginUser user = new LoginUser();
    final Form<Void> loginForm = new Form<Void>("loginForm") {
        private static final long serialVersionUID = 1L;

        @Override/*from w w w.  j  a  va 2  s . c  o  m*/
        protected void onSubmit() {
            if (user.isLoginPersistent()) {
                CookieDefaults settings = new CookieDefaults();
                // 14 days
                settings.setMaxAge(60 * 60 * 24 * 14);
                CookieUtils cookieUtils = new CookieUtils(settings);
                cookieUtils.save("loginUsername", user.getUsername());
                cookieUtils.save("loginPassword", user.getPassword());
            }
            authenticated();
        }
    };
    add(loginForm);

    loginForm.add(new FeedbackPanel("loginMessages", new ContainerFeedbackMessageFilter(loginForm)));

    final TextField<String> usernameField = new TextField<String>("usernameField",
            new PropertyModel<String>(user, "username"));
    loginForm.add(usernameField);

    final PasswordTextField passwordField = new PasswordTextField("passwordField",
            new PropertyModel<String>(user, "password"));
    loginForm.add(passwordField);

    CheckBox persistentField = new CheckBox("persistentField",
            new PropertyModel<Boolean>(user, "loginPersistent"));
    loginForm.add(persistentField);

    loginForm.add(new AbstractFormValidator() {
        private static final long serialVersionUID = 1L;

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent<?>[] { usernameField, passwordField };
        }

        @Override
        public void validate(Form<?> form) {
            boolean authenticated = ScoreBoardSession.get().authenticate(usernameField.getInput(),
                    passwordField.getInput());
            if (false == authenticated) {
                error(usernameField, "incorrectUsernamePassword");
            }
        }
    });
}

From source file:org.devproof.portal.core.module.user.page.SettingsPage.java

License:Apache License

private AbstractFormValidator createMissingCurrentPasswordValidator() {
    return new AbstractFormValidator() {
        private static final long serialVersionUID = 1L;

        @Override// w ww. ja v a  2s . c  om
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent[] { currentPassword, newPassword1 };
        }

        @Override
        public void validate(Form<?> form) {
            if (StringUtils.isNotEmpty(newPassword1.getValue())
                    && StringUtils.isEmpty(currentPassword.getValue())) {
                error(currentPassword, "currentPassword.required");
            }
        }
    };
}

From source file:org.geoserver.gwc.web.blob.BlobStorePage.java

License:Open Source License

public BlobStorePage(final BlobStoreConfig originalStore) {

    final List<String> assignedLayers = new ArrayList<String>();

    add(dialog = new GeoServerDialog("confirmDisableDialog"));
    dialog.setTitle(new ParamResourceModel("confirmDisableDialog.title", getPage()));
    dialog.setInitialHeight(200);//from   ww  w  .  ja  v a2s .  com

    typeOfBlobStore = new DropDownChoice<BlobStoreType>("typeOfBlobStore", new Model<BlobStoreType>(),
            BlobStoreTypes.getAll());
    typeOfBlobStore.setOutputMarkupId(true);
    typeOfBlobStore.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        private static final long serialVersionUID = 359589121400814043L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            blobStoreForm.setVisible(typeOfBlobStore.getModelObject() != null);
            if (typeOfBlobStore.getModelObject() != null) {
                blobStoreForm.getModel().setObject(typeOfBlobStore.getModelObject().newConfigObject());
                blobStoreForm.addOrReplace(typeOfBlobStore.getModelObject().createPanel("blobSpecificPanel",
                        blobStoreForm.getModel()));
            }

            target.addComponent(blobConfigContainer);
        }

    });
    typeOfBlobStore.add(new AttributeModifier("title", true, new ResourceModel("typeOfBlobStore.title")));

    Form selector = new Form("selector");
    selector.add(typeOfBlobStore);
    add(selector);

    blobConfigContainer = new WebMarkupContainer("blobConfigContainer");
    blobConfigContainer.setOutputMarkupId(true);
    add(blobConfigContainer);

    blobStoreForm = new Form<BlobStoreConfig>("blobStoreForm",
            new CompoundPropertyModel<BlobStoreConfig>(originalStore == null ? null : originalStore.clone()));
    blobConfigContainer.add(blobStoreForm);
    blobStoreForm.setVisible(originalStore != null);

    blobStoreForm.add((tfId = new TextField<String>("id")).setRequired(true));
    tfId.add(new AttributeModifier("title", true, new ResourceModel("id.title")));
    blobStoreForm.add(cbEnabled = new CheckBox("enabled"));
    cbEnabled.add(new AttributeModifier("title", true, new ResourceModel("enabled.title")));
    blobStoreForm.add(cbDefault = new CheckBox("default"));
    cbDefault.add(new AttributeModifier("title", true, new ResourceModel("default.title")));

    if (originalStore != null) {
        typeOfBlobStore.getModel().setObject(BlobStoreTypes.getFromClass(originalStore.getClass()));
        blobStoreForm.addOrReplace(
                typeOfBlobStore.getModelObject().createPanel("blobSpecificPanel", blobStoreForm.getModel()));
        typeOfBlobStore.setEnabled(false);

        for (TileLayer layer : GWC.get().getTileLayers()) {
            if (originalStore.getId().equals(layer.getBlobStoreId())) {
                assignedLayers.add(layer.getName());
            }
        }
    }

    blobStoreForm.add(new AbstractFormValidator() {
        private static final long serialVersionUID = 5240602030478856537L;

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent<?>[] { cbDefault, cbEnabled };
        }

        @Override
        public void validate(Form<?> form) {
            BlobStoreConfig blobStore = (BlobStoreConfig) form.getModelObject();
            if (blobStore.isDefault() && !cbDefault.getConvertedInput()) {
                form.error(new ParamResourceModel("defaultError", getPage()).getString());
            } else if (cbDefault.getConvertedInput() && !cbEnabled.getConvertedInput()) {
                form.error(new ParamResourceModel("enabledError", getPage()).getString());
            }
        }
    });

    blobStoreForm.add(new AbstractFormValidator() {
        private static final long serialVersionUID = 5240602030478856537L;

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent<?>[] { tfId };
        }

        @Override
        public void validate(Form<?> form) {
            for (BlobStoreConfig otherBlobStore : GWC.get().getBlobStores()) {
                if (otherBlobStore != originalStore
                        && otherBlobStore.getId().equals(tfId.getConvertedInput())) {
                    form.error(new ParamResourceModel("duplicateIdError", getPage()).getString());
                }
            }
        }
    });

    // build the submit/cancel
    blobStoreForm.add(new AjaxSubmitLink("save") {
        private static final long serialVersionUID = 3735176778941168701L;

        @Override
        public void onSubmit(AjaxRequestTarget target, Form<?> form) {

            final BlobStoreConfig blobStore = (BlobStoreConfig) getForm().getModelObject();

            if (originalStore != null && originalStore.isEnabled() && !blobStore.isEnabled()
                    && assignedLayers.size() > 0) {
                dialog.showOkCancel(target, new GeoServerDialog.DialogDelegate() {
                    private static final long serialVersionUID = 5257987095800108993L;

                    private boolean success;

                    private String error = null;

                    @Override
                    protected Component getContents(String id) {
                        StringBuilder sb = new StringBuilder();
                        sb.append(
                                new ParamResourceModel("confirmDisableDialog.content", getPage()).getString());
                        for (String layer : assignedLayers) {
                            sb.append("\n&nbsp;&nbsp;");
                            sb.append(escapeHtml(layer));
                        }
                        return new MultiLineLabel("userPanel", sb.toString()).setEscapeModelStrings(false);
                    }

                    @Override
                    protected boolean onSubmit(AjaxRequestTarget target, Component contents) {
                        try {
                            save(originalStore, blobStore, assignedLayers);
                            success = true;
                        } catch (ConfigurationException e) {
                            error = e.getMessage();
                        }
                        return true;
                    }

                    @Override
                    public void onClose(AjaxRequestTarget target) {
                        if (success) {
                            doReturn(BlobStoresPage.class);
                        } else if (error != null) {
                            error(error);
                            target.addComponent(feedbackPanel);
                        }
                    }

                });
            } else {
                try {
                    save(originalStore, blobStore, assignedLayers);
                    doReturn(BlobStoresPage.class);
                } catch (ConfigurationException e) {
                    error(e.getMessage());
                    target.addComponent(feedbackPanel);
                }
            }
        }

        protected void onError(AjaxRequestTarget target, Form<?> form) {
            target.addComponent(feedbackPanel);
        }
    });
    blobStoreForm.add(new BookmarkablePageLink<BlobStoreConfig>("cancel", BlobStoresPage.class));

}

From source file:org.geoserver.opensearch.eo.web.OSEOAdminPage.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
protected void build(final IModel info, Form form) {
    OSEOInfo model = (OSEOInfo) info.getObject();

    this.backend = null;
    if (model.getOpenSearchAccessStoreId() != null) {
        this.backend = getCatalog().getDataStore(model.getOpenSearchAccessStoreId());
    }/*from  ww  w .  java 2 s.c  o m*/
    DropDownChoice<DataStoreInfo> openSearchAccessReference = new DropDownChoice<>("openSearchAccessId",
            new PropertyModel<DataStoreInfo>(this, "backend"), new OpenSearchAccessListModel(),
            new StoreListChoiceRenderer());
    form.add(openSearchAccessReference);

    final TextField<Integer> recordsPerPage = new TextField<>("recordsPerPage", Integer.class);
    recordsPerPage.add(RangeValidator.minimum(0));
    recordsPerPage.setRequired(true);
    form.add(recordsPerPage);
    final TextField<Integer> maximumRecordsPerPage = new TextField<>("maximumRecordsPerPage", Integer.class);
    maximumRecordsPerPage.add(RangeValidator.minimum(0));
    maximumRecordsPerPage.setRequired(true);
    form.add(maximumRecordsPerPage);
    // check that records is lower or equal than maximum
    form.add(new AbstractFormValidator() {

        @Override
        public void validate(Form<?> form) {
            Integer records = recordsPerPage.getConvertedInput();
            Integer maximum = maximumRecordsPerPage.getConvertedInput();
            if (recordsPerPage != null && maximum != null && records > maximum) {
                form.error(new ParamResourceModel("recordsGreaterThanMaximum", form, records, maximum));
            }
        }

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent<?>[] { recordsPerPage, maximumRecordsPerPage };
        }
    });
}

From source file:org.openengsb.ui.common.editor.ServiceEditorPanel.java

License:Apache License

/**
 * attach a ServiceValidator to the given form. This formValidator is meant to validate the fields in context to
 * each other. This validation is only done on submit.
 *//*from   w w w.  j  a v a  2s  .  co m*/
public void attachFormValidator(final Form<?> form, final FormValidator validator) {
    form.add(new AbstractFormValidator() {

        @Override
        public void validate(Form<?> form) {
            Map<String, FormComponent<?>> loadFormComponents = loadFormComponents(form);
            Map<String, String> toValidate = new HashMap<String, String>();
            for (Map.Entry<String, FormComponent<?>> entry : loadFormComponents.entrySet()) {
                toValidate.put(entry.getKey(), entry.getValue().getValue());
            }
            try {
                validator.validate(toValidate);
            } catch (ConnectorValidationFailedException e) {
                Map<String, String> attributeErrorMessages = e.getErrorMessages();
                for (Map.Entry<String, String> entry : attributeErrorMessages.entrySet()) {
                    FormComponent<?> fc = loadFormComponents.get(entry.getKey());
                    fc.error((IValidationError) new ValidationError().setMessage(entry.getValue()));
                }
            }

        }

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            Collection<FormComponent<?>> formComponents = loadFormComponents(form).values();
            return formComponents.toArray(new FormComponent<?>[formComponents.size()]);
        }

        private Map<String, FormComponent<?>> loadFormComponents(final Form<?> form) {
            Map<String, FormComponent<?>> formComponents = new HashMap<String, FormComponent<?>>();
            if (validator != null) {
                for (String attribute : validator.fieldsToValidate()) {
                    Component component = form.get("attributesPanel:fields:" + attribute + ":row:field");
                    if (component instanceof FormComponent<?>) {
                        formComponents.put(attribute, (FormComponent<?>) component);
                    }
                }
            }
            return formComponents;
        }
    });
}

From source file:org.tomochika1985.rssreader.ListPage.java

License:Apache License

@SuppressWarnings("serial")
void createUrlForm() {
    final Form<Void> urlForm = new Form<Void>("urlForm");
    add(urlForm);/* w w w  .j a v a 2 s .com*/
    final TextField<String> txtFld = new TextField<String>("urlTextField", new Model<String>());
    txtFld.setRequired(true);
    txtFld.add(new PatternValidator("^http://.*$"));
    urlForm.add(txtFld);

    final CheckBox check1 = new CheckBox("check1", new Model<Boolean>(true));
    final CheckBox check2 = new CheckBox("check2", new Model<Boolean>(true));
    urlForm.add(check1);
    urlForm.add(check2);

    add(new FeedbackPanel("feedback"));

    urlForm.add(new Button("submitBtn") {
        @Override
        public void onSubmit() {
            Component urlField = this.getParent().get("urlTextField");
            String newUrlStr = urlField.getDefaultModelObjectAsString();
            info("?: " + newUrlStr);
        }
    });
    urlForm.add(new AbstractFormValidator() {
        @Override
        public void validate(Form<?> form) {
            if (!check1.getConvertedInput() && !check2.getConvertedInput()) {
                urlForm.error("????????");
            }
        }

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent<?>[] { check1, check2 };
        }
    });
}