[英]notBlank constraint not working for File input - Form with OneToMany relationship Symfony2
我在Article
和Image
实体之间有一个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;
....
}
在创建表单时我没有嵌入集合 ,因为项目要求在提交整个表单之前分别要求为每个图像上传AJAX。 换句话说,绑定到输入change
事件的javascript事件侦听器创建AJAX调用,该调用在验证之后上载图像(即,文件在控制器中验证大小,扩展名)。 但是当我发送整个表单时,只有其他字段被验证并且即使没有选择文件也会提交表单(我在标题中看到除了文件一之外的所有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
:
image_upload:
pattern: /{id}/image_upload
defaults: { _controller: ShopManagementBundle:Image:uploads}
requirements: { _method: post|put }
控制器:
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');
}
正如您所看到的,我不是在控制器中使用isValid
验证,我使用if-else语句验证,假设文件已经发送。
毕竟我们在评论区说过,我建议您使用@Assert\\File
进行文件上传验证。 它会阻止你的if / else堆栈。
既然你告诉我你把上传的文件附加到现有的文章上,那么使用嵌入的表格集合将是我提到的解决方案。
由于您将检索文章,然后使用它创建表单类型,因此所有图像都将链接到该文章。
在您的图像列表中添加计数约束 :
/**
* @Assert\Count(
* min = "1",
* minMessage = "You must upload at least one image",
* )
*/
private $images;
由于您将检索已链接到已上载的Image实例的文章,因此验证不会引发错误。 否则,用户正在尝试提交没有图像的表单。 然后,验证约束将使表单无效。
可能还有其他一些小事要做,但它可以帮助你继续前进。
要更好地处理文件上传,请遵循以下方法:
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
它是正式的,然后在上传方法返回时,检查是否没有文件返回您的错误消息,否则继续上传。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.