无法显示验证消息

时间:2014-04-23 15:05:37

标签: validation cakephp file-upload

我是新手。我在其他帖子上尝试了很多解决方案,但我无法做到正确。我的验证中的消息根本没有显示,但是如果大小验证没有通过,则文件输入字段的值变为null,但似乎扩展valdiation也不起作用。为什么没有消息显示且扩展验证不起作用? 我正在使用CakePHP 2.4.4。

控制器

    public function admin_upload_image(){
        $this->set('title_for_layout', 'Inserir Fotografias');
        if(!$this->Session->check('User')) {
            $this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
            $this->redirect(array(
                            'controller' => 'users',
                            'action' => 'login'));
        }

        $this->layout = 'admin_index';
        if($this->request->is('post') || $this->request->is('put')) {
          /*  $file = $this->request->data['gallery_images']['path']['name'];*/
        $file = array(
                'GalleryImage' => array(
                'path' => $this->request->data['gallery_images']['path']['name']
                                        )
                );
            move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);

            $this->loadModel('GalleryImage');

            $this->GalleryImage->create();
           //debug($file);
           //die;
            if($this->GalleryImage->save($file)){
                $validationErrors = $this->GalleryImage->invalidFields();
                $this->Session->setFlash($validationErrors['path']); // named key of the rule
                $this->Session->setFlash(__('Evento guardado com sucesso.'));
            }
            //else{

                //$error = $this->Notification->validationErrors;
                //$this->set('error', $error);

                //$this->Session->setFlash(__($error), 'Flash/warning');
            //}
        }
    }

查看

<h2>Adicionar Fotografia</h2>
<?php
echo "<br>";
echo $this->Form->create('GalleryImage',array('type'=>'file'));
echo $this->Form->file('gallery_images.path');
echo "<br>";
echo $this->Form->submit(__('Guardar'), array('class' => 'btn btn-success','formnovalidate' => false)) ;
echo $this->Form->end();
/*if ($this->Form->isFieldError('path')) {
    echo $this->Form->error('path');
}*/
?>

模型

<?php
App::uses('AppModel', 'Model');
  class GalleryImage extends AppModel{
    public $displayField ='path';
    public $useTable = 'gallery_images';
    //public $actsAs = array('MultipleDisplayFields' => array('fields' => array('path', 'id')));
    var $name = 'GalleryImage';
    var $validate= array(
        'path' => array(
            'is_valid' => array(
                'rule' => 'notEmpty',
                'message' => 'Seleccione uma fotografia por favor.',
                'last' => true),
            'size' => array(
                'rule' => array('fileSize','<=','1.5MB'),
                'message' => 'O ficheiro deve ter um tamanho igual ou inferior a 1.5MB.',
                'last' => true),
            'extension' => array(
                'rule' => array('extension', array('gif','jpeg','png','jpg')),
                'message'=> 'A imagem deve estar num formato gif, jpeg, png ou jpg.',
                'last' => true)
            )
        );
  }
?>

调试($文件)

\app\Controller\GalleriesController.php (line 58)
array(
'GalleryImage' => array(
    'path' => '1604710_722861904399871_963210258_n.jpg'
)
)

PR($这 - &GT; GalleryImage-&GT; invalidFields());

Notice (8): Undefined index: gallery_images [APP\Controller\GalleriesController.php, line 50]
Notice (8): Undefined index: gallery_images [APP\Controller\GalleriesController.php, line 53]
Notice (8): Undefined index: gallery_images [APP\Controller\GalleriesController.php, line 53]
Array
(
[path] => Array
    (
        [0] => Seleccione uma fotografia por favor.
        [1] => Seleccione uma fotografia por favor.
    )

)

1 个答案:

答案 0 :(得分:2)

使用表格 - >输入

&#34;积木&#34; checkboxradioselectfile等函数只是输入。生成表单的常规方法是使用input(或inputs)方法:

echo $this->Form->input('path', array('type' => 'file'));

或明确呈现错误

或者,您可以显式呈现验证错误:

echo $this->Form->file('path');
echo $this->Form->error('path');