Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RegexValidator not working properly with "match" option set to "false" #5603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
advancingu opened this issue Sep 26, 2012 · 9 comments
Closed

Comments

@advancingu
Copy link

I was trying to use the RegexValidator with the "match" option set to "false". This leads to incorrect behavior, i.e. the validator keeps raising errors for fields that definitely do not match the Regex.

The code in question is here.

The issue appears to be that the "match" parameter is maintained as a string when set to false and then xored upon.

I found this blog that explains the problem in more detail. I couldn't find a corresponding bug that was reported by this person, so I'm reporting it now. The patch described there works for me.

@advancingu
Copy link
Author

Addendum: This is with Symfony 2.1.2-dev

@advancingu
Copy link
Author

A shorter fix than described in the blog would be:

        if (!($constraint->match === 'false') xor preg_match($constraint->pattern, $value)) {

@stof
Copy link
Member

stof commented Sep 26, 2012

Can you paste your mapping for the constraint ?

@advancingu
Copy link
Author

Example:

        <property name="myprop">
            <constraint name="Regex">
                <option name="pattern">/^1/</option>
                <option name="match">false</option>
                <option name="message">my.message</option>
                <option name="groups">
                    <value>group1</value>
                </option>
            </constraint>
        </property>

If the myprop field is set to e.g. 123 the above constraint does not catch as it is supposed to.

@stof
Copy link
Member

stof commented Sep 26, 2012

The bug is in fact in the XmlFileLoader of the validator component. It does not apply the converting rules used in other components for values.

@stof
Copy link
Member

stof commented Sep 26, 2012

btw, using your suggested fix is wrong. It would break it for people using Yaml, PHP or annotations (where defining a boolean as possible natively, without having to hack something in the loader to convert the value)

@advancingu
Copy link
Author

It was just a 5 minute hack that "worked for me (tm)". Seems like you already found the real culprit.

(Friendly question:) Do you have an ETA for a patch? This is blocking some code from going into production over here.

@74Labs
Copy link

74Labs commented Aug 19, 2013

Workaround: use 0 instead of "false". Works in 2.3.3 version.

Example:

<constraint name="Regex">
     <option name="pattern">/\s/</option>
     <option name="match">0</option>
     <option name="message">The value cannot contain spaces</option>
</constraint>

@fabpot
Copy link
Member

fabpot commented Aug 22, 2013

#8831 should fix this issue.

fabpot added a commit that referenced this issue Aug 24, 2013
This PR was merged into the 2.2 branch.

Discussion
----------

[Validator] fixed Boolean handling in XML constraint mappings

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #5603
| License       | MIT
| Doc PR        | n/a

ping @bschussek

Commits
-------

33b0a17 [Validator] fixed Boolean handling in XML constraint mappings (closes #5603)
@fabpot fabpot closed this as completed Aug 24, 2013
fabpot added a commit that referenced this issue Aug 24, 2013
* 2.2:
  [Locale] Fixed: StubLocale::setDefault() throws no exception when "en" is passed
  [Yaml] fixed embedded folded string parsing
  [Validator] fixed Boolean handling in XML constraint mappings (closes #5603)

Conflicts:
	src/Symfony/Component/Locale/Stub/StubLocale.php
	src/Symfony/Component/Locale/Tests/Stub/StubLocaleTest.php
fabpot added a commit that referenced this issue Aug 29, 2013
* 2.3:
  Clear lazy loading initializer after the service is successfully initialized
  [FrameworkBundle] added support for double-quoted strings in the extractor (closes #8797)
  [SecurityBundle] Move format-dependent tests from SecurityExtensionTest
  bumped Symfony version to 2.3.5-DEV
  updated VERSION for 2.3.4
  updated CHANGELOG for 2.3.4
  bumped Symfony version to 2.2.7
  updated VERSION for 2.2.6
  update CONTRIBUTORS for 2.2.6
  updated CHANGELOG for 2.2.6
  clearToken exception is thrown at wrong place.
  fix typo in test skipped message
  [Form] Fixed Form::all() signature for PHP 5.3.3
  [Form] Fixed Form::all() signature for PHP 5.3.3
  [Locale] Fixed: Locale::setDefault() throws no exception when "en" is passed
  [Locale] Fixed: StubLocale::setDefault() throws no exception when "en" is passed
  [Translation] Grammar fix
  [Yaml] fixed embedded folded string parsing
  [Validator] fixed Boolean handling in XML constraint mappings (closes #5603)
  [Translation] Fixed regression: When only one rule is passed to transChoice(), this rule should be used

Conflicts:
	src/Symfony/Component/HttpKernel/Kernel.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants