【问题标题】:Can´t show validation messages无法显示验证消息
【发布时间】:2014-04-23 23:44:34
【问题描述】:

我是新手。我在其他帖子上尝试了很多解决方案,但我就是做错了。我的验证消息根本不显示,但是如果大小验证未通过,文件输入字段的值将变为 null,但扩展验证似乎无法正常工作。为什么没有消息显示并且扩展验证不起作用? 我正在使用 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)
            )
        );
  }
?>

调试($file)

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

pr($this->GalleryImage->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.
    )

)

【问题讨论】:

  • 可以在控制器中使用if ($this-&gt;GalleryImage-&gt;save($file)) /*your code*/ else {pr($this-&gt;ModelName-&gt;invalidFields());}进行调试吗?检查您是否没有从模型中获取错误,或者您的控制器是否正确接收了它们但视图没有显示它们。使用pr() 的结果更新您的问题(或者如果您没有得到错误数组,则缺少它)。
  • @Nunser 我更新了我的问题。

标签: validation cakephp file-upload


【解决方案1】:

使用表单->输入

checkboxradioselectfile 等“构建块”函数只是输入。生成表单的常规方法是使用input(或inputs)方法:

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

或显式渲染错误

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

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

【讨论】:

  • 我使用了echo $this-&gt;Form-&gt;error('path');,它显示了一个错误,但它是错误的。当我尝试上传超过 1.5MB 的文件时,我收到 is_valid 规则消息,而不是 size 规则消息
  • 我用了12MB的图片来测试,我的php上传限制设置为20MB。我需要知道的是为什么消息没有显示,以及为什么我使用您发布的代码得到错误的错误。
  • 所以你是说这是由于上传的文件大小限制造成的?
  • 好的,我会将您的问题标记为解决方案。谢谢。
猜你喜欢
  • 2018-08-09
  • 2018-12-16
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多