Error in documentation - symfony forms in action

About symfony tutorials, symfony book and API documentation.

Moderators: dwhittle, Ian

Error in documentation - symfony forms in action

Postby jonhfx » Thu Jul 30, 2009 2:50 am

This caused quite a bit of confusion.

In the following documentation:

http://www.symfony-project.org/forms/1_2/en/11-Doctrine-Inte gration

Under the section on file uploads:

Listing 4-25 - Modifying the file Field of the ArticleForm form.

Code: Select all
class ArticleForm extends BaseArticleForm
{
  public function configure()
  {
    // ...
 
    $this->widgetSchema['file'] = new sfWidgetFormInputFile();
    $this->validatorSchema['file'] = new sfValidatorFile();
  }
}


This yields a type error, every time. The only way to get sfValidatorFile to work is to set the path option.

Is this not an error?
Jonathan Montgomery
http://jonocode.wordpress.com
jonhfx
Member
 
Posts: 37
Joined: Fri Jan 18, 2008 3:25 am

Re: Error in documentation - symfony forms in action

Postby Russ » Thu Jul 30, 2009 12:33 pm

Can you be more specific about the error? If it's a type error then it's expecting a string right? Or are you saving the file in the database as a binary (blob) object?

If the path is a problem then the validator should throw an exception before it even gets as far as the Doctrine lower level validators.
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: Error in documentation - symfony forms in action

Postby jonhfx » Thu Jul 30, 2009 2:10 pm

Sorry, Russ. I should have posted the exact error message. Right now I don't have access to it, but it is the same validation error that you get when the field in your Doctrine schema is set to varchar instead of string.

I am saving the file to the file system, and the full path of the file to a string table field.

It is something like "1 validation error: field_name (type)".

However, when you declare your file validator with the path option set, this validation error disappears and everything works.

Code: Select all
$this->validatorSchema['file'] = new sfValidatorFile(array('path' => sfConfig::get('sf_upload_dir')));


I found this solution while google searching, and someone said the path must be declared. I'm sure if I check the documentation for sfValidatorFile, the path option must be required. Of course, the "type" validation error does not make this clear. And the code in the documentation does not run with Symfony 1.2.7.

So I'm wondering if this document should be updated?

Thank you for responding, Russ.
Jonathan Montgomery
http://jonocode.wordpress.com
jonhfx
Member
 
Posts: 37
Joined: Fri Jan 18, 2008 3:25 am

Re: Error in documentation - symfony forms in action

Postby Russ » Thu Jul 30, 2009 3:27 pm

That's the part I don't understand, because the validator works like this:

Code: Select all
* If you don't pass a file name, it will be generated by the generateFilename method.
   * This will only work if you have passed a path when initializing this instance.


Either way this should be caught and handled here in the validator, and not allowed to slip through and fail at the database level.

It would be interesting to see what value is actually getting passed and causing it to fail. All I can see in the class is that the name that is returned if $path is null, is the name of the file (which should be correct as the file name contains the path in cases where $path is not set). If there is a validation error at the db level, then it means there is something strange happening, like an array or integer or something getting sent to Doctrine instead of a string.
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: Error in documentation - symfony forms in action

Postby Russ » Thu Jul 30, 2009 4:08 pm

Are you using a custom file class? The file object that the sfFileValidator returns is sfValidatedFile.

This class has a __toString() method as follows:

Code: Select all
  public function __toString()
  {
    return is_null($this->savedName) ? '' : $this->savedName;
  }


As I understand it, this value is what should ultimately end up in your field.

Oh hang on... I think I see the problem...

That value is only set after the file is saved. Are you trying to save the form and then save the file?
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: Error in documentation - symfony forms in action

Postby jonhfx » Thu Jul 30, 2009 4:36 pm

Just following the code as-is in the documentation:

http://www.symfony-project.org/forms/1_2/en/11-Doctrine-Inte gration

(under the file uploads section at the very bottom)

It appears to save the file name field automatically. But I don't think I've done anything differently than what is in the documentation.

It simply doesn't work unless I set the path in the validator declaration, and the documentation does not have this step.
Jonathan Montgomery
http://jonocode.wordpress.com
jonhfx
Member
 
Posts: 37
Joined: Fri Jan 18, 2008 3:25 am

Re: Error in documentation - symfony forms in action

Postby Russ » Thu Jul 30, 2009 9:44 pm

I've opened a ticket to remind myself to look through it. Need to figure out if it's a doc bug or a code problem.

http://trac.symfony-project.org/ticket/6936
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: Error in documentation - symfony forms in action

Postby jonhfx » Fri Jul 31, 2009 4:08 am

Russ, you are endlessly helpful.
Jonathan Montgomery
http://jonocode.wordpress.com
jonhfx
Member
 
Posts: 37
Joined: Fri Jan 18, 2008 3:25 am

Re: Error in documentation - symfony forms in action

Postby Russ » Fri Jul 31, 2009 1:08 pm

Haha, I just want the documentation to be correct :)~ :-)~ :smile:
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: Error in documentation - symfony forms in action

Postby Russ » Sat Aug 01, 2009 2:34 am

Can... open... worms... everywhere...

Well, I'm going through opening tickets and fixing some basic things as I go along. There's a lot of stuff to clean up on that page!

To explain your particular problem, which is most certainly a bug, this code:

Code: Select all
// we need the base directory
if (!$this->validatorSchema[$field]->getOption('path'))
{
  return $values[$field];
}


Is the culprit. Because the file validator actually returns an object (sfValidatedFile) from it's doClean() method, that is what is contained in $values[$field]. From the code above, you can see that if the path is not set, this object is returned. This object is what gets passed through the chain to be saved in the database, and since object != string you get the type validation error.

If a path is set, the code continues and eventually the value of $file->save() is returned. Since this method (assuming the user is using the sfValidatedFile default and hasn't specified their own) returns the filename of the saved file, the type validator is happy when it comes to save it in the database as a string.

So there are multiple problems here - another thing interestingly is that Doctrine is automatically calling the $file->save() function when the form is submitted, so there is no need for the duplicate call in Listing 4-27, essentially the file is being saved twice! Well, it's being saved, then unlinked, then saved again...

So, this page needs seriously updating... sheesh.
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway


Return to Documentation

Who is online

Users browsing this forum: No registered users and 1 guest