You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by ch...@globus.ch on 2004/12/03 16:33:38 UTC

Using a Value-ChangeListener for large Validation

Hi there!

I wanted to ask you guys for your opinion on what I'm doing....

I have some validation that I have to do in my application. This 
Validation is more or less business related
and needs database quering:

1. So I decided not to make a custum Validator because this would lower 
the performance...

2. A possibilty would be to make in my business logic when the user hits 
"save" 

3. I thought to make a validation in the case the user has changed the 
field, which happens only once or twice while working.

So I added a valuechangelistener:

<h:inputText id="senderBranch" 
value="#{WVSForm.a_WVSObject.a_SenderBranch}" required="true" 
valueChangeListener="#{WVSForm.validateBranch}" >

And a method in my backing bean:

public void validateBranch(ValueChangeEvent p_ValueChangeEvent) throws 
AbortProcessingException
{
        Integer t_branchString = 
(Integer)p_ValueChangeEvent.getNewValue();

        try
        {
                t_branch = 
MdaBranchService.current().getThisBranch(t_company,t_branchString.toString());
        }
        catch (Exception e)
        {
                e.printStackTrace();  //To change body of catch statement 
use Options | File Templates.
        }

        if(t_branch == null)
        {
                // setting error message
                new MessageFormat()
                FacesContext.getCurrentInstance().addMessage(
                null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, null,
                         WVSMsgBeanGui.getMyBundle().getString(
                        WVSMsgBeanGui.WVS_FINISH_FAILED_BRANCHES) + " (" + 
t_branchString.toString() + ")"));

                // throw the abort exception
                throw new AbortProcessingException();
        }
}

This cancels the real ActionEvent like save and returns to the last jsp 
with the new error message.

I read that it isn't really a good thing to use the 
AbortProcessingException... But I wanted to here your opinion about that.

thanks,

 Chris




Re: boolean custom converter

Posted by Francesco Consumi <co...@istitutodeglinnocenti.it>.
Scrive Heath Borders <he...@gmail.com>:

> The problem is that UISelectBoolean is dependent on its value always
> being a boolean.  The converter is only for use between JSF and HTML,
> not within JSF.  Again, you'll either have to put conversion on your
> bean, or make a custom component that doesn't depend on a boolean
> value.
>
> Don't blame MyFaces, this is in the API
>
Ok, I understand. I think I'll start writing a new UISelectIntBoolean 
component.

thanks.

--
Francesco Consumi



Re: boolean custom converter

Posted by Heath Borders <he...@gmail.com>.
The problem is that UISelectBoolean is dependent on its value always
being a boolean.  The converter is only for use between JSF and HTML,
not within JSF.  Again, you'll either have to put conversion on your
bean, or make a custom component that doesn't depend on a boolean
value.

Don't blame MyFaces, this is in the API


On Tue,  7 Dec 2004 17:30:49 +0100, Francesco Consumi
<co...@istitutodeglinnocenti.it> wrote:
> Scrive Heath Borders <he...@gmail.com>:
> 
> > I think UISelectBoolean can only use boolean types.
> 
> I know. so, I want to write a converter for giving it a boolean
> starting from an
> integer..... :-)
> 
> 
> > You might have to
> > do the conversion on your bean.  That is, have boolean getters and
> > setters that call int getters and setters.
> >
> I can't. these values are in a ArrayList of maps automatically
> generated from a
> SQL query.
> 
> --
> 
> 
> Francesco Consumi
> Ufficio Sistemi informativi
> Istituto degli Innocenti
> Piazza SS.Annunziata, 12
> 50122 Firenze
> consumi at istitutodeglinnocenti.it
> Tel. +39 055 2037320
> ICQ# 12516133
> 
> 


-- 
If you don't have a GMail account, I probably have 5 invites.  Just ask!
-Heath Borders-Wing
hborders@mail.win.org

Re: boolean custom converter

Posted by Francesco Consumi <co...@istitutodeglinnocenti.it>.
Scrive Heath Borders <he...@gmail.com>:

> I think UISelectBoolean can only use boolean types.

I know. so, I want to write a converter for giving it a boolean 
starting from an
integer..... :-)


> You might have to
> do the conversion on your bean.  That is, have boolean getters and
> setters that call int getters and setters.
>
I can't. these values are in a ArrayList of maps automatically 
generated from a
SQL query.




--
Francesco Consumi
Ufficio Sistemi informativi
Istituto degli Innocenti
Piazza SS.Annunziata, 12
50122 Firenze
consumi at istitutodeglinnocenti.it
Tel. +39 055 2037320
ICQ# 12516133


Re: boolean custom converter

Posted by Heath Borders <he...@gmail.com>.
I think UISelectBoolean can only use boolean types.  You might have to
do the conversion on your bean.  That is, have boolean getters and
setters that call int getters and setters.


On Tue,  7 Dec 2004 11:54:37 +0100, Francesco Consumi
<co...@istitutodeglinnocenti.it> wrote:
> Hi all,
> 
> I'm creating a custom converter in order to use smallint database fields with
> boolean components, such as SelectBooleanCheckBox. I'm doing this because
> Firebird SQL doesn't have a real boolean type.
> 
> I've written a very simple converter:
> 
> package frautils.Converters;
> 
> import javax.faces.context.FacesContext;
> import javax.faces.convert.Converter;
> import javax.faces.component.UIComponent;
> import javax.faces.convert.ConverterException;
> 
> public class intBoolConverter implements Converter {
>   public static final String CONVERTER_ID ="intBoolConverter";
> 
>   public Object getAsObject(FacesContext context, UIComponent component,
>                             String newValue) throws ConverterException {
>       return (newValue.equals("1");
> 
>   }
>   public String getAsString(FacesContext context, UIComponent component,
>                             Object value) throws ConverterException {
>     String s = "0";
>     if (value.toString().equals("true")) { s = "1"; };
>     return s;
>   }
> 
> ...and I've registered this in faces-config.xml:
> 
>   <converter>
>     <converter-id>intBoolConverter</converter-id>
>     <converter-class>frautils.Converters.intBoolConverter</converter-class>
>   </converter>
> 
> ..and I'm using that in this way:
> 
>   <h:selectBooleanCheckbox value="#{lista.FRUITO}">
>      <f:converter converterId="intBoolConverter"/>
>   </h:selectBooleanCheckbox>
> 
> ("FRUITO" is a smallint field )
> the problem is that the converter class is never being called, and the
> application returns this error:
> 
> java.lang.ClassCastException: java.lang.Integer
>     at
> javax.faces.component.UISelectBoolean.isSelected(UISelectBoolean.java:38)
>     at
> net.sourceforge.myfaces.renderkit.html.HtmlCheckboxRendererBase.encodeEnd(HtmlCheckboxRendererBase.java:84)
> ...etc.......
> 
> where am I wrong ?
> 
> thanks,
> 
> --
> Francesco Consumi
> Ufficio Sistemi informativi
> Istituto degli Innocenti
> Piazza SS.Annunziata, 12
> 50122 Firenze
> consumi at istitutodeglinnocenti.it
> Tel. +39 055 2037320
> ICQ# 12516133
> 
> 


-- 
If you don't have a GMail account, I probably have 5 invites.  Just ask!
-Heath Borders-Wing
hborders@mail.win.org

boolean custom converter

Posted by Francesco Consumi <co...@istitutodeglinnocenti.it>.
Hi all,

I'm creating a custom converter in order to use smallint database fields with
boolean components, such as SelectBooleanCheckBox. I'm doing this because
Firebird SQL doesn't have a real boolean type.

I've written a very simple converter:

package frautils.Converters;

import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.component.UIComponent;
import javax.faces.convert.ConverterException;

public class intBoolConverter implements Converter {
   public static final String CONVERTER_ID ="intBoolConverter";

   public Object getAsObject(FacesContext context, UIComponent component,
                             String newValue) throws ConverterException {
       return (newValue.equals("1");

   }
   public String getAsString(FacesContext context, UIComponent component,
                             Object value) throws ConverterException {
     String s = "0";
     if (value.toString().equals("true")) { s = "1"; };
     return s;
   }

...and I've registered this in faces-config.xml:


   <converter>
     <converter-id>intBoolConverter</converter-id>
     <converter-class>frautils.Converters.intBoolConverter</converter-class>
   </converter>

..and I'm using that in this way:

   <h:selectBooleanCheckbox value="#{lista.FRUITO}">
      <f:converter converterId="intBoolConverter"/>
   </h:selectBooleanCheckbox>

("FRUITO" is a smallint field )
the problem is that the converter class is never being called, and the
application returns this error:

java.lang.ClassCastException: java.lang.Integer
     at 
javax.faces.component.UISelectBoolean.isSelected(UISelectBoolean.java:38)
     at
net.sourceforge.myfaces.renderkit.html.HtmlCheckboxRendererBase.encodeEnd(HtmlCheckboxRendererBase.java:84)
...etc.......


where am I wrong ?

thanks,



--
Francesco Consumi
Ufficio Sistemi informativi
Istituto degli Innocenti
Piazza SS.Annunziata, 12
50122 Firenze
consumi at istitutodeglinnocenti.it
Tel. +39 055 2037320
ICQ# 12516133


RE: Using a Value-ChangeListener for large Validation

Posted by Arinaya <ar...@realidea.ca>.
Hi Christian,
 
I do something similar, but use a custom validator method instead of a
valueChangeListener, and it works great. It looks like what you have right
now is kind of a mix between the two approaches, which is maybe why it
doesn't work.
 
To convert to a custom validator method, you would just change
valueChangeListener="..." to validator="..." in your JSP, and in your
backing bean make sure the validate method takes in (FacesContext context,
UIComponent component, Object value), and throws ValidatorException instead
of AbortProcessingException.
 
HTH,
Arinaya
 


  _____  

From: christian.rueedi@globus.ch [mailto:christian.rueedi@globus.ch] 
Sent: Friday, December 03, 2004 8:34 AM
To: MyFaces Discussion
Subject: Using a Value-ChangeListener for large Validation



Hi there! 

I wanted to ask you guys for your opinion on what I'm doing.... 

I have some validation that I have to do in my application. This Validation
is more or less business related 
and needs database quering: 

1. So I decided not to make a custum Validator because this would lower the
performance... 

2. A possibilty would be to make in my business logic when the user hits
"save" 

3. I thought to make a validation in the case the user has changed the
field, which happens only once or twice while working. 

So I added a valuechangelistener: 

<h:inputText id="senderBranch" value="#{WVSForm.a_WVSObject.a_SenderBranch}"
required="true" valueChangeListener="#{WVSForm.validateBranch}" > 

And a method in my backing bean: 

public void validateBranch(ValueChangeEvent p_ValueChangeEvent) throws
AbortProcessingException 
{ 
        Integer t_branchString = (Integer)p_ValueChangeEvent.getNewValue(); 

        try 
        { 
                t_branch =
MdaBranchService.current().getThisBranch(t_company,t_branchString.toString()
); 
        } 
        catch (Exception e) 
        { 
                e.printStackTrace();  //To change body of catch statement
use Options | File Templates. 
        } 

        if(t_branch == null) 
        { 
                // setting error message 
                new MessageFormat() 
                FacesContext.getCurrentInstance().addMessage( 
                null, 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, null, 
                         WVSMsgBeanGui.getMyBundle().getString( 
                        WVSMsgBeanGui.WVS_FINISH_FAILED_BRANCHES) + " (" +
t_branchString.toString() + ")")); 

                // throw the abort exception 
                throw new AbortProcessingException(); 
        } 
} 

This cancels the real ActionEvent like save and returns to the last jsp with
the new error message. 

I read that it isn't really a good thing to use the
AbortProcessingException... But I wanted to here your opinion about that. 

thanks, 

 Chris