Error in documentation - symfony forms in action [message #82859] |
Thu, 30 July 2009 02:50  |
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 #82894 is a reply to message #82887 ] |
Thu, 30 July 2009 14:10   |
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 #83001 is a reply to message #82859 ] |
Sat, 01 August 2009 02:34  |
|
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*

|
|
|