1 15 package org.apache.tapestry.valid; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.apache.tapestry.IMarkupWriter; 21 import org.apache.tapestry.IRequestCycle; 22 import org.apache.tapestry.form.IFormComponent; 23 24 31 32 public class StringValidator extends BaseValidator 33 { 34 private int _minimumLength; 35 36 private String _minimumLengthMessage; 37 38 39 40 private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script"; 41 42 public StringValidator() 43 { 44 } 45 46 51 52 public StringValidator(String initializer) 53 { 54 super(initializer); 55 } 56 57 public String toString(IFormComponent field, Object value) 58 { 59 if (value == null) 60 return null; 61 62 return value.toString(); 63 } 64 65 public Object toObject(IFormComponent field, String input) throws ValidatorException 66 { 67 if (checkRequired(field, input)) 68 return null; 69 70 if (_minimumLength > 0 && input.length() < _minimumLength) 71 throw new ValidatorException(buildMinimumLengthMessage(field), 72 ValidationConstraint.MINIMUM_WIDTH); 73 74 return input; 75 } 76 77 public int getMinimumLength() 78 { 79 return _minimumLength; 80 } 81 82 public void setMinimumLength(int minimumLength) 83 { 84 _minimumLength = minimumLength; 85 } 86 87 90 91 public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, 92 IRequestCycle cycle) 93 { 94 if (!isClientScriptingEnabled()) 95 return; 96 97 if (!(isRequired() || _minimumLength > 0)) 98 return; 99 100 Map symbols = new HashMap (); 101 102 if (isRequired()) 103 symbols.put("requiredMessage", buildRequiredMessage(field)); 104 105 if (_minimumLength > 0) 106 symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field)); 107 108 processValidatorScript(_scriptPath, cycle, field, symbols); 109 } 110 111 114 115 public String getScriptPath() 116 { 117 return _scriptPath; 118 } 119 120 128 129 public void setScriptPath(String scriptPath) 130 { 131 _scriptPath = scriptPath; 132 } 133 134 135 public String getMinimumLengthMessage() 136 { 137 return _minimumLengthMessage; 138 } 139 140 146 147 public void setMinimumLengthMessage(String string) 148 { 149 _minimumLengthMessage = string; 150 } 151 152 153 154 protected String buildMinimumLengthMessage(IFormComponent field) 155 { 156 String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage() 157 .getLocale()); 158 159 return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName()); 160 } 161 162 } | Popular Tags |