Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:27:04+00:00 2026-06-09T21:27:04+00:00

I’ve got a little problem here with my application. I’d like to check if

  • 0

I’ve got a little problem here with my application. I’d like to check if fields password and confirm password match together, so I tried to do it like in the first answer for this question: Cross field validation with Hibernate Validator (JSR 303)

The problem is that it actually doesn’t work and I HAVE NO IDEA WHY. Please, help me! This is my first post here, so please don’t be too harsh for me.

Here’s my Annotation :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints;

import java.lang.annotation.*;
import javax.validation.Constraint;
import javax.validation.Payload;

/**
 *
 * @author lukasz
 */
@Documented
@Constraint(validatedBy = FieldMatchValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldMatch {

String message() default "{pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String first();

String second();
}

Here’s my ValidatorClass:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author lukasz
 */
public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object> {

    private String firstFieldName;
    private String secondFieldName;

    @Override
    public void initialize(FieldMatch constraintAnnotation) {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        try {
            String sFirstField = BeanUtils.getProperty(value, firstFieldName);
            String sSecondField = BeanUtils.getProperty(value, secondFieldName);
            if(sFirstField.equals(sSecondField)){
                return true;
            }
        } catch (IllegalAccessException ex) {
            Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
        Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
        Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}

And here’s my bean:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.lodz.p.zsk.ssbd2012.ssbd12.Beans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.CheckEmail;
import pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch;
import pl.lodz.p.zsk.ssbd2012.ssbd12.entities.Account;
import pl.lodz.p.zsk.ssbd2012.ssbd12.mok.endpoint.MokEndpointLocal;

/**
 *
 * @author krzys
 * @author lukasz
 */

@ManagedBean
@RequestScoped
@FieldMatch(first = "password", second = "password2", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch}")
public class RegisterBean {

    /**
     * Creates a new instance of RegisterBean
     */

    @EJB
    MokEndpointLocal endpoint;

    @Size(min = 3, max = 16, message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.login.size}")
    @Pattern(regexp = "[a-zA-Z0-9]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.login.invalid}")
    private String login;

    @Size(min = 6, max = 64, message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.password.size}")
    @Pattern(regexp = "[a-zA-Z0-9]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.password.invalid}")
    private String password;

    private String password2;

    @Pattern(regexp = "[A-Ż][a-ż]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.name.invalid}")
    private String name;

    @Pattern(regexp = "[A-Ż][a-ż]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.surname.invalid}")
    private String surname;

    @CheckEmail
    private String email = "";


    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

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

    public String register ()
    {
//       Account account = new Account();
//       account.setLogin(login);
//       account.setHaslo(password);
//       account.setImie(name);
//       account.setNazwisko(surname);
//       account.setEmail(email);
//       endpoint.register(account);
       return "registerSucces";
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }
}

And here’s JSF:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <h:outputStylesheet name="menu.css" library="css" />
    <title>#{messages.registration}</title>
</h:head>

<h:body>
    <ui:composition template="./../resources/mainTemplate.xhtml">

        <h4>#{messages.registration}</h4>
    <ui:define name="content">
        <h4>#{messages.registration}</h4>
        <h1>#{messages.registrationInfo1}</h1>
    #{messages.registrationInfo2}
    <h:form>
        <h2>
            <h:outputText value="#{messages.loginForm}"/>
            <h:inputText id="login" value="#{registerBean.login}" />  
        </h2>
        <h2>
            <h:outputText value="#{messages.passwordForm}" />
            <h:inputSecret id="password" value="#{registerBean.password}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.repeatPasswordForm}"/> 
            <h:inputSecret id="confirm" value="#{registerBean.password2}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.nameForm}"/>
            <h:inputText id="name" value="#{registerBean.name}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.surnameForm}"/>
            <h:inputText id="surname" value="#{registerBean.surname}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.emailForm}"/>
            <h:inputText id="email" value="#{registerBean.email}" />
        </h2>
        <h2>    
            <h:commandButton value="#{messages.send}" action="#{registerBean.register()}" />
        </h2>
    </h:form>
    </ui:define>
    </ui:composition>
</h:body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:27:05+00:00Added an answer on June 9, 2026 at 9:27 pm

    Class-level constraints are not triggered automatically by JSF during validation phase. You can use only field-level constraints (moreover not all fields are valuated by JSF but only those that are in your facelet).

    If you want to use bean validation you can perform validation manually:

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Test>> violations = validator.validate( this, Default.class );
    

    You can do it in registration method of your bean or after update model phase (but I’ve never tried it).

    Anyway you can use JSF validation instead of bean validation or check the passwords directly in the registration method:

    public String registration() {
        ...
    
        if ( !password.equals(password2) ) {
            FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( "Passwords do not match" ) );
            return null;
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.