gpt4 book ai didi

javascript - 我需要帮助加载文件 symfony2,无法在 formbuilder 中找到输入名称

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:41 25 4
gpt4 key购买 nike

我正在遵循 Symfony 2.8 中上传文件的指南

这是我的 Controller 。

public function new1Action(Request $request)
{
$pedido = new Pedidos();
$form = $this->createForm(new PedidosType, $pedido);
if ($request->getMethod() == 'POST')
{
$image = $request->files->get('name_input_image');
print_r($image);exit;
if(($image instanceof UploadedFile) && ($image->getError() == '0'))
{
$originalName = $image->getClientOriginalName();
$name_array = explode('.',$originalName);
$file_type = $name_array[sizeof($name_array) - 1];
$valid_filetypes = array('jpg','jpeg','png');
if(in_array(strtolower($file_type), $valid_filetypes))
{
$document = new Document();
$document->setFile($image);
$document->setSubDirectory('archivos');
$document->processFile();

$pedido->setDescripcion($image->getBasename());

$em = $this->getDoctrine()->getManager();

$em->persist($pedido);
$em->flush();

$this->get('session')->getFlashBag()->add('mensaje',
'Se inserto la imagen correctamente');
return $this->redirect($this->generateUrl('bd_pedidos'));

} else
{
$this->get('session')->getFlashBag()->add('mensaje',
'La extension del archivo no es la correcta');
return $this->redirect($this->generateUrl('bd_formnew1'));
}
} else
{

$this->get('session')->getFlashBag()->add('mensaje',
'No ingreso el archivo o se produjo un error inesperado');
return $this->redirect($this->generateUrl('bd_formnew1'));
}
}

问题出在行 $image = $request->files->get('name_input_image');'name_input_image' 是我 View 中输入的名称。当我尝试使用常规输入(没有 formbuilder)时,一切正常,因为我可以控制输入的名称,但是当我使用 formbuilder 时,我不知道应该在 中设置的输入名称是什么$image = $request->files->get('XXXXX'); .

我还尝试使用 {{ form.task.vars.full_name }} 这可以返回 pedido_bdbundle_pedidos[descripcion],但是当我使用 $image = $request->files->get('pedido_bdbundle_pedidos[descripcion]'); 我的 $image 变量为空。

我不知道问题出在哪里,也不知道我能做什么。

这是我的观点:

<form role="form" method="POST" enctype="multipart/form-data" action="{{path('bd_formnew1')}}">
<table class="record_properties">
<tr>
<td>{{ form_label (form.codArticulo) }}</td>
<td>{{ form_widget (form.codArticulo) }}</td>
</tr>
<tr>
<td>{{ form_label (form.descripcion) }}</td>
<td>{{ form_widget (form.descripcion) }}</td> // this is my image field
</tr>
<tr>
<td>{{ form_label (form.abreviacion) }}</td>
<td>{{ form_widget (form.abreviacion) }}</td>
</tr>
<tr>
<td>{{ form_label (form.color) }}</td>
<td>{{ form_widget (form.color) }}</td>
</tr>
<tr>
<td>{{ form_label (form.cantidad) }}</td>
<td>{{ form_widget (form.cantidad) }}</td>
</tr>
<tr>
<td>{{ form_label (form.precio) }}</td>
<td>{{ form_widget (form.precio) }}</td>
</tr>
<tr>
<td>{{ form_label (form.envio) }}</td>
<td>{{ form_widget (form.envio) }}</td>
</tr>
<tr>
<td>{{ form_label (form.total) }}</td>
<td>{{ form_widget (form.total) }}</td>
</tr>
<tr>
<td>{{ form_label (form.fechaIni) }}</td>
<td>{{ form_widget (form.fechaIni) }}</td>
</tr>
<tr>
<td>{{ form_label (form.fechaFin) }}</td>
<td>{{ form_widget (form.fechaFin) }}</td>
</tr>
<tr>
<td>{{ form_label (form.flag) }}</td>
<td>{{ form_widget (form.flag) }}</td>
</tr>
{{ form_rest(form) }}
</table>
<table>
<tr>
<td><button type="submit" class="btn btn-primary">Enviar</button> </td>
</tr>
</table>

也许问题出在我的实体上。有谁知道如何在表单生成器中设置输入文件的名称?

这是我的实体:

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('codArticulo',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('descripcion',FileType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px'),'label'=>'Imagen', 'data_class'=>null))
->add('abreviacion',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('color',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('cantidad',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('precio',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('envio',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('total',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px')))
->add('fechaIni',DateType::class, array('widget'=>'single_text', 'html5' => false, 'input' => 'datetime','label'=>'Fecha Pedido','format'=>'dd/MM/yyyy', 'read_only'=>true, 'attr'=> ['class'=>'form-control js-datepicker', 'style' => 'margin-bottom:5px','placeholder'=>'dd/mm/yyyy']))
->add('fechaFin',DateType::class, array('widget'=>'single_text', 'html5' => false, 'input' => 'datetime','label'=>'Fecha Entrega','format'=>'dd/MM/yyyy', 'read_only'=>true, 'attr'=> ['class'=>'form-control js-datepicker', 'style' => 'margin-bottom:15px','placeholder'=>'dd/mm/yyyy']))
->add('flag',ChoiceType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:5px'),'disabled'=>false,'choices'=>array('1'=>'Vigente','0'=>'Arrivado'),'required'=>true))
;
}

最佳答案

您只需使用 $form->handleRequest 在 Controller 操作中提交表单:

public function new1Action(Request $request)
{
$pedido = new Pedidos();
$form = $this->createForm(new PedidosType, $pedido);
if ($request->getMethod() == 'POST')
{
$form->handleRequest($request);
$image = $pedido->getImage();
//

handleRequest() 将通过表单将请求数据与您的 Pedido 对象混合在一起。

现在您的 $pedido 对象已水化,您可以使用您在实体类中设置的 getter 直接检索图像。

关于javascript - 我需要帮助加载文件 symfony2,无法在 formbuilder 中找到输入名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42801477/

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