Package net.sourceforge.stripes.validation

Examples of net.sourceforge.stripes.validation.ValidationErrors


        squad.setMembers(loadedMembers);
        squad.setLeader(leader);
        service.create(squad);
        return new RedirectResolution(this.getClass(), "all");           
        }catch(java.lang.NullPointerException e){
            ValidationErrors errs = new ValidationErrors();
            errs.add("membersCount", new LocalizableError("validation.membersCount.noMember"));
            context.setValidationErrors(errs);
            return context.getSourcePageResolution();
        }
       
    }
View Full Code Here


     *
     * @return a Collection of validation errors
     */
    public ValidationErrors getValidationErrors() {
        if (this.validationErrors == null) {
            this.validationErrors = new ValidationErrors();
        }

        return validationErrors;
    }
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

    @Override
    public int doStartTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest();
        ActionBean mainBean = (ActionBean) request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);
        FormTag formTag = getParentTag(FormTag.class);
        ValidationErrors errors = null;

        // If we are supplied with an 'action' attribute then display the errors
        // only if that action matches the 'action' of the current action bean
        if (getAction() != null) {
            if (mainBean != null) {
                String mainAction = StripesFilter.getConfiguration()
                        .getActionResolver().getUrlBinding(mainBean.getClass());

                if (getAction().equals(mainAction)) {
                    errors = mainBean.getContext().getValidationErrors();
                }
            }
        }
        // Else we don't have an 'action' attribute, so see if we are nested in
        // a form tag
        else if (formTag != null) {
            ActionBean formBean = formTag.getActionBean();
            if (formBean != null) {
                errors = formBean.getContext().getValidationErrors();
            }

        }
        // Else if no name was set, and we're not in a action tag, we're global and ok to display
        else if (mainBean != null) {
            errors = mainBean.getContext().getValidationErrors();
        }

        // If we found some errors that are applicable for display, figure out what to do
        if (errors != null) {
            // Using a set ensures that duplicate messages get filtered out, which can
            // happen during multi-row validation
            this.allErrors = new TreeSet<ValidationError>(new ErrorComparator());

            if (this.field != null) {
                // we're filtering for a specific field
                List<ValidationError> fieldErrors = errors.get(this.field);
                if (fieldErrors != null) {
                    this.allErrors.addAll(fieldErrors);
                }
            }
            else if (this.globalErrorsOnly) {
                List<ValidationError> globalErrors = errors.get(ValidationErrors.GLOBAL_ERROR);
                if (globalErrors != null) {
                    this.allErrors.addAll(globalErrors);
                }
            }
            else {
                for (List<ValidationError> fieldErrors : errors.values()) {
                    if (fieldErrors != null) {
                        this.allErrors.addAll(fieldErrors);
                    }
                }
            }
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

    protected boolean isFormInError(InputTagSupport tag) throws StripesJspException {
        boolean inError = false;

        ActionBean actionBean = tag.getParentFormTag().getActionBean();
        if (actionBean != null) {
            ValidationErrors errors = actionBean.getContext().getValidationErrors();
            inError = (errors != null && errors.size() > 0);
        }

        return inError;
    }
View Full Code Here

        ActionBeanContext context = actionBean.getContext();
        if (context == null)
            return false;

        ValidationErrors errors = context.getValidationErrors();
        if (errors == null || errors.isEmpty())
            return false;

        List<ValidationError> fieldErrors = errors.get(field);
        return fieldErrors != null && fieldErrors.size() > 0;
    }
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) {
                resolution = context.getSourcePageResolution();
            }
        }

        return resolution;
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

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.