Hello Robert,
Very cool!
Thanks for posting your code for everyone. It is complete and looks well
done. I like the fact that you included an example of how to use it too.
John
On Thu, Dec 4, 2008 at 6:11 PM, <robert.hook_at_salmat.com.au> wrote:
> Thanks for the input on this folks, I stepped back and had a think about it
> and came up with a solution that works for me based on
> extending the validator, which on reflection is what I want to be doing
> anyway.
>
> The tutorial at <http://www.netbeans.org/kb/docs/web/convert-validate.html>
> is very good (and better than the 6.1 version that
> various FAQs point at), and led me down the right path. The difficulty with
> overriding the default messages in a resource bundle is
> that it still leaves all instances of that message the same (or at least
> having the same format), whereas I wanted a little more
> flexibility for my purposes.
>
> I've not tried the javascript approach that John suggested, but it's an
> intriguing possibility I will explore at some time.
>
> What I did first was build a simple extension of the validator:
>
> package au.com.hpa.webservice.faces;
>
> import javax.faces.application.FacesMessage;
> import javax.faces.component.UIComponent;
> import javax.faces.context.FacesContext;
> import javax.faces.validator.ValidatorException;
>
> /**
> * extends the base woodstock LongRangeValidator to provide a mechanism for
> customising the error messages.
> * @author Robert Hook
> */
> public class CustomLongRangeValidator extends
> javax.faces.validator.LongRangeValidator {
>
> String briefMessage = null;
> String fullMessage = null;
>
> /**
> * mutator - store a new short-form error message
> * @return
> */
> public String getBriefMessage() {
> return briefMessage;
> }
>
> /**
> * mutator - store a new short form error message
> * @param briefMessage
> */
> public void setBriefMessage(String briefMessage) {
> this.briefMessage = briefMessage;
> }
>
> /**
> *
> * @return
> */
> public String getFullMessage() {
> return fullMessage;
> }
>
> /**
> * mutator - store a new long form error message
> * @param fullMessage
> */
> public void setFullMessage(String fullMessage) {
> this.fullMessage = fullMessage;
> }
>
> /**
> * Peform the base validation, but throwing a custom message. A custom
> message
> * will only be thrown if at least either the brief or full message have
> been set.
> * @param context
> * @param component
> * @param value
> * @throws javax.faces.validator.ValidatorException
> */
> @Override
> public void validate(FacesContext context, UIComponent component, Object
> value) throws ValidatorException {
> try {
> super.validate(context, component, value);
> } catch (ValidatorException ex) {
> if (getBriefMessage() != null && getFullMessage() != null) {
> throw new ValidatorException(new
> FacesMessage(getBriefMessage(), getFullMessage()));
> } else if (getBriefMessage() != null) {
> throw new ValidatorException(new
> FacesMessage(getBriefMessage()));
> } else if (getFullMessage() != null) {
> throw new ValidatorException(new
> FacesMessage(getFullMessage()));
> } else {
> throw ex;
> }
> }
> }
> }
>
> Then I include an instance of that object in my backing bean, and in init()
> for the page populate it:
>
> setCustomLongRangeValidator(new CustomLongRangeValidator());
> CustomLongRangeValidator crv = getCustomLongRangeValidator();
>
> crv.setBriefMessage(Utilities.getResourceString("tooltip.datasources.port.brief",
> Constants.resourcePath));
>
> crv.setFullMessage(Utilities.getResourceString("tooltip.datasources.port",
> Constants.resourcePath));
> crv.setMinimum(1024);
> crv.setMaximum(8192);
>
>
> Finally in the JSF I refer to my customised validator:
>
> <webuijsf:textField autoValidate="true" columns="4" id="textField4"
> maxLength="8" notify="form1:alert1"
> required="true" style="left: 264px; top: 168px; position:
> absolute" text="#{SessionBean1.currentDataSource.port}"
> toolTip="#{resources['tooltip.datasources.port']}"
> validatorExpression="#{EditDatasource.customLongRangeValidator.validate}"/>
>
>
> --
>
> Robert Hook | Software Engineer for Salmat BusinessForce
> 16 Archimedes Pl
> Murarrie QLD 4172 Australia
>
> t +61 (07) 3896 0896
> f +61 (07) 3899 5674
> m +61 0407 959 570
> e robert.hook_at_salmat.com.au
> w salmat.com.au
>
>
> ***********************************************************************************
> This e-mail, including any attachments to it, may contain confidential
> and/or personal information. If you have received this e-mail in error, you
> must not copy, distribute, or disclose it, use or take any action based on
> the information contained within it. Please notify the sender immediately by
> return e-mail of the error and then delete the original e-mail.
>
> The information contained within this e-mail may be solely the opinion of
> the sender and may not necessarily reflect the position, beliefs or opinions
> of the organisation on any issue. This email has been swept for the presence
> of computer viruses known to the organisation's anti-virus systems.
>
>
> ***********************************************************************************
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_woodstock.dev.java.net
> For additional commands, e-mail: users-help_at_woodstock.dev.java.net
>
>
--
John Yeary
--
http://javaevangelist.blogspot.com
"Far better it is to dare might things, to win glorious triumphs, even
though checkered by failure,
than to take rank with those poor spirits who neither enjoy much nor suffer
much, because they live in the grey
twilight that knows not victory nor defeat." -- Theodore Roosevelt