0

I'm trying to override default primefaces messages when it comes to validation user input when logging in. I managed to get my own message when username and password are not admin/admin and now I'd like to get my own messages when there is no username, password or both.

I've tried the code below (else if lines) but it doesn't work. Tried it for all three cases and it didn't work.

     public class UserLogin{

    private String username;

    private String password;

    private String vrstaPrava;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVrstaPrava() {
        return vrstaPrava;
    }

    public void setVrstaPrava(String vrstaPrava) {
        this.vrstaPrava = vrstaPrava;
    }

    public void login() {
        FacesMessage message = null;
        boolean loggedIn = false;



        if(username != null && username.equals("admin") && password != null && password.equals("admin")) {
            loggedIn = true;
            message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Dobro došli", username);

        } else if (username == null) {
            loggedIn = false;
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Greška prilikom prijave", "Niste unijeli korisničko ime/lozinku");
        } else {
            loggedIn = false;
            message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Greška prilikom prijave", "Pogrešno korisničko ime/lozinka");
        }

        FacesContext.getCurrentInstance().addMessage(null, message);
        PrimeFaces.current().ajax().addCallbackParam("loggedIn", loggedIn);
    }  }
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Dino Kralj
  • 25
  • 6
  • username.equals(""); or username..length()==0; Also could you please translate the messages in English? – Jim_Ktr Sep 16 '19 at 09:23
  • What is the scope of you bean? – Jasper de Vries Sep 16 '19 at 10:24
  • @Jim_Ktr tried both, didnt work. Message if admin/admin "Welcome" + username, else "Error during login, wrong username/password". Selaron no it didnt work. Jasper de Vries didn't put the scope of bean, only put "@ManagedBean" – Dino Kralj Sep 16 '19 at 10:38
  • Did you try to set breakpoints and debug? – Selaron Sep 16 '19 at 11:46
  • @Selaron no I haven't tried it – Dino Kralj Sep 17 '19 at 09:24
  • 1
    Can you please elaborate on what you mean by "it didn't work". Try setting the scope to at least `ViewScoped`. – Jasper de Vries Sep 17 '19 at 10:19
  • @JasperdeVries With code above, when I leave username empty, it returns default growl that comes with primefaces.. it didnt return the message that i wrote in my class – Dino Kralj Sep 18 '19 at 13:10
  • Possible duplicate of [Change the default message "Validation Error: Value is required" to just "Value is required"](https://stackoverflow.com/questions/9155684/change-the-default-message-validation-error-value-is-required-to-just-value) – Kukeltje Oct 19 '19 at 05:38

2 Answers2

0

Not sure what you are using for input but you can always set required = true and input your own message with requiredMessage="yourmessage" for each input.

Example:

<p:inputText id="username" required="true" requiredMessage="Please enter a username" />
  • Like in this existing Q/A? https://stackoverflow.com/questions/9155684/change-the-default-message-validation-error-value-is-required-to-just-value – Kukeltje Oct 19 '19 at 05:37
0

As Rou Jie mentioned, you can use the required + requiredMessage in this case.

You can also customize default messages in the appropriate messages.properties file to avoid repeating requiredMessage for each field.

javax.faces.component.UIInput.REQUIRED={0}: Your custom text here.
doughaase
  • 245
  • 4
  • 13
  • Like in this existing Q/A? https://stackoverflow.com/questions/9155684/change-the-default-message-validation-error-value-is-required-to-just-value – Kukeltje Oct 19 '19 at 05:37