View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 ||
38  		   (trim && value.trim().length() == 0 ))
39  		{
40  			return new ValidationMessage[] { new ValidationMessage( message, formField )}; 
41  		}
42  		
43  		return null;
44  	}
45  
46  	public boolean isTrim()
47  	{
48  		return trim;
49  	}
50  
51  	public void setTrim(boolean trim)
52  	{
53  		this.trim = trim;
54  	}
55  }