Bonjour à tous,
J'utilise le plugin sfForkedDoctrineGuard pour compléter sfGuard et avoir ainsi le profil des utilisateurs.
Cependant, lorsque j'essaye la partie edition du profil en mmodifiant l'avatar, le nom du fichier est bien transmit à la base de donnée mais le fichier n'est pas sauvegardé dans le dossier correspondant.
Je vous donne ci-dessous mon code pour configurer le formulaire d'édition:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 <?php class sfApplySettingsForm extends sfGuardUserProfileForm { public function configure() { parent::configure(); $this->removeFields(); $this->widgetSchema['avatar'] = new sfWidgetFormInputFileEditable(array( 'label' => 'Avatar ' .$this->getObject()->getFirstName().'', 'file_src' => '/bib_uploads/avatars/'.$this->getObject()->getAvatar(), 'is_image' => true, 'edit_mode' => !$this->isNew(), 'delete_label' => 'Supprimer l\'image', 'template' => '%file%<br /> <span class="avatar">%input%</span> <br />%delete_label% %delete%', ), array( 'width'=>'70' )); $this->validatorSchema['avatar_delete'] = new sfValidatorPass(); // We're editing the user who is logged in. It is not appropriate // for the user to get to pick somebody else's userid, or change // the validate field which is part of how their account is // verified by email. Also, users cannot change their email // addresses as they are our only verified connection to the user. $this->widgetSchema->setLabels( array( 'firstname' => 'First Name', 'lastname' => 'Last name') ); $this->widgetSchema->moveField('firstname', sfWidgetFormSchema::FIRST); $this->widgetSchema->moveField('lastname', sfWidgetFormSchema::AFTER, 'firstname'); $this->widgetSchema->setNameFormat('sfApplySettings[%s]'); $this->widgetSchema->setFormFormatterName('list'); $this->setValidator( 'firstname' , new sfValidatorApplyFirstname() ); $this->setValidator( 'lastname', new sfValidatorApplyLastname() ); } protected function removeFields() { unset( $this['user_id'], $this['validate'], $this['email'], $this['email_new'], $this['validate_at'], $this['id'], $this['created_at'], $this['updated_at'] ); } }
Puis la partie du code du fichier sfApplyActionsLibrary.class.php correspondant:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public function executeSettings(sfRequest $request) { // sfApplySettingsForm inherits from sfApplyApplyForm, which // inherits from sfGuardUserProfile. That minimizes the amount // of duplication of effort. If you want, you can use a different // form class. I suggest inheriting from sfApplySettingsForm and // making further changes after calling parent::configure() from // your own configure() method. $profile = $this->getUser()->getProfile(); // we're getting default or customized settingsForm for the task if( !( ($this->form = $this->newForm( 'settingsForm', $profile) ) instanceof sfGuardUserProfileForm) ) { // if the form isn't instance of sfApplySettingsForm, we don't accept it throw new InvalidArgumentException( sfContext::getInstance()-> getI18N()-> __( 'The custom %action% form should be instance of %form%', array( '%action%' => 'settings', '%form%' => 'sfApplySettingsForm' ), 'sfForkedApply' ) ); } if ($request->isMethod('post')) { $formValues = $request->getParameter( $this->form->getName() ); $formFiles = $request->getFiles( $this->form->getName() ); //binding request form parameters with form $this->form->bind($formValues, $formFiles); if ($this->form->isValid()) { $this->form->save(); return $this->redirect('@homepage'); } } }
J'ai essayé de modifier ce code en faisant:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 /.. if ($this->form->isValid()) { $file = $this->form->getValue('avatar'); $filename = .sha1($file->getOriginalName()); $extension = $file->getExtension($file->getOriginalExtension()); $file->save(sfConfig::get('sf_upload_dir').'/avatars/'.$filename.$extension); $this->form->save(); return $this->redirect('@homepage'); } ../
Mais la j'ai une erreur:Je ne vois pas trop pourquoi puisque mon bind est bien définit pour récupérer aussi les fichiers...Call to a member function getOriginalName() on a non-object
Merci de votre aide.
P.S: Je tiens à préciser que lors de la création du compte l'envoi du fichier se fait bien sans problème.
exctrait sfApplyApplyForm
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 class sfApplyApplyForm extends sfGuardUserProfileForm { private $userId; public function configure() { parent::configure(); $this->removeFields(); /.. ../ $this->widgetSchema['avatar'] = new sfWidgetFormInputFile(array( 'label' => 'Avatar', )); /.. ../ $this->validatorSchema['avatar'] = new sfValidatorFile(array( 'required' => false, 'path' => sfConfig::get('sf_upload_dir').'/avatars', 'mime_types' => 'web_images', )); if( $postValidator ) { $postValidators[] = $postValidator; } $this->validatorSchema->setPostValidator( new sfValidatorAnd($postValidators) ); } public function doSave($con = null) { $user = new sfGuardUser(); $user->setUsername($this->getValue('username')); $user->setPassword($this->getValue('password')); // They must confirm their account first $user->setIsActive(false); $user->save(); $this->userId = $user->getId(); return parent::doSave($con); } public function updateObject($values = null) { $object = parent::updateObject($values); $object->setUserId($this->userId); // Don't break subclasses! return $object; } protected function removeFields() { // We're making a new user or editing the user who is // logged in. In neither case is it appropriate for // the user to get to pick an existing userid. The user // also doesn't get to modify the validate field which // is part of how their account is verified by email. unset($this['user_id'], $this['validate'], $this['validate_at'], $this['created_at'], $this['updated_at'], $this['email_new']); } }
et de l'action correspondante dans sfApplyActionsLibrary.class.php:
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public function executeApply(sfRequest $request) { /.. ../ if( $request->isMethod('post') ) { //gathering form request in one array $formValues = $request->getParameter( $this->form->getName() ); $formFiles = $request->getFiles( $this->form->getName() ); //binding request form parameters with form $this->form->bind($formValues, $formFiles); if ($this->form->isValid()) { $file = $this->form->getValue('avatar'); $guid = "n" . self::createGuid(); $this->form->getObject()->setValidate( $guid ); $date = new DateTime(); $this->form->getObject()->setValidateAt( $date->format( 'Y-m-d H:i:s' ) ); $this->form->save(); /.. ../ }
Partager