0

Sorry for my english. When you update a record by ID is not working the validator. If I make the validator via ajax:

<h:inputText id="name" value="#{testMB.instance.name}" required="true">
    <f:validator binding="#{nameValidator}" />
    <f:ajax event="blur" render="nameMessage"/>
</h:inputText>

When I once click on the Save button:

<h:commandButton type="submit" action="#{testMB.save}" value="Save"/>

The validator works on the blur event. If I click on the "save" button second time, then print out the error:

Error Rendering View[/pages/test.xhtml]: java.lang.NullPointerException

And redirects to URL without ID. If I remove the ajax blur in the field, then this error will occur when I once click on the button.

If the validator via actionListener:

<p:commandButton update="@form" actionListener="#{testMB.save}" value="Save"/>

Then it is working. Validator not working when I update a record by ID. Wnen I use the form for create a record, then this also works.

My testMB:

public Entity getInstance() {

    if (instance == null) {
        if (id != null) {
            instance = loadInstance();
        } else {
            instance = createInstance();
        }
    }

    return instance;
}

public Entity loadInstance() {
    return (Entity) getDao().findOne(getId());
}

public Entity createInstance() {
    try {
        return entityType.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

And f:metadata:

<f:metadata>
    <f:viewParam name="testId" value="#{testMB.id}"/>
</f:metadata>

Why not work validation with a regular button and ID?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
zerg
  • 255
  • 1
  • 3
  • 17
  • So, the bean is not view scoped? – BalusC Sep 24 '15 at 13:21
  • No, the bean is view scoped. Now I tried to use session scoped and it is not working also. – zerg Sep 24 '15 at 13:37
  • The error is exactly the same as if I go to the page "test.xhtml" without "?testId=1". – zerg Sep 24 '15 at 13:39
  • `getInstance()` code is kind of weird because it's totally unnecessary on a view scoped bean. A MCVE would help a lot to exclude ambiguity in the question caused by potential design mistakes not visible in the information provided so far. At least, my educated guess tells that this is the answer you're looking for: http://stackoverflow.com/q/17734230 Nonetheless, if your bean is really view scoped, then this should be the design guideline: http://stackoverflow.com/q/8459903 – BalusC Sep 24 '15 at 13:47
  • I to add `f:param` tag for the button and it is working. Thank you very much. And thank you for design guideline. – zerg Sep 24 '15 at 15:39

0 Answers0