gpt4 book ai didi

forms - notBlank 约束不适用于文件输入 - 具有 OneToMany 关系的表单 Symfony2

转载 作者:行者123 更新时间:2023-12-02 11:02:37 25 4
gpt4 key购买 nike

我在 ArticleImage 实体之间存在 OneToMany 关系。我创建了文章的形式,如下所示:

class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder

->add('articleTitle','text',array('required' => false))
->add('articlePrice','number',array('required' => false))
->add('images', new ImageType())

;

}
....
}

还有

class ImageType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file',array('required' => false))

;
}
....
}

我在验证文件属性时遇到问题。我的目标是当用户选择任何文件时返回错误消息(至少一个图像应与文章相关联)。我尝试断言 NotBlank 但它不起作用。

<?php
....

use Symfony\Component\Validator\Constraints as Assert;


class Image
{


/**
* Image file
*
* @var File
* @Assert\NotBlank
*/
private $file;


....
}

我不embed collections在创建我的表单时,由于项目要求要求在提交整个表单之前分别对每个图像进行 AJAX 上传。换句话说,绑定(bind)到输入 change 事件的 JavaScript 事件监听器会创建 AJAX 调用,该调用会在验证后上传图像(即在 Controller 中验证文件的大小、扩展名)。但是,当我发送整个表单时,即使没有选择文件,也只会验证其他字段并提交表单(我在标题中看到除文件外的所有 formData 元素)。

我想注意到,当我检查浏览器中的元素时,表单会正确呈现。

我花了很多时间试图解决这个问题,但没有成功,你的帮助就是救援。

编辑:根据 Nawfal Serrar 先生的要求

AJAX 调用的执行方式如下:

$(':file').change(function(){

var file = this.files[0];

var url= $('.article-form').attr('data-iu-url');

var formData = new FormData($('form')[0]);

$.ajax({
url: url,
type: 'POST',
success: completeHandler,
data: formData,
});

});

url 包含遵循以下配置的路由 image_upload:

image_upload:
pattern: /{id}/image_upload
defaults: { _controller: ShopManagementBundle:Image:uploads}
requirements: { _method: post|put }

Controller :

   public function uploadsAction(Request $request, $id) {

if ($request->isMethod('POST')) {
$image = $request->files->get('articletype')['images']['file'];

$status='success';
$message='';
$uploadedURL='';
$the_id=0;


if (($image instanceof UploadedFile) && ($image->getError() == 0)) {

if ($image->getSize() < 50000000000) {
$originalName = $image->getClientOriginalName();
$name_array = explode('.', $originalName);
$extension = $name_array[sizeof($name_array) - 1];
$valid_file_types = array('jpeg', 'jpg', 'bmp', 'png', 'gif');
if (in_array(strtolower($extension), $valid_file_types)) {

$imagee= new Image();

$em = $this->getDoctrine()->getManager();
$imagee->setFile($image);

$imagee->setSubDir('hg');
$imagee->upload();

$entity = $em->getRepository('ShopManagementBundle:Article')->find($id);
$imagee->setAricle($entity);

$uploadedURL= $imagee->getUploadDir(). DIRECTORY_SEPARATOR . $imagee->getSubDir(). DIRECTORY_SEPARATOR . $image->getBasename();


$em->persist($entity);
$em->persist($imagee);
$em->flush();
$the_id=$imagee->getId();

} else {
$status = "fail";
$message = "extension problem";
}
} else {
$status = "fail";
$message = "Image size too big";
}
} else {
$status = "fail";
$message = "Error uploading";
}

return $this->render('ShopManagementBundle:Image:image_portion.html.twig', array(
'status' => $status,
'message' => $message,
'uploadedURL' => $uploadedURL,
'image_id'=>$the_id,

));
}
else
return new Response('RE try uploading');
}

正如您所看到的,我没有在 Controller 中使用 isValid 进行验证,而是使用 if-else 语句进行验证,假设文件已发送。

最佳答案

毕竟我们在评论区说了这么多,我建议您使用 @Assert\File 进行文件上传验证。它会阻止你的 if/else 堆栈。

既然您告诉我您要将上传的文件附加到已有的文章中,那么使用嵌入式表单集合将是我提到的解决方案。

由于您将检索文章,然后使用它创建表单类型,因此所有图像都将链接到它。

添加count constraint在您的图像列表中:

/**
* @Assert\Count(
* min = "1",
* minMessage = "You must upload at least one image",
* )
*/
private $images;

由于您将检索已链接到上传的图像实例的文章,因此验证不会抛出错误。否则,用户将尝试提交不带图像的表单。验证约束将使表单无效。

可能还需要做其他一些小事情才能使其正常工作,但它应该可以帮助您继续前进。

关于forms - notBlank 约束不适用于文件输入 - 具有 OneToMany 关系的表单 Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com