2011-09-20 20 views
15

Tôi đang cố gắng xác thực hai trường mật khẩu bằng JSF nhưng không tốt cho đến bây giờ, tôi tìm kiếm nó trên google nhưng mọi thứ về JSF 1.2 và khá khó hiểu, tôi đang sử dụng JSF 2.0.Làm cách nào để xác thực hai trường mật khẩu bằng ajax?

Đây là những gì tôi đang làm cho đến nay:

 <h:outputLabel for="password" value="Password:" /> 
     <h:inputSecret id="password" value="#{register.user.password}" > 
      <f:ajax event="blur" listener="#{register.validatePassword}" render="m_password" /> 
     </h:inputSecret> 
     <rich:message id="m_password" for="password"/> 

     <h:outputLabel for="password_2" value="Password (again):" /> 
     <h:inputSecret id="password_2" value="#{register.user.password_2}" > 
      <f:ajax event="blur"  listener="#{register.validatePassword}" /> 
     </h:inputSecret> 

Đây là cách tôi nó là bộ điều khiển của tôi:

public void validatePassword() { 
    FacesMessage message; 

    if (!user.getPassword().equals(user.getPassword_2())){ 
     message = new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "different password"); 
    }else{ 
     message = new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"); 
    } 

    FacesContext.getCurrentInstance().addMessage("form:password", message); 
} 

Bất kỳ chàng trai ý tưởng?

+0

Điều gì có nghĩa là nó hoạt động nhưng không tốt như vậy? Nó có chậm không? Không chắc tôi hiểu những gì bạn muốn thay đổi và tại sao. – jdias

+0

Hi @ jdias, ý tôi là không hoạt động theo cách mà nó giả sử. Cảm ơn vì điều đó có thể hơi khó hiểu. –

Trả lời

19

Trước hết, sử dụng thựcValidator để xác thực thông tin nhập. Đừng làm điều đó trong một phương thức hành động sự kiện.

Đối với vấn đề cụ thể của bạn, bạn chỉ cần phải xác định cả trường trong execute thuộc tính của <f:ajax>, nó cụ thể là giá trị mặc định để chỉ thành phần hiện tại. Nếu bạn đính kèm một trình xác nhận hợp lệ cho đầu vào đầu tiên và gửi giá trị của đầu vào thứ hai cùng với một <f:attribute>, thì bạn sẽ có thể lấy nó trong trình xác nhận hợp lệ. Bạn có thể sử dụng thuộc tính binding để liên kết thành phần với chế độ xem. Bằng cách này, bạn có thể chuyển giá trị đã gửi của nó cùng với UIInput#getSubmittedValue().

Dưới đây là một ví dụ Kickoff:

<h:outputLabel for="password" value="Password:" /> 
<h:inputSecret id="password" value="#{bean.password}" required="true"> 
    <f:validator validatorId="confirmPasswordValidator" /> 
    <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" /> 
    <f:ajax event="blur" execute="password confirm" render="m_password" /> 
</h:inputSecret> 
<h:message id="m_password" for="password" /> 

<h:outputLabel for="confirm" value="Password (again):" /> 
<h:inputSecret id="confirm" binding="#{confirmPassword}" required="true"> 
    <f:ajax event="blur" execute="password confirm" render="m_password m_confirm" /> 
</h:inputSecret> 
<h:message id="m_confirm" for="confirm" /> 

(lưu ý rằng tôi thêm required="true" để cả hai thành phần và cũng lưu ý rằng bạn không nhất thiết cần phải ràng buộc giá trị thành phần mật khẩu xác nhận để một tài sản bean được quản lý, đó là vô giá trị trên có anyway)

với validator này

@FacesValidator("confirmPasswordValidator") 
public class ConfirmPasswordValidator implements Validator { 

    @Override 
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 
     String password = (String) value; 
     String confirm = (String) component.getAttributes().get("confirm"); 

     if (password == null || confirm == null) { 
      return; // Just ignore and let required="true" do its job. 
     } 

     if (!password.equals(confirm)) { 
      throw new ValidatorException(new FacesMessage("Passwords are not equal.")); 
     } 
    } 

} 
+0

xác nhậnPassword trong binding = "# {confirmPassword}" là gì? –

+1

@Jsword: nó sẽ liên kết thành phần với khung nhìn bằng chính xác định danh đã cho để nó có sẵn bằng '# {confirmPassword}' ở nơi khác trong khung nhìn (chẳng hạn như trong ' 'của trường đầu tiên để có thể thu được trong trình xác thực). – BalusC

+0

Tôi hiểu bây giờ chức năng của 'ràng buộc' BalusC, cảm ơn cho dude giúp đỡ. –

0

Với đường may 2, bạn có thành phần <s:validateEquality> và bạn không cần phải viết mã. Đối với JSF2, bạn có các mô-đun Seam 3, đặc biệt là Faces module và Xác thực Mẫu Cross-field. Một ví dụ:

Trước tiên, bạn phải sử dụng thẻ s:validateForm:

<h:form id="passwordForm"> 
    <h:inputSecret id="newPassword" 
     required="true" 
     redisplay="true" 
     value="#{passwordController.newPassword}"> 
    </h:inputSecret>    
    <h:inputSecret id="confirmationPassword" 
     value="#{passwordController.confirmPassword}" 
     required="true" 
     redisplay="true"> 
    </h:inputSecret> 
    <h:commandButton id="submit" value="Submit" action="#{passwordController.submitPassword}" /> 
    <s:validateForm validatorId="passwordValidator" /> 
</h:form> 

và Validator tương ứng đối với hình thức mật khẩu ở trên sẽ giống như thế này:

@FacesValidator("PasswordValidator") 
public class PasswordValidator implements Validator 
{ 
    @Inject 
    @InputField 
    private String newPassword; 

    @Inject 
    @InputField 
    private String confirmPassword; 

    @Override 
    public void validate(final FacesContext context, final UIComponent comp, final Object values) throws ValidatorException 
    { 
     if (!confirmPassword.equals(newPassword)) 
     { 
     throw new ValidatorException(new FacesMessage("Passwords do not match!")); 
     } 
    } 
} 
+0

Tôi đã tìm một giải pháp tương tự trong JSF2 thuần túy, nhưng tôi không thể tìm thấy nó. Bạn có biết điều này có tồn tại trong JSF2 không? Đặc biệt chú thích @InputField có vẻ hữu ích. –

0

Bạn có thể u se Primefaces p: thẻ mật khẩu. Vui lòng xem demo example. Nó có thuộc tính khớp với thuộc tính phải là id của mật khẩu xác nhận.

<p:panel header="Match Mode"> 
    <p:messages id="messages" showDetail="true" autoUpdate="true"/> 

    <h:panelGrid columns="2" id="matchGrid">      
       <h:outputLabel for="pass" value="Password " /> 
       <p:password id="pass" value="#{passwordBean.password}" match="confirmPass" required="true"/> 

       <h:outputLabel for="confirmPass" value="Confirm Password " /> 
       <p:password id="confirmPass" value="#{passwordBean.confirmPassword}" required="true"/> 
    </h:panelGrid> 

    <p:commandButton id="saveButton" update="matchGrid" value="Save" /> 
</p:panel> 
Các vấn đề liên quan