Tag Archive for 'frameworks'

observe_field in symfony and it’s options

While looking for possible options for the observe_field() helper in symfony, I noticed there wasn’t a lot of documentation around to describe what the possible options are to pass to it. After a bit of searching, I found what I wanted and thought I’d share it in case someone else would need a similar thing:

        < ?php echo object_input_tag($server, 'getMainIp') ?>
        <div id="valid_ip"></div>
        < ?php echo observe_field('main_ip', array(
                'url' => '@lookup_ip',
                'frequency' => 3,
                'update' => 'valid_ip',
                'with' => "'ip='+$('main_ip').value",
                'loading' => "Element.show('indicator')",
                'complete' => "Element.hide('indicator');"
        )) ?>
        <div id="indicator" style="display:none"></div>

I needed to create an input field for an IP. If you want to change the IP, a request should be made to check if the IP is still available or not.

What this basically does is check if the field 'main_ip' (this is the object_input_tag() field) changes, and it does that every 3 seconds. If it has changed, we go to the url '@lookup_ip' and pass it one parameter, named ‘ip’ with the value of the input field we are observing. During loading the request, we show an indicator that we’re loading in the background, and if the request is complete, the indicator is hidden and the div 'valid_ip' is filled with the output of our action.

Pretty easy if you have a look at the code, but there aren’t much docs around that give examples for this. A good resource for these things is the Symfony JS and Ajax cheat sheet you can find here. (I chose the english version ;))