简体   繁体   English

f:validateRegex对于不需要的inputText是可能的吗?

[英]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 /> . 据我所知,验证完全按照实现<b:inputText />时的设计方式进行。

To avoid misunderstandings, I've uploaded the reproducer to GitHub. 为避免误解,我已将复制器上传到GitHub。 It's a part of our BootsFaces showcase. 这是我们的BootsFaces展示柜的一部分。 If you're familiar with Maven, you should get it up and running in a few minutes. 如果您熟悉Maven,则应在几分钟内启动并运行它。 This is the commit containing the reproducer: https://github.com/TheCoder4eu/BootsFacesWeb/commit/4633fce03e8984005ae6c0698ade97e5da8d65e8 这是包含复制者的提交: 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. 不管是否有输入,始终会检查<f:validateRegex /> 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!) (注意问号!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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