View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.x.form.ValidationMessage;
16  import com.eviware.x.form.XFormField;
17  import com.eviware.x.form.XFormFieldValidator;
18  
19  public class RequiredValidator implements XFormFieldValidator
20  {
21  	private boolean trim;
22  	private String message;
23  
24  	public RequiredValidator()
25  	{
26  		this.message = "Field requires a value";
27  	}
28  
29  	public RequiredValidator( String message )
30  	{
31  		this.message = message;
32  	}
33  
34  	public ValidationMessage[] validateField( XFormField formField )
35  	{
36  		String value = formField.getValue();
37  		if( value == null || value.length() == 0 || ( trim && value.trim().length() == 0 ) )
38  		{
39  			return new ValidationMessage[] { new ValidationMessage( message, formField ) };
40  		}
41  
42  		return null;
43  	}
44  
45  	public boolean isTrim()
46  	{
47  		return trim;
48  	}
49  
50  	public void setTrim( boolean trim )
51  	{
52  		this.trim = trim;
53  	}
54  }