Package net.sourceforge.stripes.validation

Examples of net.sourceforge.stripes.validation.ValidationErrors


     * them in an instance variable to use during error rendering.
     */
    protected void loadErrors() throws StripesJspException {
        ActionBean actionBean = getActionBean();
        if (actionBean != null) {
            ValidationErrors validationErrors = actionBean.getContext().getValidationErrors();

            if (validationErrors != null) {
                this.fieldErrors = validationErrors.get(getName());
            }
        }
    }
View Full Code Here


    }
   
       
    @ValidationMethod(on = "save")
    public void validatePayInformation() {
        ValidationErrors errors= getContext().getValidationErrors();
        if(selectedProduct.getPrice().compareTo(BigDecimal.ZERO)>0)
        {
            if(getCvv2()==null){
                errors.add("cvv2", new LocalizableError("com.zesped.action.EngageCredit.EmptyCvv2"));
            }
            if(getCardnumber()==null){
                errors.add("cardnumber", new LocalizableError("com.zesped.action.EngageCredit.EmptyCardnumber"));
            }
            if(getCardholder()==null){
                errors.add("cardholder", new LocalizableError("com.zesped.action.EngageCredit.EmptyCardholder"));
            }
        }
       
    }
View Full Code Here

     * @param context the ActionBeanContext of the current request
     * @param validate true indicates that validation should be run, false indicates that only type
     *            conversion should occur
     */
    public ValidationErrors bind(ActionBean bean, ActionBeanContext context, boolean validate) {
        ValidationErrors fieldErrors = context.getValidationErrors();
        Map<String, ValidationMetadata> validationInfos = this.configuration
                .getValidationMetadataProvider().getValidationMetadata(bean.getClass());

        // Take the ParameterMap and turn the keys into ParameterNames
        Map<ParameterName, String[]> parameters = getParameters(bean);

        // Run the required validation first to catch fields that weren't even submitted
        if (validate) {
            validateRequiredFields(parameters, bean, fieldErrors);
        }

        // Converted values for all fields are accumulated in this map to make post-conversion
        // validation go a little easier
        Map<ParameterName, List<Object>> allConvertedFields = new TreeMap<ParameterName, List<Object>>();

        // First we bind all the regular parameters
        for (Map.Entry<ParameterName, String[]> entry : parameters.entrySet()) {
            List<Object> convertedValues = null;
            ParameterName name = entry.getKey();

            try {
                String pname = name.getName(); // exact name of the param in the request
                if (!StripesConstants.SPECIAL_URL_KEYS.contains(pname)
                        && !fieldErrors.containsKey(pname)) {
                    log.trace("Running binding for property with name: ", name);

                    // Determine the target type
                    ValidationMetadata validationInfo = validationInfos.get(name.getStrippedName());
                    PropertyExpressionEvaluation eval;
                    try {
                        eval = new PropertyExpressionEvaluation(PropertyExpression
                                .getExpression(pname), bean);
                    }
                    catch (Exception e) {
                        if (pname.equals(context.getEventName()))
                            continue;
                        else
                            throw e;
                    }
                    Class<?> type = eval.getType();
                    Class<?> scalarType = eval.getScalarType();

                    // Check to see if binding into this expression is permitted
                    if (!isBindingAllowed(eval))
                        continue;

                    if (type == null
                            && (validationInfo == null || validationInfo.converter() == null)) {
                        if (!pname.equals(context.getEventName())) {
                            log.trace("Could not find type for property '", name.getName(),
                                    "' of '", bean.getClass().getSimpleName(),
                                    "' probably because it's not ",
                                    "a property of the bean.  Skipping binding.");
                        }
                        continue;
                    }
                    String[] values = entry.getValue();

                    // Do Validation and type conversion
                    List<ValidationError> errors = new ArrayList<ValidationError>();

                    // If the property should be ignored, skip to the next property
                    if (validationInfo != null && validationInfo.ignore()) {
                        continue;
                    }

                    if (validate && validationInfo != null) {
                        doPreConversionValidations(name, values, validationInfo, errors);
                    }

                    // Only do type conversion if there aren't errors already
                    if (errors.isEmpty()) {
                        convertedValues = convert(bean, name, values, type, scalarType, validationInfo, errors);
                        allConvertedFields.put(name, convertedValues);
                    }

                    // If we have errors, save them, otherwise bind the parameter to the form
                    if (errors.size() > 0) {
                        fieldErrors.addAll(name.getName(), errors);
                    }
                    else if (convertedValues.size() > 0) {
                        bindNonNullValue(bean, eval, convertedValues, type, scalarType);
                    }
                    else {
View Full Code Here

     * @return a Resolution if any interceptor determines that the request processing should
     *         be aborted in favor of another Resolution, null otherwise.
     */
    public static Resolution doCustomValidation(final ExecutionContext ctx,
                                                final boolean alwaysInvokeValidate) throws Exception {
        final ValidationErrors errors = ctx.getActionBeanContext().getValidationErrors();
        final ActionBean bean = ctx.getActionBean();
        final Method handler = ctx.getHandler();
        final boolean doBind = handler != null && handler.getAnnotation(DontBind.class) == null;
        final boolean doValidate = doBind && handler.getAnnotation(DontValidate.class) == null;
        Configuration config = StripesFilter.getConfiguration();

        // Run the bean's methods annotated with @ValidateMethod if the following conditions are met:
        //   l. This event is not marked to bypass binding
        //   2. This event is not marked to bypass validation (doValidate == true)
        //   3. We have no errors so far OR alwaysInvokeValidate is true
        if (doValidate) {

            ctx.setLifecycleStage(LifecycleStage.CustomValidation);
            ctx.setInterceptors(config.getInterceptors(LifecycleStage.CustomValidation));

            return ctx.wrap( new Interceptor() {
        public Resolution intercept(ExecutionContext context) throws Exception {
                    // Run any of the annotated validation methods
                    Method[] validations = findCustomValidationMethods(bean.getClass());
                    for (Method validation : validations) {
                        ValidationMethod ann = validation.getAnnotation(ValidationMethod.class);

                        boolean run = (ann.when() == ValidationState.ALWAYS)
                                   || (ann.when() == ValidationState.DEFAULT && alwaysInvokeValidate)
                                   || errors.isEmpty();

                        if (run && applies(ann, ctx.getActionBeanContext().getEventName())) {
                            Class<?>[] args = validation.getParameterTypes();
                            if (args.length == 1 && args[0].equals(ValidationErrors.class)) {
                                validation.invoke(bean, errors);
View Full Code Here

        Resolution resolution = null;
        if (doValidate) {
            ActionBean bean = ctx.getActionBean();
            ActionBeanContext context = ctx.getActionBeanContext();
            ValidationErrors errors = context.getValidationErrors();

            // Now if we have errors and the bean wants to handle them...
            if (errors.size() > 0 && bean instanceof ValidationErrorHandler) {
                resolution = ((ValidationErrorHandler) bean).handleValidationErrors(errors);
                fillInValidationErrors(ctx);
            }

            // If there are still errors see if we need to lookup the resolution
            if (errors.size() > 0 && resolution == null) {
                logValidationErrors(context);
                resolution = context.getSourcePageResolution();
            }
        }
View Full Code Here

     *
     * @param ctx the ExecutionContext being used to process the current request
     */
    public static void fillInValidationErrors(ExecutionContext ctx) {
        ActionBeanContext context = ctx.getActionBeanContext();
        ValidationErrors errors = context.getValidationErrors();

        if (errors.size() > 0) {
            String formAction = StripesFilter.getConfiguration().getActionResolver()
                    .getUrlBinding(ctx.getActionBean().getClass());
            HttpServletRequest request = ctx.getActionBeanContext().getRequest();

            /** Since we don't pass form action down the stack, we add it to the errors here. */
            for (Map.Entry<String, List<ValidationError>> entry : errors.entrySet()) {
                String parameterName = entry.getKey();
                List<ValidationError> listOfErrors = entry.getValue();

                for (ValidationError error : listOfErrors) {
                    // Make sure we process each error only once, no matter how often we're called
View Full Code Here

        trip.addParameter("two", "not25chars");
        trip.addParameter("three", "3");
        trip.addParameter("four", "onetwothree");
        trip.execute("/Validate.action");

        ValidationErrors errors = trip.getValidationErrors();
        Assert.assertNull(errors.get("one"), "Field one should not have errors.");
        Assert.assertEquals(errors.get("two").size(), 1, "Field two should not have 1 error.");
        Assert.assertEquals(errors.get("three").size(), 1, "Field three should not have errors.");
        Assert.assertEquals(errors.get("four").size(), 1, "Field one should not have errors.");
    }
View Full Code Here

     */
    protected void setFocusOnFieldIfRequired(InputTagSupport tag) {
        // Decide whether or not this field should be focused
        if (this.focus != null && !this.focusSet) {
            ActionBean bean = getActionBean();
            ValidationErrors errors = bean == null ? null : bean.getContext().getValidationErrors();

            // If there are validation errors, select the first field in error
            if (errors != null && errors.hasFieldErrors()) {
                List<ValidationError> fieldErrors = errors.get(tag.getName());
                if (fieldErrors != null && fieldErrors.size() > 0) {
                    tag.setFocus(true);
                    this.focusSet = true;
                }
            }
View Full Code Here

    public Resolution onSave() {
        User user = userService.findUser(sessionUser);

        boolean passwordMatch = this.formUser.isPasswordMatch();
        boolean compliant = this.formUser.isPasswordPolicyCompliant();
        ValidationErrors errors = new ValidationErrors();

        if (!passwordMatch) {
            errors.add("password", new LocalizableError("password.nomatch"));
        }

        if (!compliant) {
            errors.add("password", new SimpleError(passwordPolicy.getDescription()));
        }

        if (errors.size() == 0) {
            user.setFullName(this.formUser.getFullName());
            userService.saveWithPassword(user, this.formUser.getPassword());
            return new ForwardResolution(HOME_ACTION);
        } else {
            getContext().setValidationErrors(errors);
View Full Code Here

            if (value != null) {
                formUser.getRole(value).setAssigned(true);
            }
        }

        ValidationErrors passwordError = new ValidationErrors();

        if (formUser.hasPassword()) {
            boolean passwordMatch = this.formUser.isPasswordMatch();
            boolean compliant = this.formUser.isPasswordPolicyCompliant();

            if (!passwordMatch) {
                passwordError.add("password", new LocalizableError("password.nomatch"));
            }

            if (!compliant) {
                passwordError.add("password", new SimpleError(passwordPolicy.getDescription()));
            }
        }

        if (passwordError.size() == 0) {
            actualUser.setFullName(this.formUser.getFullName());
            actualUser.clearRoles();

            for (FormRole role : formUser.getRoles())  {
                if (role.isAssigned()) {
View Full Code Here

TOP

Related Classes of net.sourceforge.stripes.validation.ValidationErrors

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.