I'm trying to upload 2 images with 2 form fields. My form:
->add('zdjecie', FileType::class, array('label' => 'Zdjecie (img file)'))
->add('zdjecieMIN', FileType::class, array('label' => 'Zdjecie miniatura (img file)'))
Entity:
/**
* #ORM\Column(type="string")
*
* #Assert\NotBlank(message="Dodaj zdjecie miniaturke")
* #Assert\File(mimeTypes={"image/png", "image/jpeg", "image/jpg",})
*/
private $zdjecieMIN;
public function getZdjecieMIN()
{
return $this->zdjecieMIN;
}
public function setZdjecieMIN($zdjecieMIN)
{
$this->zdjecieMIN = $zdjecieMIN;
return $this;
}
/**
* #ORM\Column(type="string")
*
* #Assert\NotBlank(message="Dodaj zdjecie")
* #Assert\File(mimeTypes={"image/png", "image/jpeg", "image/jpg",})
*/
private $zdjecie;
public function getZdjecie()
{
return $this->zdjecie;
}
public function setZdjecie($zdjecie)
{
$this->zdjecie = $zdjecie;
return $this;
}
Controller:
public function newAction(Request $request)
{
$buty = new Buty();
$form = $this->createForm('ShoeShopBundle\Form\ButyType', $buty);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $buty->getZdjecie();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move(
$this->getParameter('img_directory'),
$fileName
);
$buty->setZdjecie($fileName);
$file2 = $buty->getZdjecieMIN();
$fileName2 = md5(uniqid()).'.'.$file->guessExtension();
$file2->move(
$this->getParameter('img_directory'),
$fileName2
);
$buty->setZdjecieMIN($fileName2);
$em = $this->getDoctrine()->getManager();
$em->persist($buty);
$em->flush();
return $this->redirectToRoute('app_admin_buty_show', array('id' => $buty->getId()));
}
return $this->render('ShoeShopBundle:Admin/Buty:new.html.twig', array(
'buty' => $buty,
'form' => $form->createView(),
));
}
Config:
parameters:
locale: en
img_directory: '%kernel.root_dir%/../web/uploads/img'
Everything was ok when I was using only 1 image upload field but now im getting "The file "C:\xampp\tmp\phpBF79.tmp" does not exist " error, anyone know what's wrong?
Thanks in advance for your help.
Edit: Added my html/twig form
{% extends 'base.html.twig' %}
{% block body %}
<div class="adm-new">
<h2>Dodaj nowy produkt</h2>
{{ form_start(form) }}
<div>
{{ form_errors(form.marka) }}
<div>
<div>
{{ form_label(form.marka) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.marka) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.model) }}
<div>
<div>
{{ form_label(form.model) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.model) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.kolor) }}
<div>
<div>
{{ form_label(form.kolor) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.kolor) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.cena) }}
<div>
<div>
{{ form_label(form.cena) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.cena) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.rozmiar) }}
<div>
<div>
{{ form_label(form.rozmiar) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.rozmiar) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.zdjecieMIN) }}
<div>
<div>
{{ form_label(form.zdjecieMIN) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.zdjecieMIN) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.zdjecie) }}
<div>
<div>
{{ form_label(form.zdjecie) }}:
</div>
</div>
<div>
<div>
{{ form_widget(form.zdjecie) }}
</div>
</div>
</div>
<div><input type="submit" value="Dodaj" /></div>
{{ form_end(form) }}
<ul>
<li>
Powrot do listy produktow
</li>
</ul>
</div>
{% endblock %}
I have a suspicion that uniqid is generating the same filename for both uploads, and that that is the heart of the file-not-found issue you're seeing.
From the PHP docs:
uniqid
Gets a prefixed unique identifier based on the current time in microseconds.
Both calls to uniqid are executed close enough together that based on the time-in-microseconds part of the function, it might assign them both the same name. Therefore, the second one will be "missing" when you go to $file->move it.
Try subbing in mt_rand, for a far lesser likelihood of file name collisions. You can also mitigate this possibility by calling getBasename on the $file, and concatenating that to the string passed to md5.
Like Camerun Hurd suggested it was a problem with uniqid generating the same filename for both files. mt_rand() function gave the same result so I just changed
$file2 = $buty->getZdjecieMIN();
$fileName2 = md5(uniqid()).'.'.$file->guessExtension();
$file2->move(
$this->getParameter('img_directory'),
$fileName2
);
$buty->setZdjecieMIN($fileName2);
in my controller to:
$ext_pos = strrpos($fileName, '.');
$file2 = $buty->getZdjecieMIN();
$fileName2 = substr($fileName, 0, $ext_pos) . '_min' . substr($fileName, $ext_pos);;
$file2->move(
$this->getParameter('img_directory'),
$fileName2
);
$buty->setZdjecieMIN($fileName2);
Thanks Cameron and rodd from #symfony for all the help.
I think it not solve your problem but you check in file2name extension from file not from file2.
If you move file to another place maybe it's problem with guessExtension() on moved file.
Related
I have tried to code a cutome view in which i would be able to code an html tag to directly download the file which was uploaded before with voyager admin panel. here is my route
Route::get('/download/{research}',[App\Http\Controllers\ResearchController::class, 'download'])->name('download');
here is the html tag:
Download
help me in the controller bellow
public function download(Research $research)
{
}
I've worked through this question all night long and found out this
helpful.
Then I solved it like this:
Controller
public function home()
{
$researches = Research::all();
foreach ($researches as $research){
$download_links[] = json_decode($research->attachment);
}
$departments = Department::all();
$role_id = \TCG\Voyager\Models\Role::all()->where('name','=','student')->first()->id;
$students = User::all()->where('role_id','=',$role_id);
return view('Research.home', compact('researches','departments','students','download_links'));
}
View
{{ $i=0 }}
#foreach($researches as $research)
<div class="row">
<div class="col-md-10">
<button data-toggle="collapse" data-target="#demo{{ $research->id }}" class="btn border text-start form-control" title="click to read abstract">[ {{ ucwords($research->title) }} ] By: {{ $research->user->name }} #if($research->user->student != null) {{ $research->user->student->last_name }} #else {{ $research->user->employee->last_name }}#endif</button>
</div>
<div class="col">
Download
</div>
</div>
<div id="demo{{ $research->id }}" class="collapse row border">
<div class="col-md-12 ">{!! $research->description !!}</div>
</div>
#endforeach
and now is working properly.
public function download($id) {
$research= Research::where('id', $id)->firstOrFail();
$pathToFile = storage_path('fileName' . $research->file);
return response()->download($pathToFile);
}
Here is what i'm trying to do :
I have a classic Folder entity which contains Sku entities in a OneToMany relation as an ArrayCollection. So far so good.
I want the FolderType to dynamically create as many Skus as I want in it. For that, I followed the Symfony fast track method to generate prototypes in javascript. Works great.
Now, in this folder form, "skus" are an array collection of entities. In the SkuType, I have files to uploads. Here are the forms (Folder and Sku) :
$builder
->add('norlogReference', TextType::class, [
'label' => 'Référence du dossier'
])
->add('skus', CollectionType::class, [
'entry_type' => SkuType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'prototype' => 'skus'
]);
As for the SkuType, here are my upload fields :
->add('picture_1', FileType::class, [
'label' => 'Image 1 du produit',
'mapped' => false,
'required' => false,
'attr' => ['accept' => 'image/*']
])
->add('picture_2', FileType::class, [
'label' => 'Image 2 du produit',
'mapped' => false,
'required' => false,
'attr' => ['accept' => 'image/*']
])
Here is the controller recieving the form :
/**
* #Route("/new", name="folder_new")
*/
public function new(Request $request): Response
{
$entityManager = $this->getDoctrine()->getManager();
$norlogFolder = new NorlogFolder();
$form = $this->createForm(NorlogFolderType::class, $norlogFolder);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
foreach ($norlogFolder->getSkus() as $sku) {
I want to get both file objects here, pass them in handleUploadedFile(), and use $sku->setPicture1() and setPicture2 with the $newfileName returned by the private method.
$sku->setFolder($norlogFolder);
$sku->setSKU($norlogFolder->getNorlogReference() . '-' . $sku->getSKU());
}
$entityManager->persist($norlogFolder);
$entityManager->flush();
return $this->redirectToRoute('folder_edit', ['id' => $norlogFolder->getId()]);
}
return $this->render('folder/new.html.twig', [
'form' => $form->createView(),
]);
}
and the little private method I want to use in order to handle the file :
private function handleUploadedFile(UploadedFile $uploadedFile): string
{
$destination = $this->getParameter('kernel.project_dir').'/public/uploads/sku_medias';
$originalFilename = pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_FILENAME);
$newFilename = $originalFilename . '-' . uniqid() . '.' . $uploadedFile->guessExtension();
$uploadedFile->move(
$destination,
$newFilename
);
return $newFilename;
}
And the folder form view :
{{ form_start(form, {
attr: {
class: 'sku-form'
}
}) }}
<div class="form-group">
{{ form_row(form.norlogReference, {
attr: {
placeholder: 'ex: XOTP-25',
class: 'sku-form-input'
}
}) }}
</div>
<div class="form-group sku-row skus" data-prototype="{{ form_widget(form.skus.vars.prototype)|e('html_attr') }}">
{% if form.skus|length > 0 %}
{% for sku in form.skus %}
<div class="row sku-bloc bg-light">
<div class="field-title">
<p class="display-3 text-center">Produit #{{ loop.index }}</p>
<a href="{{ path('sku_delete', {id: sku.vars.data.id}) }}"
class="btn bg-danger btn-sku-remove"><i class="fas fa-trash-alt"></i></a>
</div>
<span class="btn btn-secondary form-extend-btn">Voir</span>
<div class="col-12 sku-fields-bloc">
<div class="form-control">
{{ form_row(sku.SKU, {
attr: {
placeholder: 'ex: XVF-25663',
class: 'sku-form-input'
}
}) }}
</div>
<div class="container">
<div class="row">
{{ form_label(sku.marque) }}
{{ form_widget(sku.marque, {
attr: {
class: 'sku-form-input'
}
}) }}
{{ form_label(sku.taille) }}
{{ form_widget(sku.taille, {
attr: {
class: 'sku-form-input'
}
}) }}
{{ form_label(sku.designation) }}
{{ form_widget(sku.designation, {
attr: {
class: 'sku-form-input'
}
}) }}
{{ form_label(sku.couleur) }}
{{ form_widget(sku.couleur, {
attr: {
class: 'sku-form-input'
}
}) }}
{{ form_label(sku.etat) }}
{{ form_widget(sku.etat, {
attr: {
class: 'sku-form-input'
}
}) }}
{{ form_label(sku.composition) }}
{{ form_widget(sku.composition, {
attr: {
class: 'sku-form-input'
}
}) }}
</div>
</div>
<div class="form-control mt-3">
{{ form_row(sku.picture_1, {
attr: {
placeholder: 'Image 1',
class: 'sku-form-input'
}
}) }}
</div>
<div class="form-control mt-3">
{{ form_row(sku.picture_2, {
attr: {
placeholder: 'Image 2',
class: 'sku-form-input'
}
}) }}
</div>
</div>
</div>
{% endfor %}
{% else %}
<div class="row">
<div class="col-12 mt-4">
<p class="p-3 bg-info">Pas encore de {{ form_label(form.skus) }} ajoutés</p>
{{ form_widget(form.skus) }}
</div>
</div>
{% endif %}
</div>
<div class="row">
<div class="col-4">
<button type="button" class="btn btn-primary btn-nav w-100 mt-2 mb-2 add_item_link"
data-collection-holder-class="skus">
Ajouter Sku
</button>
</div>
<div class="col-4">
<button type="submit"
class="btn btn-success btn-nav w-100 mt-2 mb-2">{{ button_label|default('Enregistrer') }}</button>
</div>
<div class="col-4">
<a href="{{ path('folder_list') }}"
class="btn btn-primary btn-nav w-100 mt-2 mb-2">Retour liste</a>
</div>
</div>
{{ form_end(form) }}
So the question is : how to get these picture_1 and 2 objects (and not path which I already have by remapping them to true), by Sku, in the controller side, in order to handle the files as usual ?
I tried :
$request->files->get() (result = null, and I have the enctype right in the form)
$sku->getPicture_1() (result = temporary linux path, but is there a simple way to retrieve the file from that, and on every OS ?)
Trying to access the $form from inside the sku loop (result = nada)
$request->get('picture_1') and that + ->getData()
Various weird tries I forgot which gave me nothing
I might miss something obvious here, but I can't think properly on this project anymore.
Please don't mind the necessary refactoring for now =) Ty !
With 'mapped' => false, you need to handle the collection of uploads like this:
if ($form->isSubmitted() && $form->isValid()) {
foreach ($form->get('skus') as $formChild)
{
// Get the unmapped picture fields
$uploadedPicture1 = $formChild->get('picture_1')->getData();
$uploadedPicture2 = $formChild->get('picture_2')->getData();
// Get the sku
$sku = $formChild->getData();
// Upload the pictures
$picture1Filename = $this->handleUploadedFile($uploadedPicture1);
$picture2Filename = $this->handleUploadedFile($uploadedPicture2);
// Set the new filenames onto the sku
$sku->setPicture1($picture1Filename);
$sku->setPicture2($picture2Filename);
// Your original code
$sku->setFolder($norlogFolder);
$sku->setSKU($norlogFolder->getNorlogReference() . '-' . $sku->getSKU());
}
}
$entityManager->persist($norlogFolder);
$entityManager->flush();
return $this->redirectToRoute('folder_edit', ['id' => $norlogFolder->getId()]);
}
Edit: do check if a picture was actually uploaded, as in the other answer. :-)
Your picuter_1 and picture_2 are mapped: false.
That means, you have to use ->getData() (or maybe ->getNormData()) on each SkuType or your NorlogFolderType
instead of looping through SKUs entities collection, loop through sku formtypes and get the data directly from 'picture_1' and 'picture_2' field
if ($form->isSubmitted() && $form->isValid())
{
if ($form->has('skus'))
{
foreach ($form->get('skus') as $skuForm)
{
// always check if field named 'picture_1' is there
if ($skuForm->has('picture_1'))
{
/** #var \Symfony\Component\HttpFoundation\File\UploadedFile $firstPic */
$firstPic = $skuForm->get('picture_1')->getData();
//todo: maybe check if null
$picOne = $this->handleUploadedFile($firstPic);
}
// do the same with picture_2 (and others if any)
}
}
}
I'm following this tutorial to create a simple OctoberCMS plugin.
Here is content of /acme/demo/components/todo/default.htm page:
{% set tasks = __SELF__.tasks %}
<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Tasks assigned to: {{__SELF__.name}} </h3>
</div>
<div class="panel-body">
<div class="input-group">
<input name="task" type="text" id="inputItem" class="form-control" value=""/>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Add</button>
</span>
</div>
</div>
<ul class="list-group" id="result">
{% for task in tasks %}
<li class="list-group-item">
{{ task }}
<button class="close pull-right">×</button>
</li>
{% endfor %}
</ul>
</div>
</form>
And here is the content of /acme/demo/components/Todo.php:
<?php namespace Acme\Demo\Components;
use Cms\Classes\ComponentBase;
use Acme\Demo\Models\Task;
class Todo extends ComponentBase
{
/**
* This is a person's name.
* This variable will be available in the page as a twig variable
* #var string
*/
public $name;
/**
* The collection of tasks.
* #var array
*/
public $tasks;
public function componentDetails()
{
return [
'name' => 'Todo Component',
'description' => 'A database driven TODO list'
];
}
public function defineProperties()
{
return [];
}
public function init()
{
// This will execute when the component is first initialized, including AJAX events.
}
public function onRun()
{
$this->name = 'Meysam';
$this->tasks = Task::lists('title');
}
public function onAddItem()
{
$taskName = post('task');
$task = new Task();
$task->title = $taskName;
$task->save();
}
}
The problem is that onAddItem is never called. It seems that the form is not submitted properly when I add a new item. Does anybody know how I can fix this? I thought maybe the ajax libraries are missing, so I included {% framework %} as well, which again didn't help:
{% set tasks = __SELF__.tasks %}
{% framework %}
<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">
Please note that my model is working and $this->tasks = Task::lists('title'); returns the list of tasks.
The problem was that I should have included the jquery file as well:
<script src="{{ [
'assets/javascript/jquery.js',
]|theme }}"></script>
{% framework %}
I'm still learning Laravel and how the blade system all works..
I'm wondering what the better way of doing something is and that something is; I've done a query to pull a row from the database and then I want to put that query into an array so I can pick out whatever column I want.
Then I want to pass that to my home.blade.php and be able to use {{ $name }} for example. T
This is what I've got:
TO NOTE: THIS WORKS THE WAY I WANT IT TO BUT IM SURE IM DOING IT THE LONG WAY (WRONG) ROUND.
HomeController.php
<?php
class HomeController extends BaseController {
public function home() {
$hero_query = DB::table('heros')->where('owner_id', Auth::user()->id)->pluck('hero');
if($hero_query) {
$owner_id = Auth::user()->id;
$user = DB::table('heros')->where('owner_id', $owner_id)->first();
$name = $user->hero;
$level = $user->level;
$exp = $user->exp;
$str = $user->str;
$atk = $user->atk;
$def = $user->def;
$int = $user->int;
$blk = $user->blk;
return View::make('home', array(
'name' => $name,
'level' => $level,
'exp' => $exp,
'str' => $str,
'atk' => $atk,
'def' => $def,
'int' => $int,
'blk' => $blk
));
} else {
return View::make('home');
}
}
}
home.blade.php
#if($hero = DB::table('heros')->where('owner_id', Auth::user()->id)->pluck('hero'))
<form action="{{ URL::route('hero-delete') }}" method="POST">
Your hero:<br>
<b>{{ $hero; }}</b> | <input type="submit" value="Delete"><br>
<b>Stats:</b>
LvL: {{ $level }}
Exp: {{ $exp }}
Str: {{ $str }}
Atk: {{ $atk }}
Def: {{ $def }}
Int: {{ $int }}
Blk: {{ $blk }}
</form>
#else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::token()}}
</form>
#endif
Now I'm sure I'm doing it in some stupid moronic way as I'm just starting out.. but could someone explain where I'm going wrong?
Thanks in advance!
Just pass the entire $user to the view
$user = DB::table('heros')->where('owner_id', $owner_id)->first();
return View::make('home', array('user' => $user));
Then in your view
Str: {{ $user->str }}
Int: {{ $user->int }}
and so on.
You're doing lots of things wrong :(
I fix a little bit the code, but I didn't test it, so use this as a guide to implement what you want.
class HomeController extends BaseController {
public function home() {
$hero = Hero::where('owner_id', '=', $owner_id)->first();
if($hero)
{
return Response::make('home', array('hero' => $hero->toArray()));
}
else
{
return Response::make('home');
}
}
}
The blade template
#if(isset($hero))
{{ Form::open(array('route' => 'hero-delete', 'method' => 'DELETE')) }}
Your hero:<br>
<b>{{ $hero->hero }}</b> | <input type="submit" value="Delete"><br>
<b>Stats:</b>
<!-- Here you can implement a foreach for performance -->
LvL: {{ $hero->level }}
Exp: {{ $hero->exp }}
Str: {{ $hero->str }}
Atk: {{ $hero->atk }}
Def: {{ $hero->def }}
Int: {{ $hero->int }}
Blk: {{ $hero->blk }}
{{ Form::close() }}
#else
{{ Form::open(array('route' => 'hero-create')) }}
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::close() }}
#endif
I'm facing a problem that I can summarize as it follows:
I have a TWIG template page like this (reg.html.twig):
{% extends "::base.html.twig" %}
{% block body %}
<ul class="tabs">
<li class="left">tab1</li>
<li class="left">tab2</li>
<li class="left">tab3</li>
<li class="right">tab4</li>
</ul>
<div class="tabs_container">
<div id="tab1" class="blocco-tab">
<form action="{{ path('AAA') }}" method="post" {{ form_enctype(form) }}>
<div id="name_field">
{{ form_row(form.name) }}
</div><!-- /name_field -->
<div id="address">
{{ form_row(form.addresses[0].road) }}
</div><!-- /address_field -->
</form>
</div>
<div id="tab2" class="blocco-tab">
<form action="{{ path('BBB') }}" method="post" {{ form_enctype(form) }}>
<div id="surname_field">
{{ form_row(form.surname) }}
</div><!-- /surname_field -->
</form>
</div>
</div> <!-- contenitore_tabs -->
{% endblock %}
Fields name, surname and addresses belong to a sample Symfony2 entity Person.
addresses is the first and only element of a collection of addresses (I need this as collection for other reasons)
The working JS file is:
jQuery(document).ready(function() {
$(".blocco-tab").hide();
$("ul.tabs li:first").addClass("active").show();
$(".blocco-tab:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".blocco-tab").hide();
var activeTab = $(this).find("a").attr("href");
$(activeTab).fadeIn();
return false;
});
});
The Entity file:
class Person {
protected $name;
protected $surname;
protected $addresses;
public function __construct(){
$this->addresses = new ArrayCollection();
}
}
And in the DefaultController:
public function tab1Action(Request $request){
$person = new Person();
$address = new Address();
$addr_coll = new ArrayCollection();
$addr_coll->add($address);
$tab1_type = new Tab1Type();
$person->setAddresses($addr_coll);
$form = $this->createForm($tab1_type, $person);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
/*ecc ecc ecc*/
}
public function tab2Action(Request $request){
$person = new Person();
$tab2_type = new Tab2Type();
$form = $this->createForm($tab2_type, $person);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
/*ecc ecc ecc*/
}
Actually I took the way of having every FormType having all fields that I don't need rendered but put 'hidden' and 'property_path' => false, because I can't render only my desired fields cause the other ones will cause errors at runtime (they're null) , but I still get problems handling both cases in a joined way.
Putting every form in a different page (== different Route), with different Controllers, everything works fine, so it's not a problem related to basic use of symfony, It's the integration of N forms in a single page with JQuery UI that makes me cry.
Fixed that I have to use this tabs, how can I solve?
Do I have to make a single Action handling everything?
Do I have to make a single form?
Do I miss something?
Thanks in advance, I hope I've been clear in explaining my issue.
You just used the same variable for different forms
<div id="tab1" class="blocco-tab">
<form action="{{ path('AAA') }}" method="post" {{ form_enctype(**form1**) }}>
<div id="name_field">
{{ form_row(**form1**.name) }}
</div><!-- /name_field -->
<div id="address">
{{ form_row(**form1**.addresses[0].road) }}
</div><!-- /address_field -->
</form>
</div>
<div id="tab2" class="blocco-tab">
<form action="{{ path('BBB') }}" method="post" {{ form_enctype(**form2**) }}>
<div id="surname_field">
{{ form_row(**form2**.surname) }}
</div><!-- /surname_field -->
</form>
</div>
Try with single form
{% extends "::base.html.twig" %}
{% block body %}
<form action="{{ path('AAA') }}" method="post" {{ form_enctype(form) }}>
<ul class="tabs">
<li class="left">tab1</li>
<li class="left">tab2</li>
<li class="left">tab3</li>
<li class="right">tab4</li>
</ul>
<div class="tabs_container">
<div id="tab1" class="blocco-tab">
<div id="name_field">
{{ form_row(form.name) }}
</div><!-- /name_field -->
<div id="address">
{{ form_row(form.addresses[0].road) }}
</div><!-- /address_field -->
</div>
<div id="tab2" class="blocco-tab">
<div id="surname_field">
{{ form_row(form.surname) }}
</div><!-- /surname_field -->
</div>
</div> <!-- contenitore_tabs -->
</form>
{% endblock %}
Then you have in ocontroller aaaAction()
public function aaaAction(Request $request){
$person = new Person();
$address = new Address();
$addr_coll = new ArrayCollection();
$addr_coll->add($address);
$person->setAddresses($addr_coll);
$form = $this->createForm(new PersonType(), $person);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
/*ecc ecc ecc*/
}
and class for form builder like
class PersonType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name', null, array())
->add('surname', null, array())
->add('addresses', null, array())
;
}
public function getName()
{
return 'person';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\YourBundle\Entity\Person',
);
}
}
as I mentioned before I solved wrapping all tabs in a single form. Both of your solutions are ok, thank you for your time.
Linuxatico
In case anyone else has this problem, you can solve it like this (I am not sure if this is the most elegant solution but I think its better than making one big form for everything). This example mixes the profile and changePassword FOSUserBundle forms:
Inside your main show template (Profile:show.html.twig in my case):
{% if profileForm is defined %}
{% include 'MyBundle:Profile:edit.html.twig' with {'form':profileForm} %}
{% else %}
{% render 'MyBundle:Profile:edit' %}
{% endif %}
Repeat for changePassword:
{% if passwdForm is defined %}
{% include 'MyBundle:ChangePassword:changePassword.html.twig' with {'form':passwdForm} %}
{% else %}
{% render 'FOSUserBundle:ChangePassword:changePassword' %}
{% endif %}
In your controllers (add else):
if ($form->isValid()) {
.....
else {
return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'),
array('user' => $user, 'profileForm' => $form->createView()));
}
Add the profileForm and passwdForm accordingly. In my case the modified controllers were the ProfileController and ChangePasswordControllers (FOSUserBundle overrides).
As for your tabs, you can add javascript (or twig) to open the tab if any error is found.
Hope this helps :)