Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter 'message' must be a string in FileValidator #12947

Closed
be4i opened this issue Jul 7, 2017 · 14 comments
Closed

Parameter 'message' must be a string in FileValidator #12947

be4i opened this issue Jul 7, 2017 · 14 comments
Assignees
Labels
bug A bug report status: low Low

Comments

@be4i
Copy link

be4i commented Jul 7, 2017

When i try to validate file upload, and the file exceeds the 'maxSize' specified in the FileValidator i get "Parameter 'message' must be a string" exception.

Code

$form = new Form();
$file = new File('test');

$file->addValidator(new FileValidator([
	'maxSize'           => '3M',
	'allowEmpty'        => true,
	'allowedTypes'      => [
		'image/jpeg',
		'image/png'
	],
]));

$form->add($file);

if($this->request->isPost())
{
	if($form->isValid(array_merge($this->request->getPost(), $_FILES)))
	{

	}
	else
	{
		foreach($form->getMessages() as $message)
		{
			$this->flash->error($message);
		}
	}
}

$this->view->form = $form;
{{ form('', 'method': 'post', 'enctype': 'multipart/form-data') }}
    {{ form.render('test') }}
    <input type="submit" value="Go" />
{{ endForm() }}

Output

Parameter 'message' must be a string
#0 [internal function]: Phalcon\Validation\Message->__construct(false, 'test', 'FileIniSize', NULL)
#1 [internal function]: Phalcon\Validation\Validator\File->validate(Object(Phalcon\Validation), 'test')
#2 [internal function]: Phalcon\Validation->validate(Array, NULL)
#3 /home/be4i/Projects/phalcontest/app/controllers/IndexController.php(29): Phalcon\Forms\Form->isValid(Array)
#4 [internal function]: IndexController->indexAction()
#5 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#6 [internal function]: Phalcon\Dispatcher->_dispatch()
#7 [internal function]: Phalcon\Dispatcher->dispatch()
#8 /home/be4i/Projects/phalcontest/public/index.php(42): Phalcon\Mvc\Application->handle()
#9 {main}

Details

  • Phalcon version: 3.2.0
  • PHP Version: PHP 7.0.20-1~dotdeb+8.2
  • Operating System: Debian Jessie
  • Installation type: installing via package manager
  • Zephir version (if any): 0.9.8-6335775f25
  • Server: Apache2
@Jurigag
Copy link
Contributor

Jurigag commented Jul 7, 2017

Not sure what happens here. Somehow you have false returned as message. Can you check what $validation->getDefaultMessage('FileIniSize') returns?

@be4i
Copy link
Author

be4i commented Jul 7, 2017

File :field exceeds the maximum file size

@Jurigag
Copy link
Contributor

Jurigag commented Jul 7, 2017

Oh https://github.com/phalcon/cphalcon/blob/master/phalcon/validation/validator/file.zep#L114 there is no replacePairs defined, i guess when there is no replace pairs and those are not replaced there is false returned? @sergeyklay

@Jurigag
Copy link
Contributor

Jurigag commented Jul 7, 2017

I will try to fix it later.

@Zaszczyk
Copy link
Contributor

I've also occured this bug

Zaszczyk added a commit to Zaszczyk/cphalcon that referenced this issue Jul 19, 2017
@Zaszczyk Zaszczyk mentioned this issue Jul 19, 2017
3 tasks
@haginaga
Copy link

Same problem happened to me.
I am waiting to merge.

@martijnenco
Copy link

martijnenco commented Sep 14, 2017

I've the exact same problem.
I will do a work around now with a callback for as long as this bug persits.

My error messege:
`( ! ) Fatal error: Uncaught InvalidArgumentException: Parameter 'message' must be a string in
/var/source/app/dpdk/Validators/DynamicVacancyValidator.php on line 212
( ! ) InvalidArgumentException: Parameter 'message' must be a string in
/var/source/app/dpdk/Validators/DynamicVacancyValidator.php on line 212

Call Stack
Time Memory Function Location
1 0.0573 368192 {main}( ) .../index.php:0
2 1.6264 599160 dpdk\Bootstrap\BaseBootstrap->run( ) .../index.php:11
3 1.6264 599376 handle ( ) .../BaseBootstrap.php:70
4 1.6266 617888 dispatch ( ) .../BaseBootstrap.php:70`

@sergeyklay
Copy link
Contributor

sergeyklay commented Sep 14, 2017

Fixed in the 3.2.x branch. Feel free to open a new issue if the problem appears again. Thank you for contributing.

@deadboy2
Copy link

deadboy2 commented Oct 5, 2017

Не работает. Проблема актуальна

Warning: strtr(): The second argument is not an array in C:\OSPanel\domains\blog.test\app\controllers\DashboardController.php on line 209
Parameter 'message' must be a string

Т.е ошибка здесь if (!$avatar->isValid($_FILES)) Что делать?

Помимо файла передаются еще текстовые данные из формы.

Валидатор:

<?php

use Phalcon\Forms\Element\File;
use Phalcon\Forms\Form;
use Phalcon\Validation\Validator\File as FileValidator;

class SettingsAvatarForm extends Form
{
    private $avatar;

    public function initialize()
    {
        $this->avatar = new File(
            'avatar',
            [
                'class' => 'form-control',
                'id' => 'inputAvatar',
            ]
        );

        $this->avatar->addValidator(
            new FileValidator(
                [
                    "maxSize"              => "1M",
                    "messageSize"          => ":field exceeds the max filesize (:max)",
                    "allowedTypes"         => [
                        "image/jpeg",
                        "image/png",
                    ],
                    "messageType"          => "Allowed file types are :types",
                    "maxResolution"        => "512x512",
                    "messageMaxResolution" => "Max resolution of :field is :max",
                ]
            )
        );

        $this->add($this->avatar);
    }
}

Контроллер:

$avatar = new SettingsAvatarForm();

        if ($this->request->hasFiles()) {
            
                if (!$avatar->isValid($_FILES)) {
                    $messagesAvatar = $avatar->getMessagesFor('avatar');

                    $avatarMsg = '';

                    foreach ($messagesAvatar as $message) {
                        $avatarMsg .= "<span class='errorMsg'>" . $message . "</span>" . "<br>";
                    }

                    $this->view->setVar('errorAvatar', $avatarMsg);
                }
}

@martijnenco
Copy link

What version do you run? It has been fixxed 21 days ago..
Also I have made a work around. Currently I check the size manually (since we run an older version).
Also, isnt there a policy about communicating in english on this forum?

@deadboy2
Copy link

deadboy2 commented Oct 5, 2017

Sorry.

Phalcon version: 3.2.2
PHP Version: PHP 7.1
Operating System: Win10
Server: OpenServer

@deadboy2
Copy link

deadboy2 commented Oct 5, 2017

I can't go on while the problem is actually. I hope you'll decide it as soon as possible.

@Zaszczyk
Copy link
Contributor

Zaszczyk commented Oct 5, 2017

@deadboy2 just use try..catch:

try {
    $messages = $validator->validate($fileTemp);
} catch (\InvalidArgumentException $e) {
    //send message
}

@deadboy2
Copy link

deadboy2 commented Oct 5, 2017

Its WORK! Thank you!

@niden niden added bug A bug report status: low Low and removed Bug - Low labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: low Low
Projects
None yet
Development

No branches or pull requests

8 participants