0

I want use Callback validation but continue_if_empty not working for File, it’s returning No file was uploaded error. But everything working for Input and Select. I need help with this.

$this->add([
    'name' => 'images',
    'required' => false,
    'allow_empty' => true,
    'continue_if_empty' => true,
    'type' => FileInput::class,
    'filters' => [
        [
            'name' => Rename::class,
            'options' => [
                'target' => '/var/www/project/public/website/content/tmp/image_' . md5(time()),
                'randomize' => true,
            ],
        ],
    ],
    'validators' => [
        [
            'name' => Callback::class,
            'options' => [
                'callback' => [$this, 'imageValidator'],
            ],
        ],
     ],
]);


public function imageValidator($value, $context)
{
     dd($context);
}
karen
  • 105
  • 1
  • 1
  • 4

1 Answers1

0

This helped me to solve "No file was uploaded" error.

Include this line in your view template (.phtml) before you open form tag:

$form->prepare()

Full Example:

<!-- File: upload-form.phtml -->
<?php
    $form->prepare(); // The correct enctype is set here
    $fileElement = $form->get('image-file');
    echo $this->form()->openTag($form);
?>

    <div class="form-element">
        <?= $this->formLabel($fileElement) ?>
        <?= $this->formFile($fileElement) ?>
        <?= $this->formElementErrors($fileElement) ?>
    </div>

    <button>Submit</button>

<?= $this->form()->closeTag() ?>

Source: https://docs.laminas.dev/laminas-form/file-upload/