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

[BUG]: messageFileEmpty not showing #14928

Closed
assadnazar opened this issue Mar 25, 2020 · 4 comments · Fixed by #15769
Closed

[BUG]: messageFileEmpty not showing #14928

assadnazar opened this issue Mar 25, 2020 · 4 comments · Fixed by #15769
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium

Comments

@assadnazar
Copy link

File Validation Empty custom message is not being shown.

$facilityimage = new File('facilityimage');

$facilityimage->addValidators([
    //new FileValidator()
    new FileValidator([
        'maxSize' => '0.5M',
        'messageSize' => 'Your image is too big. Max file size: 500KB',
        'allowedTypes' => array('image/jpeg', 'image/png'),
        'messageType' => 'Your image must be a JPEG or PNG file',
        'allowEmpty' => false,
        'messageFileEmpty' => 'No image uploaded.'
    ])
]);

$this->add($facilityimage);

Only default message is being shown."Field facilityimage must not be empty"

@assadnazar assadnazar added bug A bug report status: unverified Unverified labels Mar 25, 2020
@Jeckerson Jeckerson added the 5.0 The issues we want to solve in the 5.0 release label Mar 30, 2021
@Izopi4a
Copy link
Member

Izopi4a commented May 3, 2021

Hi,

I think you are not passing the correct params to the validate method.

working example:

form

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


class FormOne extends Form
{
    /**
     * @param null $entity
     * @param array $options
     */
    public function initialize($entity = null, array $options = [])
    {

        $name = new Text('name', [
            'placeholder' => 'Name',
            'class'=> 'form-control'
        ]);

        $name->addValidators([
            new PresenceOf([
                'message' => 'The name is required',
            ]),
        ]);

        $this->add($name);

        $facilityimage = new File('image1');


        $facilityimage->addValidators([
            //new FileValidator()
            new FileValidator([
                'maxSize' => '0.5M',
                'messageSize' => 'Your image is too big. Max file size: 500KB',
                'allowedTypes' => array('image/jpeg', 'image/png'),
                'messageType' => 'Your image must be a JPEG or PNG file',
                'allowEmpty' => false,
                'messageFileEmpty' => 'No image uploaded.'
            ])
        ]);

        $this->add($facilityimage);

    }
}

controller

public function test14928Action() {

        $this->view->setTemplateBefore('public');

        $form = new FormOne();

        $this->view->setVar("form", $form);

        if ($this->request->isPost()) {

            echo "<pre>";
            var_dump($form->isValid(array_merge($_POST, $_FILES))); // <----
            echo "</pre>";
        }
    }

view

{% set form = this.view.getVar("form") %}


<form id="form" action="/index/test14928" method="post" enctype="multipart/form-data">
    <div class="form-group">
        {{ form.render("name") }}
    </div>
    <div class="form-group">
        {{ form.render("image1") }}
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

@Izopi4a Izopi4a added the need script to reproduce Script is required to reproduce the issue label May 3, 2021
@StudioMaX
Copy link
Contributor

StudioMaX commented May 17, 2021

In version 3.4.x Phalcon\Validation\Validator\File was the standard validator, which also has the isAllowEmpty method. If this method was present, it was called during Validation::preChecking before executing the file validator itself.

let allowEmpty = validator->getOption("allowEmpty", false);
if allowEmpty {
if method_exists(validator, "isAllowEmpty") {
return validator->isAllowEmpty(this, field);
}

In 4.x Phalcon\Validation\Validator\File was changed to extend AbstractValidatorComposite. Both of them doesn't contain isAllowEmpty, but is still exists in Phalcon\Validation\Validator\File\AbstractFile. But Validation::preChecking doesn't see this method, since it know nothing about AbstractFile. AbstractFile is needed only for submodules (File\Min, File\Max, etc.), but not for Phalcon\Validation\Validator\File. So isAllowEmpty isn't executed at all.

@BeMySlaveDarlin
Copy link
Contributor

BeMySlaveDarlin commented Nov 7, 2021

@StudioMaX, your assumption wrong in case of isAllowEmpty isn't executed.

It is executed, but the message isn't reassigned due to that changes you mentioned.
Due to Phalcon\Validation\Validator\File is composite, it builds each of submodules during initialization, but legacy messages isn't passed by, so it throws default message.

Did a fix in #15769

BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 7, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 7, 2021
@BeMySlaveDarlin BeMySlaveDarlin linked a pull request Nov 7, 2021 that will close this issue
5 tasks
@niden niden added status: medium Medium and removed need script to reproduce Script is required to reproduce the issue status: unverified Unverified labels Nov 7, 2021
@niden
Copy link
Member

niden commented Nov 7, 2021

Resolved in #15769

@niden niden closed this as completed Nov 7, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 7, 2021
@niden niden moved this to Released in Phalcon v5 Aug 25, 2022
@niden niden added this to Phalcon v5 Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants