symfonynerds.com

Nerds who love the symfony-project

Archive for July, 2008

Symfony ‘time’ input field

Unless I’m just going blind, there is no mention of this in the manual, but if you want to have a dropdown on your form for someone to input a time, there is a helper for it… just use:

echo select_time_tag('cutover_time', 'now');

The first arg is the name for the field, the second is the time to display. ‘Now’ means it will display the current (to your server) time when someone loads the form.

If you’re also using a input_date_tag in the same form (quite likely you’d get a date and time together), then the helper will be automatically loaded for you, but if you just had this on a form without an input_date_tag you would also need to include somewhere:

use_helper('DateForm');
  • 2 Comments
  • Filed under: 1.0, forms
  • 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.

  • 5 Comments
  • Filed under: 1.0, ajax, forms
  • Symfony Tip: 1.1 Model Building

    Consider this a twitter post more than blog material… but I am just playing around with Symfony 1.1 CRUD generation, and it seems there is an extra step from v1.0… After generating your model, but before trying to generate a CRUD, you need to run the command:

    symfony propel:build-forms

    If you don’t, then when you do a symfony propel-generate-crud, you will get the error:

    Fatal error: Class 'CustomerForm' not found in /usr/share/php/symfony/generator/sfAdminGenerator.class.php on line 722

    In the example above its Class ‘CustomerForm’ because Customer was the name of the class I chose to try and generate the form for.

    I’m off to do some more reading on forms/CRUD in 1.1, I’m getting the feeling it’s going to be a LOT different to 1.0….eHabib, you better add another offline package for what I just linked to I think, it will be my weeks reading in the can… I mean office… (ok I did mean can)

  • 5 Comments
  • Filed under: 1.1, forms, generators
  • If you are writing your schema.yml file, the Symfony book isn’t too detailed as to what you can and can’t put in your schema.yml. I’d recommend you head over to the Propel Guide, and read the appendix for detailed documentation.

    Before I begin, this is not a general overview of object_select_tag, the book has a fair bit about it, but rather, details on using it to achieve a fairly specific objective I had…

    I needed:

    • A dropdown select
    • The dropdown to be populated with values from a DB table
    • Not using ALL the values from the DB table, only a subset
    • Include a blank entry at the start so an observe_field could be used on it to give magic AJAX powers to the dropdown (without a blank field, you can’t trigger a request on the first entry being selected unless you first select something else)

    I went through a few different attempts (which I will blog on later) before getting to this final one, but each came up short in meeting ALL of the above requirements at once. The answer was actually found by the one and only eHabib, but it impressed me so much I thought I would post on it first and steal his glory. :D

    Step 1: In the indexSuccess.php template

    echo object_select_tag('', '', array('name'=>'request_type', 'id'=>'request_type', 'related_class'=>'Property', 'peer_method'=>'getRequestTypes', 'include_blank'=>'true'));

    So to explain the above arguments (have a read of the API also):

    • ” - blank because in this case I don’t want to preselect any value in the list
    • ” - blank because, well, I _think_ because we specify the related_class it no longer needs this so is ignored (you can write anything in here) - be good if a reader has a comment on this…
    • ‘name’ and ‘id’ - to give a name and id to the dropdown as a form element
    • ‘related_class’ - the class of the model to look up
      ‘peer_method’ - the peer method (for the above class) written to return the result set wanted in the dropdown list (see below)
    • ‘include_blank’ - to give me the blank row at the start of the dropdown that I wanted

    Step 2: In the lib/model/PropertyPeer.php file:

    class PropertyPeer extends BasePropertyPeer
    {
      public static function getRequestTypes()
      {
        $c = new Criteria();
        $c->add(PropertyPeer::PROPERTY_TYPE_ID, '1');
        $request_types = PropertyPeer::doSelect($c);
        return $request_types;
      }
    }

    So this is my query on the Property table of the DB to return the values where property_type_id=1

    Step 3: In the lib/model/Property.php file:

    class Property extends BaseProperty
    {
      public function __toString()
      {
        return $this->getPropertyName();
      }
    }

    Our old friend __toString which gives us the PropertyName rather than the Id in the dropdown.

  • 8 Comments
  • Filed under: 1.0, Symfony MVC, forms
  • Offline version of the Symfony Book

    Attention Symfony PHP addicts: For those who might want to catch up on some Symfony programming on the train, bus, plane or even for reading it on your mobile phone whilst you are on the toilet - having an offline version of the Symfony book would be useful.

    So we have done that for you. Feed your symfony addiction:

    We did not do the 1.2 version of the book as that is still work in progress.

    Enjoy from your fellow Symfony nerds.

    The Beauty of sfPropelFinder

    I was just catching up on my feeds this morning, and came across an update on another great piece of workby Francois.

    The easiest way to explain what I like about this plugin is just to look at the following example… it is in another class for ease of readability and simplicity in writing compared to the ’standard’ symfony method:

    // With Peer and Criteria
    $c = new Criteria()
    $c->add(ArticlePeer::TITLE, ‘%world’, Criteria::LIKE);
    $c->add(ArticlePeer::IS_PUBLISHED, true);
    $c->addAscendingOrderByColumn(ArticlePeer::CREATED_AT);
    $articles = ArticlePeer::doSelectJoinCategory($c);


    // with sfPropelFinder
    $articles = sfPropelFinder::from('Article')->
    where('Title', 'like', '%world')->
    where('IsPublished', true)->
    orderBy('CreatedAt')->
    with('Category')->
    find();

    Check it out here.