1 24 package org.riotfamily.forms.element; 25 26 import java.io.PrintWriter ; 27 import java.util.regex.Pattern ; 28 29 import org.riotfamily.common.markup.DocumentWriter; 30 import org.riotfamily.common.markup.Html; 31 import org.riotfamily.forms.ErrorUtils; 32 import org.riotfamily.forms.MessageUtils; 33 import org.riotfamily.forms.request.FormRequest; 34 import org.springframework.util.ObjectUtils; 35 import org.springframework.util.StringUtils; 36 37 40 public class TextField extends AbstractTextElement { 41 42 private static final String CONFIRM_SUFFIX = "-confirm"; 43 44 private static final String DEFAULT_CONFIRM_MESSAGE_KEY = 45 "label.textField.confirmInput"; 46 47 private static final String DEFAULT_REGEX_MISMATCH_MESSAGE_KEY = 48 "error.textField.regexMismatch"; 49 50 private boolean confirm; 51 52 private String confirmText = null; 53 54 private String confirmMessageKey; 55 56 private String confirmMessageText; 57 58 private Pattern pattern; 59 60 private String regexMismatchMessageKey = DEFAULT_REGEX_MISMATCH_MESSAGE_KEY; 61 62 private String regexMismatchMessageText; 63 64 65 public TextField() { 66 this("text"); 67 } 68 69 public TextField(String s) { 70 super(s); 71 } 72 73 public void setConfirm(boolean confirm) { 74 this.confirm = confirm; 75 } 76 77 public void setConfirmMessageKey(String confirmMessageKey) { 78 this.confirmMessageKey = confirmMessageKey; 79 } 80 81 public void setConfirmMessageText(String confirmMessageText) { 82 this.confirmMessageText = confirmMessageText; 83 } 84 85 public void setRegex(String regex) { 86 this.pattern = Pattern.compile(regex); 87 setValidateOnChange(true); 88 } 89 90 public void setRegexMismatchMessageKey(String regexMismatchMessageKey) { 91 this.regexMismatchMessageKey = regexMismatchMessageKey; 92 } 93 94 public void setRegexMismatchMessageText(String regexMismatchMessageText) { 95 this.regexMismatchMessageText = regexMismatchMessageText; 96 } 97 98 public void renderInternal(PrintWriter writer) { 99 if (confirm) { 100 DocumentWriter doc = new DocumentWriter(writer); 101 doc.start(Html.DIV).attribute(Html.COMMON_CLASS, "confirm-text"); 102 doc.body(); 103 super.renderInternal(writer); 104 String msg = MessageUtils.getMessage(this, getConfirmMessage()); 105 doc.start(Html.P).body(msg).end(); 106 107 doc.startEmpty(Html.INPUT) 108 .attribute(Html.INPUT_TYPE, getType()) 109 .attribute(Html.COMMON_CLASS, getStyleClass()) 110 .attribute(Html.INPUT_NAME, getConfirmParamName()) 111 .attribute(Html.INPUT_VALUE, 112 confirmText != null ? confirmText : getText()); 113 114 doc.closeAll(); 115 } 116 else { 117 super.renderInternal(writer); 118 } 119 } 120 121 public void processRequest(FormRequest request) { 122 if (confirm) { 123 confirmText = request.getParameter(getConfirmParamName()); 124 } 125 super.processRequest(request); 126 } 127 128 protected void validate(boolean formSubmitted) { 129 super.validate(formSubmitted); 130 if (formSubmitted && confirm) { 131 if (!ObjectUtils.nullSafeEquals(getText(), confirmText)) { 132 ErrorUtils.reject(this, "error.textField.confirmationFailed"); 133 } 134 } 135 if (pattern != null && StringUtils.hasLength(getText())) { 136 if (!pattern.matcher(getText()).matches()) { 137 getForm().getErrors().rejectValue(getFieldName(), 138 regexMismatchMessageKey, regexMismatchMessageText); 139 } 140 141 } 142 } 143 144 protected String getConfirmParamName() { 145 return getParamName() + CONFIRM_SUFFIX; 146 } 147 148 protected String getConfirmMessage() { 149 if (confirmMessageText != null) { 150 return confirmMessageText; 151 } 152 else if (confirmMessageKey != null){ 153 return MessageUtils.getMessage(this, confirmMessageKey); 154 } 155 else { 156 return MessageUtils.getMessage(this, getDefaultConfirmMessageKey()); 157 } 158 } 159 160 protected String getDefaultConfirmMessageKey() { 161 return DEFAULT_CONFIRM_MESSAGE_KEY; 162 } 163 164 } 165 | Popular Tags |