Tag: tips


Setting a value to a hidden form symfony field

July 16th, 2009 — 10:23am

The case

Sometimes, when creating a new object, we need to give a default value to a specific field but we don’t want this field to be visible in the form.
A tipical example would it be a blog -> post relationship. Where we want the blog_id field to be set automatically but we want it to be hidden.

Blog->Post

Blog->Post

The mistake

When creating a new post for a blog, we do not want the blog_id field to be visible, so we typically go to PostForm.class.php and unset the field blog_id:

//myproject/lib/form/PostForm.class.form
class PostForm extends BasePostForm
{
  public function configure()
  {
    unset( $this['blog_id'] );
  }
}

Then, we edit the action class and set a default value for the blog_id field:

// mypoject/modules/post/actions/actions.class.php
class postActions extends sfActions
{
  public function executeNew(sfWebRequest $request)
  {  
    // create the form
    $form = new PostForm();
 
    // get blog_id parameter from the request
    // and set the default value
    $form->setDefault("blog_id", $request->getParameter('blog_id') );
 
    $this->form = $form;
  }
  //... rest of actions here ...
}

Finally, when we try to create a new post, we realize that the blog_id has not been set correctly.

The solution

Instead of unsetting the blog_id field, we have to change its widget to a sfWidgetFormInputHidden:

//PostForm.class.form
class PostForm extends BasePostForm
{
  public function configure()
  {
    // the wrong way in this case
    // unset( $this['blog_id'] );
 
    // the right way in this case
    $this->widgetSchema['blog_id'] = new sfWidgetFormInputHidden();
  }
}

This way the field blog_id remains hidden but do exists in the form, so the setDefault method works correctly.

8 comments » | Symfony

TODO: Change some windows habits

June 17th, 2009 — 12:00am

I have been studing symfony for the last two months and I have discovered a new world in web developing.

Armed with all my new skills, I am now migrating the web site www.ajedrezenmadrid.com from msaccess/asp to mysql/php.

As a senior windows developer I feel comfortable with my windows system, so I installed wampserver package as an easy way to get apache, mysql and php running in my windows xp system.

After the normal newbie counter-times, that I will explain in fore coming articles, and several weeks of hard work, I had a beta version of the backend running in my computer. It was time to deploy my work.

I used the straight-forward method of uploading all my project by hand via ftp, and every seemed to go well… except for one page. The web server was reporting an “unable to find the file …” exception, so I checked that the file existed, but, what to do next?

The problem was not so obvious for a long time windows developer. I had changed the case of a letter of one filename in an include php instruction. That was all, but it took me half an hour to find the problem, so today’s article is just a reminder for those of us that have always developed under Microsoft Windows systems:

Beware of the case-sensitive filenames!

Nowadays, most web servers are linux systems, which are case-sensitive, so remember to be very careful when writing filenames or you will encounter yourself following a “ghost-bug” for a while.

Comment » | tips

Back to top