1

I want one requiredmessage for two h:selectOneMenu components, that is when a user submits the form if one of those selects values is not null, the bean method should be called, otherwise(both of them null) : I should tell him to select at least one, is it possible? or do I have to use JS. Thanks in advance,

abbr
  • 33
  • 1
  • 8
  • Like this: http://showcase.omnifaces.org/validators/validateOne – Kukeltje Jan 02 '18 at 12:33
  • great solution! but is it available in primefaces, I searched for it in PF, found nothing similar – abbr Jan 02 '18 at 12:44
  • No it is not. Start using OmniFaces. Please do, it is a good choice for more reasons... Look at their showcase. Lots of useful things. When I start a new project I always include lots of apache commons things, omnifaces and deltaspike... – Kukeltje Jan 02 '18 at 12:45
  • I don't have a lot of experience in jsf but if I include ominifaces alongside PF would it garble things or make my project too heavy, I'll include it just for this matter , thanks for all the help – abbr Jan 02 '18 at 12:51
  • No, it would not garble things and it is not 'heavy'. Only the things you do use are loaded but it is by itself very light-weight already. – Kukeltje Jan 02 '18 at 13:07
  • I found a solution for the problem here: https://stackoverflow.com/questions/22971646/how-to-make-one-field-from-two-required-at-least-with-jsf-primefaces – abbr Jan 02 '18 at 14:34
  • That is a 'solution' but just for this very specific problem. Using omnifaces makes you more flexible in (almost) any situation – Kukeltje Jan 02 '18 at 19:53
  • Please help I have a demo tomorow and I did use omnifaces, didn't do the work and when I wanted to uninstall it(remove it) it did garble things with this message: GRAVE: ???????????????????????????????????????????????????????????????????????????????? ? ?? ? ? ? ? ????? ? OmniFaces failed to initialize! ? ? ???? ?????? ? ? ? ?????? ??????? ? This OmniFaces version requires CDI ... – abbr Jan 03 '18 at 09:45
  • now I just want to uninstall it and I couldn't I removed all – abbr Jan 03 '18 at 09:57
  • it's okey now, I just had to delete the omnifaces jar from the libs in target folder (I could've just delete the two last comments but let's keep this one to help people with the same problem cauz it blocks the project running) – abbr Jan 03 '18 at 10:04

1 Answers1

2

I'd use the OmniFaces validateOne validator for this or the validateOneOrMore if filling both is allowed too.

Code example from their site:

<h:form>
    <h3>Please fill out only one of two fields</h3>
    <o:validateOne id="one" components="foo bar" />

    <h:panelGrid columns="3">
        <o:outputLabel for="foo" value="Foo" />
        <h:inputText id="foo" />
        <h:message for="foo" />

        <o:outputLabel for="bar" value="Bar" />
        <h:inputText id="bar" />
        <h:message for="bar" />

        <h:panelGroup />
        <h:commandButton value="submit">
            <f:ajax execute="@form" render="@form" />
        </h:commandButton>
        <h:panelGroup>
            <h:message for="one" />
            <h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
        </h:panelGroup>
    </h:panelGrid>
</h:form>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47