Bonjour à tous,

J'ai voulu installé un système d'avatar pour mes membres, un champs de type input file lorsque les utilisateurs éditent leur profil . Ils pourront ajouté un avatar .
J'ai donc voulu utilisé le formidable plugin de GraphikArt : https://github.com/Grafikart/CakePHP-Upload
Le seul hic, c'est que rien ne se passe lorsque je clique sur "EDIT", pas d'insertion dans la base de donnée dans le champs avatar, ni d'erreur avec le DEBUG . (ni d'upload d'image)

Dans ma vue : edit.ctp (nom du champs avatar_file)

<?php echo $this->Form->create('User',array('class'=>'form-horizontal','inputDefaults'=>array('label'=>false)));?>
                          <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Username</label>
                            <div class="col-sm-10">
                              <?php echo $this->Form->input('username',array('class'=>'form-control'));?>
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
                            <div class="col-sm-10">
                               <?php echo $this->Form->input('password',array('class'=>'form-control'));?>
                            </div>
                          </div>

                          <?php echo $this->Form->input('avatar_file', array('type' => 'file')); ?>

                          <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                              <?php echo $this->Form->submit('EDIT',array('class'=>'btn btn-primary'))?>
                            </div>
                          </div>
                    <?php echo $this->Form->end();?>

UsersController :

<?php
App::uses('Controller', 'AppController');
class UsersController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();

        // Non connecté
        $this->Auth->allow('signup');
    }

    public function profile($id=null) {     
    }

    public function signup () {
        if($this->request->is('post') || $this->request->is('put') ){
            $d = $this->request->data'User'];
                if(empty($d'password']))
                    unset($d'password']);
                if($this->User->save($d)){
                    $this->Session->setFlash(__('OK'));
                }           
        }
    }

    public function edit(){
    $user_id = $this->Auth->user('id');
    if(!$user_id) {
        $this->redirect('/');
        die();
    }
    $this->User->id = $user_id;
    if($this->request->is('put') || $this->request->is('post')) {
        $d = $this->request->data;
        //debug($d) or die(); 
        $d'User']'id'] = $user_id;
        if($this->User->save($d)) {
            $this->Session->write('Auth.User.username', $this->request->data'User']'username']);
            $this->Session->setFlash("Votre profil a bien été édité.");
        }
    }
    else {
         $this->request->data = $this->User->read(array('username'), null);
       }
    }

    public function login() {
        if($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}

User (Model) :

<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
    public $validate = array(
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'password_confirm' => array(
        'compare' => array(
            'rule' => array('validate_passwords'),
            'message' => 'Please confirm the password',
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'avatar_file' => array(
            'avatar_file' => array(
                'rule' => array('fileExtension', array('jpg','png')),
                'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        )
    );
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    public $actsAs = array(
    'Upload.Upload' => array(
        'fields' => array(
            'avatar' => 'img/avatars/:id1000/:id'
            )
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Topic' => array(
                    'className' => 'Topic',
                    'foreignKey' => 'user_id',
                    'dependent' => false,
                    'conditions' => '',
                    'fields' => '',
                    'order' => '',
                    'limit' => '',
                    'offset' => '',
                    'exclusive' => '',
                    'finderQuery' => '',
                    'counterQuery' => ''
        )
    );

        public function beforeSave($options = array()){
        // if we have a password, we hash it before saving
        if(!empty($this->data'User']'password'])){
            $this->data'User']'password'] = AuthComponent::password($this->data'User']'password']);
        }
        return true; 
    }

        public function validate_passwords() { //password match check
        return $this->data$this->alias]'password'] === $this->data$this->alias]'password_confirm'];
        }

}

Le plugin à bien le nom Upload et est dans app/Plugin/Upload

Dans bootstrap.php

CakePlugin::load('Upload');

J'ai rien oublié normalement ... :/

Merci de votre aide, je suis dans le "flou" ^^

1 réponse


Hello,

La réponse se trouve ici : http://book.cakephp.org/2.0/fr/core-libraries/helpers/form.html#options-pour-create

echo $this->Form->create('User', array('type' => 'file'));