Search in sources :

Example 1 with BeanValidationContextImpl

use of org.apache.tapestry5.internal.BeanValidationContextImpl in project tapestry-5 by apache.

the class Form method beginRender.

void beginRender(MarkupWriter writer) {
    Link link = resources.createFormEventLink(EventConstants.ACTION, context);
    String actionURL = secure && secureEnabled ? link.toAbsoluteURI(true) : link.toURI();
    actionSink = new ComponentActionSink(logger, clientDataEncoder);
    clientId = javascriptSupport.allocateClientId(resources);
    // Pre-register some names, to prevent client-side collisions with function names
    // attached to the JS Form object.
    IdAllocator allocator = new IdAllocator();
    preallocateNames(allocator);
    formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
    environment.push(FormSupport.class, formSupport);
    environment.push(ValidationTracker.class, tracker);
    if (autofocus) {
        ValidationDecorator autofocusDecorator = new AutofocusValidationDecorator(environment.peek(ValidationDecorator.class), tracker, javascriptSupport);
        environment.push(ValidationDecorator.class, autofocusDecorator);
    }
    // Now that the environment is setup, inform the component or other
    // listeners that the form
    // is about to render.
    resources.triggerEvent(EventConstants.PREPARE_FOR_RENDER, context, null);
    resources.triggerEvent(EventConstants.PREPARE, context, null);
    // Push BeanValidationContext only after the container had a chance to prepare
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
    // Save the form element for later, in case we want to write an encoding
    // type attribute.
    form = writer.element("form", "id", clientId, "method", "post", "action", actionURL, "data-update-zone", zone, DATA_ATTRIBUTE, DATA_ATTRIBUTE_VALUE);
    if (clientValidation != ClientValidation.NONE) {
        writer.attributes("data-validate", "submit");
    }
    if (async) {
        javascriptSupport.require("t5/core/zone");
        writer.attributes("data-async-trigger", true);
    }
    resources.renderInformalParameters(writer);
    div = writer.element("div");
    for (String parameterName : link.getParameterNames()) {
        String[] values = link.getParameterValues(parameterName);
        for (String value : values) {
            // but the input value shouldn't be encoded.
            try {
                value = URLDecoder.decode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                logger.error("Enable to decode parameter value for parameter {} in form {}", parameterName, form.getName(), e);
            }
            writer.element("input", "type", "hidden", "name", parameterName, "value", value);
            writer.end();
        }
    }
    // div
    writer.end();
    environment.peek(Heartbeat.class).begin();
}
Also used : ComponentActionSink(org.apache.tapestry5.corelib.internal.ComponentActionSink) IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Heartbeat(org.apache.tapestry5.services.Heartbeat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Link(org.apache.tapestry5.http.Link) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 2 with BeanValidationContextImpl

use of org.apache.tapestry5.internal.BeanValidationContextImpl in project tapestry-5 by apache.

the class BeanEditor method doPrepare.

/**
 * Used to initialize the model if necessary, to instantiate the object being edited if necessary, and to push the
 * BeanEditContext into the environment.
 */
void doPrepare() {
    if (model == null) {
        Class type = resources.getBoundType("object");
        model = modelSource.createEditModel(type, overrides.getOverrideMessages());
        BeanModelUtils.modify(model, add, include, exclude, reorder);
    }
    if (object == null) {
        try {
            object = model.newInstance();
        } catch (Exception ex) {
            String message = String.format("Exception instantiating instance of %s (for component '%s'): %s", PlasticUtils.toTypeName(model.getBeanType()), resources.getCompleteId(), ex);
            throw new TapestryException(message, resources.getLocation(), ex);
        }
    }
    BeanEditContext context = new BeanEditContextImpl(model.getBeanType());
    cachedObject = object;
    environment.push(BeanEditContext.class, context);
    // TAP5-2101: Always provide a new BeanValidationContext
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(object));
}
Also used : BeanEditContextImpl(org.apache.tapestry5.internal.BeanEditContextImpl) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) BeanEditContext(org.apache.tapestry5.services.BeanEditContext) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 3 with BeanValidationContextImpl

use of org.apache.tapestry5.internal.BeanValidationContextImpl in project tapestry-5 by apache.

the class Form method onAction.

@SuppressWarnings({ "unchecked", "InfiniteLoopStatement" })
Object onAction(EventContext context) throws IOException {
    beforeProcessSubmit(context);
    tracker.clear();
    formSupport = new FormSupportImpl(resources, validationId);
    environment.push(ValidationTracker.class, tracker);
    environment.push(FormSupport.class, formSupport);
    Heartbeat heartbeat = new HeartbeatImpl();
    environment.push(Heartbeat.class, heartbeat);
    heartbeat.begin();
    boolean didPushBeanValidationContext = false;
    try {
        resources.triggerContextEvent(EventConstants.PREPARE_FOR_SUBMIT, context, eventCallback);
        if (eventCallback.isAborted())
            return true;
        resources.triggerContextEvent(EventConstants.PREPARE, context, eventCallback);
        if (eventCallback.isAborted())
            return true;
        if (isFormCancelled()) {
            executeStoredActions(true);
            resources.triggerContextEvent(EventConstants.CANCELED, context, eventCallback);
            if (eventCallback.isAborted())
                return true;
        }
        environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
        didPushBeanValidationContext = true;
        executeStoredActions(false);
        heartbeat.end();
        formSupport.executeDeferred();
        fireValidateEvent(EventConstants.VALIDATE, context, eventCallback);
        if (eventCallback.isAborted()) {
            return true;
        }
        afterValidate();
        if (!tracker.getHasErrors()) {
            tracker.clear();
        }
        String eventType = tracker.getHasErrors() ? EventConstants.FAILURE : EventConstants.SUCCESS;
        resources.triggerContextEvent(eventType, context, eventCallback);
        if (eventCallback.isAborted()) {
            return true;
        }
        // Lastly, tell anyone whose interested that the form is completely
        // submitted.
        resources.triggerContextEvent(EventConstants.SUBMIT, context, eventCallback);
        afterSuccessOrFailure();
        if (eventCallback.isAborted()) {
            return true;
        }
        if (tracker.getHasErrors() && !request.isXHR()) {
            return STREAM_ACTIVE_PAGE_CONTENT;
        }
        return false;
    } finally {
        environment.pop(Heartbeat.class);
        environment.pop(FormSupport.class);
        environment.pop(ValidationTracker.class);
        if (didPushBeanValidationContext) {
            environment.pop(BeanValidationContext.class);
        }
    }
}
Also used : Heartbeat(org.apache.tapestry5.services.Heartbeat) FormSupportImpl(org.apache.tapestry5.corelib.internal.FormSupportImpl) HeartbeatImpl(org.apache.tapestry5.internal.services.HeartbeatImpl) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Aggregations

BeanValidationContextImpl (org.apache.tapestry5.internal.BeanValidationContextImpl)3 Heartbeat (org.apache.tapestry5.services.Heartbeat)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 ComponentActionSink (org.apache.tapestry5.corelib.internal.ComponentActionSink)1 FormSupportImpl (org.apache.tapestry5.corelib.internal.FormSupportImpl)1 Link (org.apache.tapestry5.http.Link)1 BeanEditContextImpl (org.apache.tapestry5.internal.BeanEditContextImpl)1 HeartbeatImpl (org.apache.tapestry5.internal.services.HeartbeatImpl)1 AutofocusValidationDecorator (org.apache.tapestry5.internal.util.AutofocusValidationDecorator)1 IdAllocator (org.apache.tapestry5.ioc.util.IdAllocator)1 BeanEditContext (org.apache.tapestry5.services.BeanEditContext)1