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.outerj.daisy.repository.schema.FieldTypeUse;
22:
23: import java.util.Iterator;
24:
25: /**
26: * Checks that required fields have a value, if validation has not been disabled.
27: */
28: public class CheckFieldsFormValidator implements WidgetValidator {
29: private DocumentEditorForm documentEditorForm;
30:
31: public CheckFieldsFormValidator(
32: DocumentEditorForm documentEditorForm) {
33: this .documentEditorForm = documentEditorForm;
34: }
35:
36: public boolean validate(Widget widget) {
37: boolean success = true;
38:
39: if (documentEditorForm.getValidateOnSave()) {
40: Iterator childWidgetsIt = documentEditorForm
41: .getFieldsForm().getChildren();
42: while (childWidgetsIt.hasNext()) {
43: Widget childWidget = (Widget) childWidgetsIt.next();
44: if (childWidget.getId().startsWith("field_")) {
45: FieldEditor fieldEditor = (FieldEditor) childWidget
46: .getAttribute("fieldEditor");
47: FieldTypeUse fieldTypeUse = documentEditorForm
48: .getDocumentType().getFieldTypeUse(
49: fieldEditor.getFieldTypeUse()
50: .getFieldType().getId());
51: if (fieldTypeUse.isRequired()
52: && !fieldEditor.hasValue(childWidget)) {
53: fieldEditor
54: .setValidationError(new ValidationError(
55: "editdoc.field-required", true));
56: success = false;
57: }
58: }
59: }
60: }
61:
62: return success;
63: }
64: }
|