Search in sources :

Example 16 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class UploadTest method begin_render_invokes_field_validator.

@SuppressWarnings("unchecked")
@Test
public void begin_render_invokes_field_validator() throws Exception {
    getMocksControl().checkOrder(true);
    FieldValidator<Object> validate = mockFieldValidator();
    ComponentResources resources = mockComponentResources();
    Upload component = new Upload(null, validate, null, null, resources, null);
    MarkupWriter writer = createMarkupWriter();
    writer.element("form");
    Request request = mockRequest();
    FormSupport formSupport = mockFormSupport();
    formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
    ValidationDecorator decorator = mockValidationDecorator();
    component.injectDecorator(decorator).injectRequest(request).injectFormSupport(formSupport);
    validate.render(writer);
    resources.renderInformalParameters(writer);
    decorator.insideField(component);
    train_isXHR(request, false);
    replay();
    component.beginRender(writer);
    verify();
}
Also used : Request(org.apache.tapestry5.http.services.Request) FormSupport(org.apache.tapestry5.services.FormSupport) Test(org.testng.annotations.Test)

Example 17 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class UploadTest method process_submission_calls_validator.

@SuppressWarnings("unchecked")
@Test
public void process_submission_calls_validator() throws Exception {
    MultipartDecoder decoder = mockMultipartDecoder();
    UploadedFile uploadedFile = mockUploadedFile();
    FieldValidator<Object> validate = mockFieldValidator();
    ComponentResources resources = mockComponentResources();
    FieldValidationSupport support = mockFieldValidationSupport();
    Upload component = new Upload(null, validate, decoder, null, resources, support);
    expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
    expect(uploadedFile.getFileName()).andReturn("test").atLeastOnce();
    support.validate(uploadedFile, resources, validate);
    replay();
    component.processSubmission("test");
    verify();
}
Also used : UploadedFile(org.apache.tapestry5.upload.services.UploadedFile) MultipartDecoder(org.apache.tapestry5.upload.services.MultipartDecoder) Test(org.testng.annotations.Test)

Example 18 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class Upload method processSubmission.

@SuppressWarnings({ "unchecked" })
@Override
protected void processSubmission(String controlName) {
    UploadedFile uploaded = decoder.getFileUpload(controlName);
    if (uploaded != null && (uploaded.getFileName() == null || uploaded.getFileName().length() == 0)) {
        uploaded = null;
    }
    try {
        fieldValidationSupport.validate(uploaded, resources, validate);
    } catch (ValidationException ex) {
        validationTracker.recordError(this, ex.getMessage());
    }
    value = uploaded;
}
Also used : UploadedFile(org.apache.tapestry5.upload.services.UploadedFile)

Example 19 with Validate

use of org.apache.tapestry5.beaneditor.Validate 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)

Example 20 with Validate

use of org.apache.tapestry5.beaneditor.Validate in project tapestry-5 by apache.

the class Html5DateField method processSubmission.

@Override
protected void processSubmission(String controlName) {
    String value = request.getParameter(controlName);
    validationTracker.recordInput(this, value);
    Date parsedValue = null;
    try {
        if (InternalUtils.isNonBlank(value))
            parsedValue = getDateFormat().parse(value);
    } catch (ParseException ex) {
        validationTracker.recordError(this, messages.format("core-date-value-not-parseable", value));
        return;
    }
    putPropertyNameIntoBeanValidationContext("value");
    try {
        fieldValidationSupport.validate(parsedValue, resources, validate);
        this.value = parsedValue;
    } catch (ValidationException ex) {
        validationTracker.recordError(this, ex.getMessage());
    }
    removePropertyNameFromBeanValidationContext();
}
Also used : ValidationException(org.apache.tapestry5.ValidationException) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

Test (org.testng.annotations.Test)16 Field (org.apache.tapestry5.Field)7 ValidationException (org.apache.tapestry5.ValidationException)5 Validate (org.apache.tapestry5.beaneditor.Validate)5 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)5 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)5 PropertyConduit (org.apache.tapestry5.beanmodel.PropertyConduit)4 Html5Support (org.apache.tapestry5.services.Html5Support)4 ValidationConstraintGenerator (org.apache.tapestry5.services.ValidationConstraintGenerator)4 Messages (org.apache.tapestry5.commons.Messages)3 MultipartDecoder (org.apache.tapestry5.upload.services.MultipartDecoder)3 FieldValidator (org.apache.tapestry5.FieldValidator)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 BeanValidationContextImpl (org.apache.tapestry5.internal.BeanValidationContextImpl)2 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)2 Heartbeat (org.apache.tapestry5.services.Heartbeat)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1