01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.frontend.editor;
17:
18: import org.apache.cocoon.forms.validation.WidgetValidator;
19: import org.apache.cocoon.forms.validation.ValidationError;
20: import org.apache.cocoon.forms.formmodel.Widget;
21: import org.apache.cocoon.forms.formmodel.Field;
22: import org.outerj.daisy.frontend.HtmlHelper;
23:
24: /**
25: * Validates that the part has a value, if required & if validation enabled.
26: */
27: class PartRequiredValidator implements WidgetValidator {
28: private DocumentEditorForm documentEditorForm;
29: private boolean isRequired;
30: private boolean htmlMode;
31:
32: public PartRequiredValidator(DocumentEditorForm documentEditorForm,
33: boolean isRequired, boolean htmlMode) {
34: this .documentEditorForm = documentEditorForm;
35: this .isRequired = isRequired;
36: this .htmlMode = htmlMode;
37: }
38:
39: public boolean validate(Widget widget) {
40: boolean success = true;
41: if (documentEditorForm.getValidateOnSave() && isRequired) {
42: if (htmlMode) {
43: if (HtmlHelper.isEmpty((String) widget.getValue())) {
44: ((Field) widget)
45: .setValidationError(new ValidationError(
46: "editdoc.part-required", true));
47: success = false;
48: }
49: } else {
50: if (widget.validate() && widget.getValue() == null) {
51: ((Field) widget)
52: .setValidationError(new ValidationError(
53: "editdoc.part-required", true));
54: success = false;
55: }
56: }
57: }
58: return success;
59: }
60: }
|