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
006package fc.web.forms;
007
008import javax.servlet.*;
009import javax.servlet.http.*;
010import java.io.*;
011import java.util.*;
012import java.util.regex.*;
013
014import fc.jdbc.*;
015import fc.io.*;
016import fc.util.*;
017
018/**
019Validates 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
025This 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
028check to see if the select value was <b>not</b> <i><tt>---choose an
029option---</tt></i>.
030
031@author hursh jain
032**/
033public final class VSelectValue extends FieldValidator
034{
035String 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**/
043public VSelectValue(
044Select field, String errorMessage, String badSelectValue) 
045  {
046  super(field, errorMessage);
047  this.errorval = badSelectValue;
048  }
049
050/** 
051Works 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**/
057public 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