简体   繁体   中英

f:validateRegex for not required inputText it's possible?

I have the following code:

<b:inputText id="txtBuscarPorCodigo" 
      placeholder="123456-78-90123" 
      col-sm="6" 
      required="true" 
      requiredMessage="Es necesario este dato de la manera en la que se indica"
      rendered="#{miControlador.buscarPor == 'codigo'}"
      value="#{miControlador.miBean.codigo}" 
      label="Código:" >
          <f:validateRegex id="validateRegex" 
                pattern="^([0-9]{4}-[0-9]{2}-[0-9]{5})$" />
</b:inputText>

And, it works... BUT. If I delete the next lines:

required="true" 
requiredMessage="Es necesario este dato de la manera en la que se indica"

the validation does not work now.

Do you know some code for the places that does not required?

I mean, I want to the user write in the place nothing or how i demand exactly.

Thanks for reading and I'm sorry for my orthography but I'm not an English speaker.

I've tried to reproduce your bug. As far as I can see, the validation works exactly the way I designed to when I implemented <b:inputText /> .

To avoid misunderstandings, I've uploaded the reproducer to GitHub. It's a part of our BootsFaces showcase. If you're familiar with Maven, you should get it up and running in a few minutes. This is the commit containing the reproducer: https://github.com/TheCoder4eu/BootsFacesWeb/commit/4633fce03e8984005ae6c0698ade97e5da8d65e8

在此处输入图片说明

So I suppose there's no bug. But maybe there's a simple misunderstanding. The <f:validateRegex /> is always checked, no matter whether there's an input or not. If I understand you correctly, you want to check the regular expression only if the input is not empty.

In that case, my approach is to include the empty string with the regular expression, like so:

<f:validateRegex pattern="^([0-9]{4}-[0-9]{2}-[0-9]{5})?$" />

(Notice the question mark!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM