4 Replies Latest reply on Jun 6, 2007 8:40 AM by wsollers

    DRY Edit pages

    awhitford

      Consider the following seam-gen generated snippet from an Edit.xhtml:

       <h:inputText id="code"
       disabled="#{countryHome.managed}"
       required="true"
       size="2"
       maxlength="2"
       value="#{countryHome.instance.code}">
       <a:support event="onblur" reRender="codeDecoration"/>
       </h:inputText>
      


      I like how seam will impose my Entity bean restrictions at the UI level, so that my @NotNull constraint is translated into a required="true", and my @Length(max=2) constraint is translated into a maxlength="2". But I'm concerned that this is repeating oneself -- especially when a change is made. If a constraint is changed on the @Entity bean, I need to update the Edit page in addition to the database... Why can't the Edit page be driven by the meta-data of the Entity bean at run-time? I would like the framework to interrogate the Entity bean's meta data for required and maxlength if they are not specified.

      (If this is a conscious decision based on the poor performance of reflection, then I understand. Too bad the xhtml couldn't be enriched from meta data at build time.)


        • 1. Re: DRY Edit pages
          gavin.king

          Actually its a limitation of JSF. You can't write a Validator that validates null.

          • 2. Re: DRY Edit pages
            stephen.friedrich

            Huh?
            Either I am not understanding the anwser or Gavin, you did not understand the question.
            What if the maximum length changes to 5? Currently you have to update both the entity and the xhtml.

            Is there any possibility to avoid this? A JSF component or validator that queries the bean's metadata?

            • 3. Re: DRY Edit pages
              gavin.king

              Well, size and maxlength are nothing to do with validation.

              Well, I guess maxlength kinda is. I suppose you could argue that its a bad idea that we include the maxlength in there. Take it out if you like...

              • 4. Re: DRY Edit pages
                wsollers

                The problem of changing db / entity metadata requiring a second change in the ui is not new and has been around for ever and predates java.

                However to make life easier just make a utility f(x) that will grab the entity and check for the field and see if it has a Hibernate annotation or your own custom annotation and get the appropriate metadata (length / precision etc ) you need and then use it.

                Maxlength and size both support EL. So you can do a size="#{fieldMetadata.sizeCalc('foo.class', 'field'}" instead of a " size="2".

                You can use custom facelets to do the same thing.