adding validation to my save method

浪子不回头ぞ 提交于 2019-12-11 03:08:28

问题


In my xpages application I want to save documents via a save method in a managed bean. However I do not know how to include validation rules in this save method and send a message back to the XPage. I tried several examples that I have found googling but none of them worked. Perhaps someone can shine a light on how I could add a simple server-side validation without setting up a separate validation bean or so?

Here is what I have come up so far:

public void save(Employee employee) throws ValidatorException {

        try {

            Document doc;           openDBPlusView();

            if (employee.getUnid() != null) {
                doc = view.getDocumentByKey(employee.getUnid(), true);
            } else {
                doc = null;
            }
            if (doc == null) {
                doc = db.createDocument();
            }
            if (employee.getEmail().equals("")) {
                FacesMessage message = new FacesMessage();
                message.setDetail("Email missing");
                throw new ValidatorException(message);
            } else {
                doc.replaceItemValue("email", employee.getEmail());
            }
            doc.replaceItemValue("Form", "employee");
            doc.save(true, false);          
            recycleNotesObjects();
        } catch (NotesException e) {

            e.printStackTrace();
        }
    }

回答1:


Try this sample, it should solve your problem:

Employee (your data object):

package com.test.data;

public class Employee {

    private String email;

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

}

FormBean (your bean for saving data in notes backend):

package com.test.bean;

import java.io.Serializable;

import com.test.data.Employee;

public class FormBean implements Serializable {

    private static final long serialVersionUID = -1042911106119057617L;

    public void save(Employee employee) {

        System.out.println("Keep in  mind: Save will only be entered if no validation errors! JSF Lifecycle!");
        System.out.println(employee.getEmail());

    }

}

FormValidators (a bean for validation, in your example the email address):

package com.test.validation;

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public class FormValidators implements Serializable {

   private static final long serialVersionUID = 7157377379640149444L;

   public void validateEmail(FacesContext facesContext, UIComponent component, Object value) {
      // Check if email is valid or not
      if (value.toString().equals("") || value.toString().indexOf('@') == -1) {
         // Create a message -> email is invalid
         FacesMessage message = new FacesMessage("Email is invalid!");
         // Throw an exception so that it prevents document from being saved
         throw new ValidatorException(message);
      }
   }

}

faces-config:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <managed-bean>
    <managed-bean-name>formBean</managed-bean-name>
    <managed-bean-class>com.test.bean.FormBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
  </managed-bean>
  <managed-bean>
    <managed-bean-name>formValidators</managed-bean-name>
    <managed-bean-class>com.test.validation.FormValidators</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
  </managed-bean>
  <!--AUTOGEN-START-BUILDER: Automatically generated by IBM Domino Designer. Do not modify.-->
  <!--AUTOGEN-END-BUILDER: End of automatically generated section-->
</faces-config>

XPage (for testing your requirements/ putting all together):

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">


    <xp:br></xp:br>
    <xp:br></xp:br>

    <xp:messages id="messages1"></xp:messages>

    <xp:br></xp:br>
    <xp:br></xp:br>

    <xp:inputText id="txtEmail" validator="#{formValidators.validateEmail}" value="#{requestScope.email}"
        required="true">
    </xp:inputText>

    <xp:br></xp:br>
    <xp:br></xp:br>

    <xp:button id="btnSave" value="Save">
        <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:var employee:com.test.data.Employee = new com.test.data.Employee();
employee.setEmail(requestScope.email);
formBean.save(employee);}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>

</xp:view>

Keep in mind: Save will only be entered if no validation errors. JSF Lifecycle!




回答2:


What I have done in the past is have the method return a boolean back to my save button, false if the save was not completed. Then in the save button SSJS code I call the bean to get the message to display in the displayerror (or errors) controls. I am not sure validatorException works outside of a custom validator.

Another option is to validate your input texts and don't do the save unless the various controls pass the validation.



来源:https://stackoverflow.com/questions/43217207/adding-validation-to-my-save-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!