Search in sources :

Example 6 with ValidationException

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

the class RequiredTest method blank_value.

@Test
public void blank_value() {
    MessageFormatter formatter = mockMessageFormatter();
    Field field = mockFieldWithLabel("My Field");
    train_format(formatter, "{message}", "My Field");
    Html5Support html5Support = mockHtml5Support();
    replay();
    try {
        new Required(null, html5Support).validate(field, null, formatter, "");
        unreachable();
    } catch (ValidationException ex) {
        assertEquals(ex.getMessage(), "{message}");
    }
    verify();
}
Also used : Field(org.apache.tapestry5.Field) Html5Support(org.apache.tapestry5.services.Html5Support) ValidationException(org.apache.tapestry5.ValidationException) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) Test(org.testng.annotations.Test)

Example 7 with ValidationException

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

the class RequiredTest method empty_collection_value.

@Test
public void empty_collection_value() {
    MessageFormatter formatter = mockMessageFormatter();
    Field field = mockFieldWithLabel("My Field");
    train_format(formatter, "{message}", "My Field");
    Html5Support html5Support = mockHtml5Support();
    replay();
    try {
        new Required(null, html5Support).validate(field, null, formatter, Arrays.asList());
        unreachable();
    } catch (ValidationException ex) {
        assertEquals(ex.getMessage(), "{message}");
    }
    verify();
}
Also used : Field(org.apache.tapestry5.Field) Html5Support(org.apache.tapestry5.services.Html5Support) ValidationException(org.apache.tapestry5.ValidationException) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) Test(org.testng.annotations.Test)

Example 8 with ValidationException

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

the class Select method onChange.

Object onChange(final EventContext context, @RequestParameter(value = "t:selectvalue", allowBlank = true) final String selectValue) throws ValidationException {
    final Object newValue = toValue(selectValue);
    CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
    EventContext newContext = new AbstractEventContext() {

        @Override
        public int getCount() {
            return context.getCount() + 1;
        }

        @Override
        public <T> T get(Class<T> desiredType, int index) {
            if (index == 0) {
                return typeCoercer.coerce(newValue, desiredType);
            }
            return context.get(desiredType, index - 1);
        }
    };
    this.resources.triggerContextEvent(EventConstants.VALUE_CHANGED, newContext, callback);
    this.value = newValue;
    return callback.getResult();
}
Also used : AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext) CaptureResultCallback(org.apache.tapestry5.internal.util.CaptureResultCallback) AbstractEventContext(org.apache.tapestry5.internal.AbstractEventContext)

Example 9 with ValidationException

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

the class Palette method processSubmission.

@Override
protected void processSubmission(String controlName) {
    String parameterValue = request.getParameter(controlName);
    JSONArray values = new JSONArray(parameterValue);
    // Use a couple of local variables to cut down on access via bindings
    Collection<Object> selected = this.selected;
    selected.clear();
    ValueEncoder encoder = this.encoder;
    // TODO: Validation error if the model does not contain a value.
    int count = values.length();
    for (int i = 0; i < count; i++) {
        String value = values.getString(i);
        Object objectValue = encoder.toValue(value);
        selected.add(objectValue);
    }
    putPropertyNameIntoBeanValidationContext("selected");
    try {
        fieldValidationSupport.validate(selected, resources, validate);
        this.selected = selected;
    } catch (final ValidationException e) {
        validationTracker.recordError(this, e.getMessage());
    }
    removePropertyNameFromBeanValidationContext();
}
Also used : JSONArray(org.apache.tapestry5.json.JSONArray)

Example 10 with ValidationException

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

Aggregations

ValidationException (org.apache.tapestry5.ValidationException)12 Test (org.testng.annotations.Test)12 Field (org.apache.tapestry5.Field)10 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)9 Html5Support (org.apache.tapestry5.services.Html5Support)4 Messages (org.apache.tapestry5.commons.Messages)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 Request (org.apache.tapestry5.http.services.Request)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)2 ValueEncoderSource (org.apache.tapestry5.services.ValueEncoderSource)2 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)2 EnumSelectModel (org.apache.tapestry5.util.EnumSelectModel)2 ParseException (java.text.ParseException)1 Date (java.util.Date)1 Pattern (java.util.regex.Pattern)1 ConstraintViolation (javax.validation.ConstraintViolation)1 Validator (javax.validation.Validator)1 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)1 FieldValidator (org.apache.tapestry5.FieldValidator)1 Translator (org.apache.tapestry5.Translator)1