0

This is my code main.xhtml. I am trying validate the input.

This is a method-validator:
public void validateCorseCode(FacesContext context, UIComponent component,         Object value)
      throws ValidatorException {
    if(value == null) {
      return;
    }
    String data = value.toString();
    //CourseCode must start with LUV
    if(!data.startsWith("My")) {
      FacesMessage message = new FacesMessage("CourseCode must start with     LUV");
      throw new ValidatorException(message);
    }
  }

This is my code in jsf:

<h:inputText value="#{studentValidFour.courseCode}" label="Course code"     id="courseCode" required="true"
                                    validator="#    {studentValidFour.validateCorseCode}" >
</h:inputText>
          <h:messages for="courseCode" styleClass="error"/> 

This is a error message:

Caused by: java.lang.ClassCastException: class     javax.faces.application.FacesMessage cannot be cast to class java.lang.String     (javax.faces.application.FacesMessage is in unnamed module of loader     'javax.faces.api@2.3.9.SP02' @3983b87e; java.lang.String is in module     java.base of loader 'bootstrap')
  • 1
    Hi, start by analyzing the full stacktrace, it is **always** about the full stacktrace... That will show you where the error actually occurs. Initially I thought you should not pass a `FacesMessage` in a `ValidationException` but since I was not sure (and would sort of have expected a compile time error) I looked it up in https://stackoverflow.com/questions/6047866/how-to-perform-validation-in-jsf-how-to-create-a-custom-validator-in-jsf and that seems to be correct. Only in that you use the validator differntly. Can you see if using it in another way like in the link makes it work? – Kukeltje Oct 19 '19 at 18:23
  • 1
    Thanks for your reply, yes I saw this link and also saw the differences. Perhaps my code is already deprecated , I will definitely try that option and write about the results. – Артем Oct 20 '19 at 14:17
  • You still forgot to include the full stack trace. The answer is there. – BalusC Oct 21 '19 at 10:27

0 Answers0