You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by "Geoff Callender (JIRA)" <ji...@apache.org> on 2013/12/08 14:45:35 UTC

[jira] [Updated] (TAP5-2255) Form and BeanEditForm differ in JSR-303 detection

     [ https://issues.apache.org/jira/browse/TAP5-2255?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Geoff Callender updated TAP5-2255:
----------------------------------

    Description: 
I have an entity field that the getter and setter convert between types (field is Date, getter and setter convert from/to JodaTime's DateMidnight). 

Form detects @NotNull on the field, but BeanEditForm doesn't. BeanEditForm detects @NotNull on the getter, but Form doesn't. So I have to provide @NotNull on the field AND the getter. Shouldn't Form and BeanEditForm behave the same?

For example, a snippet from an entity:

@Entity
public class DatesExample implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(nullable = false)
	private Long id;

	// This JSR-303 validation will be picked up by Form.
	@NotNull
	private java.sql.Date aDateMidnight;

	public Long getId() {
		return id;
	}

	// This JSR-303 validation will be picked up by BeanEditForm.
	@NotNull
	public DateMidnight getADateMidnight() {
		return JodaTimeUtil.toDateMidnight(aDateMidnight);
	}

	public void setADateMidnight(DateMidnight dm) {
		this.aDateMidnight = JodaTimeUtil.toSQLDate(dm);
	}

}

I've contributed type coercers in AppModule:

    public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {

       // From java.util.Date to DateMidnight

        Coercion<java.util.Date, DateMidnight> toDateMidnight = new Coercion<java.util.Date, DateMidnight>() {
            public DateMidnight coerce(java.util.Date input) {
                // TODO - confirm this conversion always works, esp. across timezones
                return JodaTimeUtil.toDateMidnight(input);
            }
        };

        configuration.add(new CoercionTuple<>(java.util.Date.class, DateMidnight.class, toDateMidnight));

        // From DateMidnight to java.util.Date

        Coercion<DateMidnight, java.util.Date> fromDateMidnight = new Coercion<DateMidnight, java.util.Date>() {
            public java.util.Date coerce(DateMidnight input) {
                // TODO - confirm this conversion always works, esp. across timezones
                return JodaTimeUtil.toJavaDate(input);
            }
        };

        configuration.add(new CoercionTuple<>(DateMidnight.class, java.util.Date.class, fromDateMidnight));
    }

and I've contributed an editor:

    public static void contributeBeanBlockSource(Configuration<BeanBlockContribution> configuration) {
        configuration.add(new EditBlockContribution("dateMidnight", "infra/AppPropertyEditBlocks", "dateMidnight"));
    }

Here is the editor:

<t:container xml:space="default" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">

    <t:block id="dateMidnight">
        <t:label for="dateMidnight"/>
        <input t:id="dateMidnight" t:type="DateField" value="context.propertyValue" label="prop:context.label" 
            format="prop:dateInputFormat" translate="prop:dateMidnightTranslator" validate="prop:dateMidnightValidator" 
            clientId="prop:context.propertyId" annotationProvider="context"/>
    </t:block>

</t:container>

public class AppPropertyEditBlocks {

    @Property
    @Environmental
    private PropertyEditContext context;

    @Component
    private DateField dateMidnight;

    @Component
    private DateField localDate;
    
    public DateFormat getDateInputFormat() {
        return new SimpleDateFormat("dd MMMM yyyy");
    }
    
    public FieldTranslator<?> getDateMidnightTranslator() {
        return context.getTranslator(dateMidnight);
    }
    
    public FieldValidator<?> getDateMidnightValidator() {
        return context.getValidator(dateMidnight);
    }

}


  was:
I have an entity field that the getter and setter convert between types (field is Date, getter and setter convert from/to JodaTime's DateMidnight). 

Form detects @NotNull on the field, but BeanEditForm doesn't. BeanEditForm detects @NotNull on the getter, but Form doesn't. So I have to provide @NotNull on the field AND the getter. Shouldn't Form and BeanEditForm behave the same?

For example, a snippet from an entity:

@Entity
public class DatesExample implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(nullable = false)
	private Long id;

	// This JSR-303 validation will be picked up by Form.
	@NotNull
	private java.sql.Date aDateMidnight;

	public Long getId() {
		return id;
	}

	// This JSR-303 validation will be picked up by BeanEditForm.
	@NotNull
	public DateMidnight getADateMidnight() {
		return JodaTimeUtil.toDateMidnight(aDateMidnight);
	}

	public void setADateMidnight(DateMidnight dm) {
		this.aDateMidnight = JodaTimeUtil.toSQLDate(dm);
	}

}

I've contributed type coercers in AppModule:

    public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {

       // From java.util.Date to DateMidnight

        Coercion<java.util.Date, DateMidnight> toDateMidnight = new Coercion<java.util.Date, DateMidnight>() {
            public DateMidnight coerce(java.util.Date input) {
                // TODO - confirm this conversion always works, esp. across timezones
                return JodaTimeUtil.toDateMidnight(input);
            }
        };

        configuration.add(new CoercionTuple<>(java.util.Date.class, DateMidnight.class, toDateMidnight));

        // From DateMidnight to java.util.Date

        Coercion<DateMidnight, java.util.Date> fromDateMidnight = new Coercion<DateMidnight, java.util.Date>() {
            public java.util.Date coerce(DateMidnight input) {
                // TODO - confirm this conversion always works, esp. across timezones
                return JodaTimeUtil.toJavaDate(input);
            }
        };
    }

and I've contributed an editor:

    public static void contributeBeanBlockSource(Configuration<BeanBlockContribution> configuration) {
        configuration.add(new EditBlockContribution("dateMidnight", "infra/AppPropertyEditBlocks", "dateMidnight"));
    }

Here is the editor:

<t:container xml:space="default" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">

    <t:block id="dateMidnight">
        <t:label for="dateMidnight"/>
        <input t:id="dateMidnight" t:type="DateField" value="context.propertyValue" label="prop:context.label" 
            format="prop:dateInputFormat" translate="prop:dateMidnightTranslator" validate="prop:dateMidnightValidator" 
            clientId="prop:context.propertyId" annotationProvider="context"/>
    </t:block>

</t:container>

public class AppPropertyEditBlocks {

    @Property
    @Environmental
    private PropertyEditContext context;

    @Component
    private DateField dateMidnight;

    @Component
    private DateField localDate;
    
    public DateFormat getDateInputFormat() {
        return new SimpleDateFormat("dd MMMM yyyy");
    }
    
    public FieldTranslator<?> getDateMidnightTranslator() {
        return context.getTranslator(dateMidnight);
    }
    
    public FieldValidator<?> getDateMidnightValidator() {
        return context.getValidator(dateMidnight);
    }

}



> Form and BeanEditForm differ in JSR-303 detection
> -------------------------------------------------
>
>                 Key: TAP5-2255
>                 URL: https://issues.apache.org/jira/browse/TAP5-2255
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-beanvalidator, tapestry-core
>    Affects Versions: 5.4
>            Reporter: Geoff Callender
>
> I have an entity field that the getter and setter convert between types (field is Date, getter and setter convert from/to JodaTime's DateMidnight). 
> Form detects @NotNull on the field, but BeanEditForm doesn't. BeanEditForm detects @NotNull on the getter, but Form doesn't. So I have to provide @NotNull on the field AND the getter. Shouldn't Form and BeanEditForm behave the same?
> For example, a snippet from an entity:
> @Entity
> public class DatesExample implements Serializable {
> 	@Id
> 	@GeneratedValue(strategy = GenerationType.AUTO)
> 	@Column(nullable = false)
> 	private Long id;
> 	// This JSR-303 validation will be picked up by Form.
> 	@NotNull
> 	private java.sql.Date aDateMidnight;
> 	public Long getId() {
> 		return id;
> 	}
> 	// This JSR-303 validation will be picked up by BeanEditForm.
> 	@NotNull
> 	public DateMidnight getADateMidnight() {
> 		return JodaTimeUtil.toDateMidnight(aDateMidnight);
> 	}
> 	public void setADateMidnight(DateMidnight dm) {
> 		this.aDateMidnight = JodaTimeUtil.toSQLDate(dm);
> 	}
> }
> I've contributed type coercers in AppModule:
>     public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {
>        // From java.util.Date to DateMidnight
>         Coercion<java.util.Date, DateMidnight> toDateMidnight = new Coercion<java.util.Date, DateMidnight>() {
>             public DateMidnight coerce(java.util.Date input) {
>                 // TODO - confirm this conversion always works, esp. across timezones
>                 return JodaTimeUtil.toDateMidnight(input);
>             }
>         };
>         configuration.add(new CoercionTuple<>(java.util.Date.class, DateMidnight.class, toDateMidnight));
>         // From DateMidnight to java.util.Date
>         Coercion<DateMidnight, java.util.Date> fromDateMidnight = new Coercion<DateMidnight, java.util.Date>() {
>             public java.util.Date coerce(DateMidnight input) {
>                 // TODO - confirm this conversion always works, esp. across timezones
>                 return JodaTimeUtil.toJavaDate(input);
>             }
>         };
>         configuration.add(new CoercionTuple<>(DateMidnight.class, java.util.Date.class, fromDateMidnight));
>     }
> and I've contributed an editor:
>     public static void contributeBeanBlockSource(Configuration<BeanBlockContribution> configuration) {
>         configuration.add(new EditBlockContribution("dateMidnight", "infra/AppPropertyEditBlocks", "dateMidnight"));
>     }
> Here is the editor:
> <t:container xml:space="default" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">
>     <t:block id="dateMidnight">
>         <t:label for="dateMidnight"/>
>         <input t:id="dateMidnight" t:type="DateField" value="context.propertyValue" label="prop:context.label" 
>             format="prop:dateInputFormat" translate="prop:dateMidnightTranslator" validate="prop:dateMidnightValidator" 
>             clientId="prop:context.propertyId" annotationProvider="context"/>
>     </t:block>
> </t:container>
> public class AppPropertyEditBlocks {
>     @Property
>     @Environmental
>     private PropertyEditContext context;
>     @Component
>     private DateField dateMidnight;
>     @Component
>     private DateField localDate;
>     
>     public DateFormat getDateInputFormat() {
>         return new SimpleDateFormat("dd MMMM yyyy");
>     }
>     
>     public FieldTranslator<?> getDateMidnightTranslator() {
>         return context.getTranslator(dateMidnight);
>     }
>     
>     public FieldValidator<?> getDateMidnightValidator() {
>         return context.getValidator(dateMidnight);
>     }
> }



--
This message was sent by Atlassian JIRA
(v6.1#6144)