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

Comments

Projects
None yet
7 participants
@be4i

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

This comment has been minimized.

Show comment
Hide comment
@Jurigag

Jurigag Jul 7, 2017

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@be4i

be4i Jul 7, 2017

File :field exceeds the maximum file size

be4i commented Jul 7, 2017

File :field exceeds the maximum file size

@Jurigag

This comment has been minimized.

Show comment
Hide comment
@Jurigag

Jurigag Jul 7, 2017

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@Jurigag

Jurigag Jul 7, 2017

Member

I will try to fix it later.

Member

Jurigag commented Jul 7, 2017

I will try to fix it later.

@Zaszczyk

This comment has been minimized.

Show comment
Hide comment
@Zaszczyk

Zaszczyk Jul 19, 2017

Contributor

I've also occured this bug

Contributor

Zaszczyk commented Jul 19, 2017

I've also occured this bug

Zaszczyk added a commit to Zaszczyk/cphalcon that referenced this issue Jul 19, 2017

@Zaszczyk Zaszczyk referenced this issue Jul 19, 2017

Merged

Fix #12947 #12973

2 of 3 tasks complete
@haginaga

This comment has been minimized.

Show comment
Hide comment
@haginaga

haginaga Aug 27, 2017

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

haginaga commented Aug 27, 2017

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

@martijnenco

This comment has been minimized.

Show comment
Hide comment
@martijnenco

martijnenco 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`

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 added a commit that referenced this issue Sep 14, 2017

@sergeyklay

This comment has been minimized.

Show comment
Hide comment
@sergeyklay

sergeyklay Sep 14, 2017

Member

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

Member

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.

@sergeyklay sergeyklay closed this Sep 14, 2017

@deadboy2

This comment has been minimized.

Show comment
Hide comment
@deadboy2

deadboy2 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);
                }
}

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

This comment has been minimized.

Show comment
Hide comment
@martijnenco

martijnenco Oct 5, 2017

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?

martijnenco commented Oct 5, 2017

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

This comment has been minimized.

Show comment
Hide comment
@deadboy2

deadboy2 Oct 5, 2017

Sorry.

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

deadboy2 commented Oct 5, 2017

Sorry.

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

@deadboy2

This comment has been minimized.

Show comment
Hide comment
@deadboy2

deadboy2 Oct 5, 2017

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

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

This comment has been minimized.

Show comment
Hide comment
@Zaszczyk

Zaszczyk Oct 5, 2017

Contributor

@deadboy2 just use try..catch:

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

Zaszczyk commented Oct 5, 2017

@deadboy2 just use try..catch:

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

This comment has been minimized.

Show comment
Hide comment
@deadboy2

deadboy2 Oct 5, 2017

Its WORK! Thank you!

deadboy2 commented Oct 5, 2017

Its WORK! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment