001    // Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002    // The Molly framework is freely distributable under the terms of an
003    // MIT-style license. For details, see the molly pages web site at:
004    // http://www.mollypages.org/. Use, modify, have fun !
005    
006    package fc.web.forms;
007    
008    import javax.servlet.*;
009    import javax.servlet.http.*;
010    import java.io.*;
011    import java.util.*;
012    import java.util.regex.*;
013    
014    import fc.jdbc.*;
015    import fc.io.*;
016    import fc.util.*;
017    
018    /**
019    Validates that a select field has:
020    <ol>
021    <li>some value(s)
022    <li>the values(s) are <b>not</b> a pre-specified value. 
023    </ol>
024    
025    This is useful to see if the select field (popup) was selected by the user
026    (typically, select fields may be displayed with a dummy default
027    <i><tt>---choose an option---</tt></i> type selection. In that case, we would
028    check to see if the select value was <b>not</b> <i><tt>---choose an
029    option---</tt></i>.
030    
031    @author hursh jain
032    **/
033    public final class VSelectValue extends FieldValidator
034    {
035    String errorval;
036    
037    /** 
038    @param  field       the parent field
039    @param  badSelectValue    the select value which will cause the
040                  validation to fail
041    @param  errorMessage    error message for unsuccessful validation error.              
042    **/
043    public VSelectValue(
044    Select field, String errorMessage, String badSelectValue) 
045      {
046      super(field, errorMessage);
047      this.errorval = badSelectValue;
048      }
049    
050    /** 
051    Works with the {@link Select} field. 
052    
053    @throws ClassCastException  If the field's {@link Field#getValue} method
054                  does not return a collection of 
055                  {@link Select.Option} objects
056    **/
057    public boolean validate(FormData fd, HttpServletRequest req) 
058      {
059      Collection list = ((Select)field).getValue(fd);
060      
061      if (list == null || list.size() == 0)
062        return false;
063        
064      Iterator it = list.iterator();
065      while (it.hasNext()) {
066        Select.Option item = (Select.Option) it.next();     
067        if (item.getValue().equals(errorval))
068          return false;
069        } 
070    
071      return true;
072      } 
073    }          //~class VSelectValue
074