001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.validator;
016
017 import java.util.Collection;
018
019 import org.apache.tapestry.IMarkupWriter;
020 import org.apache.tapestry.IRequestCycle;
021 import org.apache.tapestry.TapestryUtils;
022 import org.apache.tapestry.form.FormComponentContributorContext;
023 import org.apache.tapestry.form.IFormComponent;
024 import org.apache.tapestry.form.ValidationMessages;
025 import org.apache.tapestry.multipart.UploadPart;
026 import org.apache.tapestry.valid.ValidationConstants;
027 import org.apache.tapestry.valid.ValidationConstraint;
028 import org.apache.tapestry.valid.ValidationStrings;
029 import org.apache.tapestry.valid.ValidatorException;
030
031 /**
032 * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied.
033 *
034 * @author Howard Lewis Ship
035 * @since 4.0
036 */
037 public class Required extends BaseValidator
038 {
039 public Required()
040 {
041 }
042
043 public Required(String initializer)
044 {
045 super(initializer);
046 }
047
048 public boolean getAcceptsNull()
049 {
050 return true;
051 }
052
053 public void validate(IFormComponent field, ValidationMessages messages, Object object)
054 throws ValidatorException
055 {
056 if ((object == null)
057 || (String.class.isInstance(object) && (((String) object).length() == 0))
058 || (Collection.class.isInstance(object) && ((Collection) object).isEmpty())
059 || (UploadPart.class.isInstance(object) && ((UploadPart) object).getSize() < 1))
060 {
061 String message = buildMessage(messages, field);
062 throw new ValidatorException(message, ValidationConstraint.REQUIRED);
063 }
064 }
065
066 private String buildMessage(ValidationMessages messages, IFormComponent field)
067 {
068 return messages.formatValidationMessage(
069 getMessage(),
070 ValidationStrings.REQUIRED_FIELD,
071 new Object[]
072 { field.getDisplayName() });
073 }
074
075 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
076 FormComponentContributorContext context, IFormComponent field)
077 {
078 context.registerForFocus(ValidationConstants.REQUIRED_FIELD);
079
080 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.require_field(event, '");
081 buffer.append(field.getClientId());
082 buffer.append("', ");
083 buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
084 buffer.append("); }");
085
086 context.addSubmitHandler(buffer.toString());
087 }
088
089 /**
090 * Returns true, that's what Required means!
091 */
092 public boolean isRequired()
093 {
094 return true;
095 }
096 }