You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Christoph Jaeger <ch...@derwald.at> on 2007/10/28 10:33:13 UTC

T5: BeanEditForm and Property editor

I tried to create a property editor for use in a BeanEditForm. I followed
the documentation in "BeanEditForm guide" in the Tapestry core documentation.
I almost got it right, but some questions popped up.

What I tried to do: to find out how property editor is working, I created a
new property editor for nullable numbers, Double for now (this popped up on
the mailing list already, and I have a need for it anyhow). I want to have
a simple field where I can input a number, and if the user leaves the field
empty (or enters only whitespace), the result is not 0.0 but null.

So, I added this to my configuration:

-- snip
public static void contributeDefaultDataTypeAnalyzer(MappedConfiguration<Class, String> configuration)
{
  configuration.add(Double.class, "nullableNumber");
}

public static void contributeBeanBlockSource(Configuration<BeanBlockContribution> configuration)
{
  configuration.add(new BeanBlockContribution("nullableNumber", "NullableNumberEditBlocks", "nullableNumber", true));
}
-- snip

I put this in a template file NullableNumberEditBlocks.tml in my ...pages package:

-- snip
<span xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<t:block t:id="nullableNumber">
  <t:label for="nullableNumber"/>
  <t:textfield t:id="nullableNumber" size="10"/>
</t:block>
</span>
-- snip

And I created a NullableNumberEditBlocks class, see end of the mail.

The first problem I had was: I tried

<t:block t:id="nullableNumber" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
  <t:label for="nullableNumber"/>
  <t:textfield t:id="nullableNumber" size="10"/>
</t:block>

in my .tml file first, but always received an exception with an error message like:

Element <block> does not support an attribute named ''. The only allowed attribute name is 'id'.

Did I do something wrong here, or is there an error in the parsing of the template files?

The second thing is: I don't understand why my NullableNumberEditBlocks have to be in the pages
package. For me it sounds more reasonable to have this where all the other components live
(tried to put it there first, but it didn't work).

Third thing: the translator I implemented returns null in case of an empty input field,
but in the end something converts this back to 0.0 (the property used is defined as Double,
also the getter and setter methods are defined using Double).

Please let me know where I did something wrong, or which things need to go to Jira. If there
is interest, I can add a page to the Tapestry5HowTos also.

Best Regards,

Christoph


NullableNumberEditBlocks.java
-- snip
import org.apache.tapestry.FieldValidator;
import org.apache.tapestry.Translator;
import org.apache.tapestry.ValidationException;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Environmental;
import org.apache.tapestry.corelib.components.TextField;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.services.PropertyEditContext;

public class NullableNumberEditBlocks
{
    @Environmental
    private PropertyEditContext context;

    @Component(parameters = { "value=context.propertyValue", "label=prop:context.label",
            "translate=prop:numberTranslator", "validate=prop:numberValidator", "clientId=prop:context.propertyId" })
    private TextField nullableNumber;

    private Translator<Double> translator;

    public NullableNumberEditBlocks()
    {
        translator = new Translator<Double>()
        {
            public Double parseClient(String clientValue, Messages messages) throws ValidationException
            {
                if (clientValue == null) return null;
                clientValue = clientValue.trim();
                // convert an empty string to null
                if (clientValue.length() == 0) return null;

                try
                {
                    return Double.valueOf(clientValue);
                }
                catch (NumberFormatException e)
                {
                    throw new ValidationException("Can not convert '" + clientValue + "' to double.");
                }
            }

            public String toClient(Double value)
            {
                if (value == null) return "";
                return String.format("%.1f", value);
            }
        };
    }

    public PropertyEditContext getContext()
    {
        return context;
    }

    public FieldValidator getNumberValidator()
    {
        return context.getValidator(nullableNumber);
    }

    public Translator<Double> getNumberTranslator()
    {
        return translator;
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org