如何将对象绑定到多个字段的验证器?

时间:2015-05-22 22:15:02

标签: validation jsf jsf-2

我有一个表单。我将数据保存到数据库。我在表中有唯一的约束(title,lesson_id)。我正在验证此记录以避免重复。

<h:form id="src_form">
    <h:panelGrid id="src_pnl" columns="3">
        <h:outputLabel for="lesson" value="Select a lesson" />
        <p:selectOneMenu id="lesson" binding="#{lesson}" value="#{sourceMB.lesson}" required="true"
            converter="omnifaces.SelectItemsConverter" style="width:150px">
            <f:selectItem itemLabel="(null)" itemValue="" />
            <f:selectItems value="#{lessonMB.lessonList}" var="les"
                itemLabel="#{les.lessonName}" itemValue="#{les}" />
            <p:ajax event="change" update="lesson_msg" />
        </p:selectOneMenu>
        <p:message id="lesson_msg" for="lesson" display="text" />

        <h:outputText value="Title:" />
        <h:inputText id="title" value="#{sourceMB.title}" required="true" >
            <f:validator binding="#{sourceValidator}" />
            <f:attribute name="lesson" value="#{lesson}"/>
            <p:ajax event="blur" update="title_msg" />
        </h:inputText>
        <p:message id="title_msg" for="title" display="text" />

        <p:outputLabel for="type" value="Dosya Biçimi:" />
        <p:selectOneRadio converter="javax.faces.Integer" id="type" required="true" 
            value="#{sourceMB.type}">
            <f:selectItem itemLabel="zip/rar" itemValue="3" />
            <f:selectItem itemLabel="doc/docx" itemValue="2" />
            <f:selectItem itemLabel="image" itemValue="1" />
            <p:ajax event="change" update="type_msg" />
        </p:selectOneRadio>
        <p:message id="type_msg" for="type" display="text" />

    </h:panelGrid>
    <p:separator />
    <p:commandButton ajax="true" value="Upload" update="@([id$=src_dlg])"
        oncomplete="PF('srcDialog').show()" />
</h:form>

Validator.class

@Named(value = "sourceValidator")
@RequestScoped
public class sourceValidator implements Validator {

    @Autowired
    SourceService sourceService;
    Logger log = Logger.getLogger(sourceValidator.class);

    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {

        log.info("validation is starting.");
        final String title = (String) value;
        log.info(title);

        final UIInput lessonComponent = (UIInput) component.getAttributes()
                .get("lesson");
        final Lesson lesson = (Lesson) lessonComponent.getSubmittedValue();
        log.info(lesson); // This Object is getting null.
        log.info("Ttile: " + title + " Lesson: " + lesson.getLessonName());

        if (title == null || title.isEmpty() || lesson == null) {
            return; // Let required="true" do its job.
        }

        if (sourceService.checkDuplicates(lesson, title)) {
            final FacesMessage facesMsg = new FacesMessage("Duplicated");
            facesMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(facesMsg);
        }

    }

}

Lesson对象获取null。如何将Lesson对象绑定到具有title值的验证器?提前谢谢。

0 个答案:

没有答案