View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.x.form.validators;
14  
15  import com.eviware.soapui.support.StringUtils;
16  import com.eviware.x.form.ValidationMessage;
17  import com.eviware.x.form.XFormField;
18  import com.eviware.x.form.XFormFieldValidator;
19  import com.eviware.x.form.XFormOptionsField;
20  
21  public class RequiredValidator implements XFormFieldValidator
22  {
23  	private boolean trim;
24  	private String message;
25  
26  	public RequiredValidator()
27  	{
28  		this.message = "Field requires a value";
29  	}
30  
31  	public RequiredValidator( String message )
32  	{
33  		this.message = message;
34  	}
35  
36  	public ValidationMessage[] validateField( XFormField formField )
37  	{
38  		String value = null;
39  
40  		if( formField instanceof XFormOptionsField )
41  		{
42  			value = ((XFormOptionsField)formField).getSelectedIndexes().length == 0 ? null : "check";
43  		}
44  		else
45  		{
46  			value = formField.getValue();
47  		}
48  		
49  		if( !StringUtils.hasContent( value ))
50  		{
51  			return new ValidationMessage[] { new ValidationMessage( message, formField ) };
52  		}
53  
54  		return null;
55  	}
56  
57  	public boolean isTrim()
58  	{
59  		return trim;
60  	}
61  
62  	public void setTrim( boolean trim )
63  	{
64  		this.trim = trim;
65  	}
66  }