8000 Creating a route with an empty (optional) parameter doesn't work anymore · Issue #7316 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Creating a route with an empty (optional) parameter doesn't work anymore #7316

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
xantos05 opened this issue Mar 9, 2013 · 14 comments
Closed
Labels

Comments

@xantos05
Copy link
xantos05 commented Mar 9, 2013

Since updating to Symfony 2.2.0 i can't create a route with an empty parameter. This parameter is indicated as optional and worked fine in previous versions of Symfony. Of course i can build in a check but i wonder if this is normal behaviour.

My Route looks like this:
show_products:
path: /showProduct/{size}/{nr}
defaults: { _controller: TestShopBundle:Product:showProduct, nr: 0 }

In Twig:
{{ path('show_product', { 'size': 1, 'nr': entity.nr }) }}

entity.nr sometimes has a null value!

The following error appears:
An exception has been thrown during the rendering of a template ("Parameter "nr" for route "show_product" must match "[^/]++" ("" given) to generate a corresponding URL.") in TestShopBundle:Frontend:index.html.twig at line 270

Probably not a very serious bug but it's not described in the upgrade document -> https://github.com/symfony/symfony/blob/2.2/UPGRADE-2.2.md

@xantos05
Copy link
Author
xantos05 commented Mar 9, 2013

Thanks, that seems to do the trick! Cannot find this option in the
Symfony2 documentation though.

Op 09-03-13 21:30, Abdellatif AitBoudad schreef:

Hi just you must set strict_requirements as null in config.yml to make
it work

|framework:
....
router:
strict_requirements: null
|


Reply to this email directly or view it on GitHub
#7316 (comment).

@aitboudad
Copy link
Contributor

I think it's a bug, it must replace defaults values only if it's different than

null, '', false

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L151

what do you think @Tobion

I can fix this if needed. :)

@aitboudad
Copy link
Contributor

@xantos05

strict_requirements

    # set to true to throw an exception when a parameter does not match the requirements
    # set to false to disable exceptions when a parameter does not match the requirements (and return null instead)
    # set to null to disable parameter checks against requirements
    # 'true' is the preferred configuration in development mode, while 'false' or 'null' might be preferred in production

@xantos05
Copy link
Author

Sorry, just deleted my last two postings. Never mind..

@aitboudad
Copy link
Contributor

@xantos05 Temporary solution ;)

 {{ path('show_product', { 'size': 1, 'nr': entity.nr ?:0 }) }}

@Tobion
Copy link
Contributor
Tobion commented Mar 12, 2013

I will fix it. A null value for an optional parameter should indeed be ignore 8000 d. This is also in line with what I proposed in #5410.

@aitboudad
Copy link
Contributor

@Tobion ok, thank :)

@Tobion
Copy link
Contributor
Tobion commented Mar 14, 2013

made a PR

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

Commits
-------

d7a7434 [Routing] fix url generation for optional parameter having a null value

Discussion
----------

[Routing] fix url generation for optional parameter having a null value

| Q             | A
| ------------- | ---
| Bug fix?      | [yes]
| New feature?  | [no]
| BC breaks?    | [no]
| Deprecations? | [no]
| Tests pass?   | [yes|]
| Fixed tickets | [#7316]
| License       | MIT
@fabpot fabpot closed this as completed Mar 14, 2013
@gddfgf
Copy link
gddfgf commented May 29, 2013

when i do not define a default value for the url parameter it does not work. if i do, it works. this is a change from version 2.1 to version 2.2.

@Tobion
Copy link
Contributor
Tobion commented May 30, 2013

no there is no change

@reecefowell
Copy link
Contributor

This still seems to be broken.

After migrating my bundles from Symfony 2.1 to 2.2, it seems the router has no problem matching the url, its rather when you generate the url via the router.

In my case:

ccdn_forum_user_category_index:
    path:     /{forumName}
    defaults: { _controller: CCDNForumForumBundle:UserCategory:index, _locale: en, forumName: default }

Will match the route to load the page, but at any point you try to generate this route without for example 'forumName' having a valid value, like:

$this->router->generate('ccdn_forum_user_category_index', array('forumName' => '');

Then it just throws this:

Parameter "forumName" for route "ccdn_forum_user_category_index" must match "[^/]++" ("" given) to generate a corresponding URL.

In my case, the array value for 'forumName' is set from a variable, that may or may not have null value. When null, that is when the exception is thrown.

I would say this is a bug and is still present, as i am on 2.2.11 and im stumped right now, this worked fine in 2.1.

@Tobion
Copy link
Contributor
Tobion commented Jan 16, 2014

'' is not null. You need to pass $this->router->generate('ccdn_forum_user_category_index', array('forumName' => null); or just $this->router->generate('ccdn_forum_user_category_index');

@jayesbe
Copy link
jayesbe commented Jan 30, 2015

I just found this issue. Was working for me as well in 2.1. I just upgrade to 2.6 and my registration page now fails to load. The issue for me is I have an AJAX lookup on a username field.. but initially that username is empty.

var checkUsername = function(e) {
    var ctx = $(this);
    $.getJSON('{{ path('check_username', {'username': ''}) }}/'+ctx.val(), function(data) {
        checkValueHandler(ctx, data);
    });
};

I tried removing the parameter field alltogether.. but then it throws an error saying the field is required. Empty value should be a valid value for username. Why should the above code fail ?

@jayesbe
Copy link
jayesbe commented Jan 31, 2015

nvm. I needed to add a null default to the route..

/**
 * check if the username is already taken
 * 
 * @Route("/register/u/{username}", defaults={"username" = null}, name="check_username")
 * @Method({"GET"})
 */
public function checkUsernameAction($username)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants
0