[英]No file was uploaded error Zend framework 3 validation
我想使用Callback
驗證,但continue_if_empty
不適用於File
,它返回No file was uploaded
錯誤。 但一切都適用於Input
和Select
。 我需要這方面的幫助。
$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);
}
這幫助我解決了“沒有上傳文件”錯誤。
在您打開表單標記之前,將此行包含在您的視圖模板 (.phtml) 中:
$form->prepare()
完整示例:
<!-- 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() ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.