Nerds who love the symfony-project
23 Apr
In general, you will always run into issues trying to use the ‘/’ character as part of a parameter in your URL in web development. Recently I’ve run into issues trying to pass a parameter in my URL that has the forwardslash character. For example - trying to pass a URL within a URL:
mymodule/myaction/url=http://google.com
In the case above, Apache will return a 404 as it cannot find that URL (it things that http://google.com is part of the URL).
This is a known issue that can be resolved in two ways:
1) urlencode() + ApacheEncodedSlashes
You could use urlencode() to escape your http:// slashes, and then change you’re ApacheEncodedSlashes directive to allow this (further reading). However this does require a Apache change.
2) The quick fix is to simply tell Apache that it is a parameter, not part of the URL, by changing the / to a ?
mymodule/myaction?url=http://google.com
This works fine. However, in Symfony you can’t get this to work if you are trying to use the link_to_remote Ajax Helper function.
The following code:
$url = "http://google.com"; echo link_to_remote($topic->getName(), array( 'update' => array('success' => 'topic_detail', 'failure' => 'topic_detail_err'), 'url' => "mymodule/myaction?t="$url, 'script' => 'true', 'loading' => visual_effect('appear', 'loading_animation'), 'complete' => visual_effect('appear', 'topic_detail') ));
Will produce this URL:
....mymodule/myaction/url=http://google.com
Notice that Symfony converts the ? to a /. This is because we are using link_to_remote. link_to_remote goes through the routing layer that converts all ? to / characters. If we were using url_for, this would not be an issue.
So how do we overcome this problem if we are using link_to_remote? Well it’s simple - use both the Symfony url_for() helper function as well as the PHP http_build_query function. The http_build_query function encodes the URL for us (so ‘/’ get converted to %2F, alongside other characters).
So the end result looks like this:
$url= url_for("mymodlue/myaction")."?".http_build_query(Array("t"=>$topic->getName())); echo link_to_remote($topic->getName(), array( 'update' => array('success' => 'topic_detail', 'failure' => 'topic_detail_err'), 'url' => $url, 'script' => 'true', 'loading' => visual_effect('appear', 'loading_animation'), 'complete' => visual_effect('appear', 'topic_detail') ));
This forces link_to_remote to use the ? instead of replacing it with a /. Hope this helps!
29 Jul
This was meant to be a helpful post on getting an ajax observe_field working on a radiobutton_tag, but it is just as likely to lead to frustration, because there is a problem with prototype which means it won’t work as you may expect (more on that later), but it may still be useful for some, so here’s how you do it:
I have used two radio buttons, one each for for ‘Yes’ and ‘No’. I originally thought you needed to specify the id as an option, but you don’t, it’s made up of the name and value of the radio button, so for the first one below the id is radio_but_yes
echo radiobutton_tag('radio_but[]', 'Yes', false); echo ' Yes '; echo radiobutton_tag('radio_but[]', 'No', true); echo ' No';
Then you just have the usual overserve field, here duplicated for each dropdown. You’ll note I also set script to true as the content I show when selecting yes will also have Javascript in it.
echo observe_field('radio_but_yes', array( 'update' => 'lower_div', 'url' => 'order/Content', 'with' => "'type=' + value", 'script' => 'true' )); echo observe_field('radio_but_no', array( 'update' => 'lower_div', 'url' => 'order/Content', 'with' => "'type=' + value", 'script' => 'true' ));
And of course your Div to display the content.
echo '<div id="lower_div"></div>';
As I said though, it seems there is a problem with prototype (the ajax library Symfony is using to do this) in that it only fires once… So in my case ‘No’ is the default, if someone selects ‘Yes’ the new fields I want will be displayed, however if they then change their mind and switch back to ‘No’ the fields will still be displayed…
So for now, you’ll probabaly only want to use this if that doesn’t matter… for me it does, because mandatory fields will be shown, and if the user doesn’t want them, it will be frustrating that the form won’t submit until they’re completed, so in this case I’m going to just use a checkbox, or you could use a dropdown, both of these work fine.