Search in sources :

Example 1 with ConversionException

use of org.apache.wicket.util.convert.ConversionException in project wicket by apache.

the class PropertyResolverTest method conversionExceptionMessageContainsTheObjectPropertyBeingSet.

/**
 * @see <a href="https://issues.apache.org/jira/browse/WICKET-1802">WICKET-1802</a>
 */
@Test
public void conversionExceptionMessageContainsTheObjectPropertyBeingSet() {
    try {
        PropertyResolverConverter convertToNull = new PropertyResolverConverter(null, null) {

            private static final long serialVersionUID = 1L;

            @Override
            public <T> T convert(Object object, java.lang.Class<T> clz) {
                return null;
            }
        };
        PropertyResolver.setValue("name", person, "", convertToNull);
        fail("Should have thrown an ConversionException");
    } catch (ConversionException e) {
        assertTrue(e.getMessage().toLowerCase().contains("name"));
    }
}
Also used : ConversionException(org.apache.wicket.util.convert.ConversionException) PropertyResolverConverter(org.apache.wicket.core.util.lang.PropertyResolverConverter) Test(org.junit.Test)

Example 2 with ConversionException

use of org.apache.wicket.util.convert.ConversionException in project wicket by apache.

the class TimeField method convertInput.

@Override
public void convertInput() {
    Integer hours = hoursField.getConvertedInput();
    Integer minutes = minutesField.getConvertedInput();
    AM_PM amOrPmInput = amOrPmChoice.getConvertedInput();
    LocalTime localTime;
    if (hours == null && minutes == null) {
        localTime = null;
    } else if (hours != null && minutes != null) {
        // Use the input to create a LocalTime object
        localTime = LocalTime.of(hours, minutes);
        // Adjust for halfday if needed
        if (use12HourFormat()) {
            int halfday = (amOrPmInput == AM_PM.PM ? 1 : 0);
            localTime = localTime.with(ChronoField.AMPM_OF_DAY, halfday);
        }
    } else {
        error(newValidationError(new ConversionException("Cannot parse time").setTargetType(getType())));
        return;
    }
    setConvertedInput(localTime);
}
Also used : ConversionException(org.apache.wicket.util.convert.ConversionException) LocalTime(java.time.LocalTime)

Example 3 with ConversionException

use of org.apache.wicket.util.convert.ConversionException in project wicket by apache.

the class AbstractDateTimeField method convertInput.

/**
 * Sets the converted input, which is an instance of {@link Date}, possibly null. It combines
 * the inputs of the nested date, hours, minutes and am/pm fields and constructs a date from it.
 * <p>
 * Note that overriding this method is a better option than overriding {@link #updateModel()}
 * like the first versions of this class did. The reason for that is that this method can be
 * used by form validators without having to depend on the actual model being updated, and this
 * method is called by the default implementation of {@link #updateModel()} anyway (so we don't
 * have to override that anymore).
 */
@Override
public void convertInput() {
    // Get the converted input values
    LocalDate date = localDateField.getConvertedInput();
    LocalTime time = timeField.getConvertedInput();
    T temporal;
    if (date == null && time == null) {
        temporal = null;
    } else {
        if (date == null) {
            error(newValidationError(new ConversionException("Cannot create temporal without date").setTargetType(getType())));
            return;
        }
        if (time == null) {
            time = getDefaultTime();
            if (time == null) {
                error(newValidationError(new ConversionException("Cannot create temporal without time").setTargetType(getType())));
                return;
            }
        }
        // Use the input to create proper date-time
        temporal = createTemporal(date, time);
    }
    setConvertedInput(temporal);
}
Also used : ConversionException(org.apache.wicket.util.convert.ConversionException) LocalTime(java.time.LocalTime) LocalDate(java.time.LocalDate)

Aggregations

ConversionException (org.apache.wicket.util.convert.ConversionException)3 LocalTime (java.time.LocalTime)2 LocalDate (java.time.LocalDate)1 PropertyResolverConverter (org.apache.wicket.core.util.lang.PropertyResolverConverter)1 Test (org.junit.Test)1