This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » development » Documentation » Error in documentation - symfony forms in action
Error in documentation - symfony forms in action [message #82859] Thu, 30 July 2009 02:50 Go to next message
jonhfx  is currently offline jonhfx
Messages: 37
Registered: January 2008
Member

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.

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
Re: Error in documentation - symfony forms in action [message #82887 is a reply to message #82859 ] Thu, 30 July 2009 12:33 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

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*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Error in documentation - symfony forms in action [message #82894 is a reply to message #82887 ] Thu, 30 July 2009 14:10 Go to previous messageGo to next message
jonhfx  is currently offline jonhfx
Messages: 37
Registered: January 2008
Member
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.

$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
Re: Error in documentation - symfony forms in action [message #82898 is a reply to message #82859 ] Thu, 30 July 2009 15:27 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

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

* 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*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Error in documentation - symfony forms in action [message #82903 is a reply to message #82859 ] Thu, 30 July 2009 16:08 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

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

This class has a __toString() method as follows:

  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*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
icon14.gif  Re: Error in documentation - symfony forms in action [message #82905 is a reply to message #82903 ] Thu, 30 July 2009 16:36 Go to previous messageGo to next message
jonhfx  is currently offline jonhfx
Messages: 37
Registered: January 2008
Member
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
Re: Error in documentation - symfony forms in action [message #82930 is a reply to message #82905 ] Thu, 30 July 2009 21:44 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

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*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Error in documentation - symfony forms in action [message #82942 is a reply to message #82930 ] Fri, 31 July 2009 04:08 Go to previous messageGo to next message
jonhfx  is currently offline jonhfx
Messages: 37
Registered: January 2008
Member
Russ, you are endlessly helpful.


Jonathan Montgomery
http://jonocode.wordpress.com
Re: Error in documentation - symfony forms in action [message #82963 is a reply to message #82942 ] Fri, 31 July 2009 13:08 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Haha, I just want the documentation to be correct Smile


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Error in documentation - symfony forms in action [message #83001 is a reply to message #82859 ] Sat, 01 August 2009 02:34 Go to previous message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

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:

// 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*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Previous Topic:edit text in generator.yml
Next Topic:The symfony 1.2 architecture
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software