Is it possible to render a view into a formmapper?
The situation is like this:
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Company')
->with('Info')
->add('name')
->end()
->end()
->tab('Abonnementen')
->with('Abonnementen', array('class' => 'col-md-12'))
//Render a partial twig here
->end()
;
}
This is in my twig:
<form class="form-horizontal" action="" method="post" style="margin-top:15px;">
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header"><h4 class="box-title">Abonnementen</h4></div>
<div class="box-body">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Active</th>
</tr>
</thead>
<tbody>
{% for abonnement in abonnementen %}
<tr>
<td>{{ abonnement.name }}</td>
<td>{{ abonnement.description }}</td>
<td>€ {{ abonnement.price }}</td>
<td>
<input type="checkbox" name="{{ abonnement.id }}[active]"
{% if abonnement.active %}
checked="checked"
{% endif %}>
</td>
</tr>
{% endfor %}
<tr>
<td colspan="4">
<input type="submit" class="btn btn-primary" value="Save" name="abonnement_save">
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</form>
Is it even possible to load this into my tab in my formmapper?
I think that is possible to do something similar. You want to inject your form into base sonata form. It is possible but at the end you get something like this:
<form> # form from sonata
<form> #your custom form
</form>
</form>
I don't recommend it.
Formmaper build form from formBuilder and you can define template(html) of your form field in standart way:
http://symfony.com/doc/current/cookbook/form/form_customization.html
I reccommend you to override sonata template name 'edit' and add there your code as another form.
public function configure()
{
$this->setTemplate('edit', 'ApplicationM2MNewsletterBundle:CRUD:empty_form.html.twig');
}
Related
I made a query to group all the Audits belonging to a specific Area, like in the next query.
SELECT id_Area as idArea, area.Name as area_name, GROUP_CONCAT(id_Audit) as Audits
FROM helios.fsa_audits
LEFT JOIN helios.fsa_areas area USING (id_Area)
WHERE id_Auditor='4'
GROUP BY id_Area;
As result I get this:
I made this to get a datatable in the view like this:
But What I need is to get the id of the Audit at the moment of click each button in the Auditorias.
I'm working with symfony, and I render the datatable like this:
<table aria-describedby="dataTable_info" cellspacing="0" class="table table-hover dataTable" id="dataTable" role="grid" style="width:100%;" width="100%">
<thead>
<tr>
<th>Areas</th>
<th>Auditorias</th>
</tr>
</thead>
<tbody>
{% for audit in auditsByArea %}
<tr>
<td>{{ audit.area_name }}</td>
<td>
{% for i in 1..4 %}
<input class ="auditBtn mx-2" value="{{'W' ~ i }}" href="" data-id="{{ audit.Audits }}" data-area="{{ audit.area_name }}"id="auditBtn" name="auditBtn" type='text' readonly data-toggle="modal" data-target="#auditModal"></input>
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
But it does not put the one id per button(Audit), it assigns all the id's in all the buttons:
data-id="10,11,12,13"
Any idea or suggestion about how to get it?
I'm not in symfony but you should explode your group_concat result
my_array = explode({{ audit.Audits }});
then in you loop refer to the content on the array
<td>
{% for i in 1..4 %}
<input class ="auditBtn mx-2" value="{{'W' ~ i }}" href="" data-id="{{ my_array[i] }}" data-area="{{ audit.area_name }}"id="auditBtn" name="auditBtn" type='text' readonly data-toggle="modal" data-target="#auditModal"></input>
{% endfor %}
</td>
The Routes
Route::get('/adminContacts/{user_id}', 'AdminContactsController#index')->name('adminContacts.index')->middleware('is_admin');
Route::get('/adminContacts/{user_id}/create', 'AdminContactsController#create')->name('adminContacts.create')->middleware('is_admin');
adminContactController
public function index($user_id)
{
// Confirm User exists
User::findOrFail($user_id);
$filter = request('filter', NULL);
$contacts = NULL;
if ($filter == NULL)
$contacts = Contacts::query()->where('owner_id', $user_id)->sortable()->paginate(5);
else
$contacts = Contacts::query()->where('owner_id', $user_id)->where('name', 'like', '%'.$filter.'%')
->orWhere('number', 'like', '%'.$filter.'%')
->sortable()->paginate(5);
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create($user_id)
{
return view('adminContacts.create')->withUserId($user_id);
}
index.blade.php
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index') }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index') }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
The error is:
Undefined variable: user_id (View: D:\laragon\www\SampleContacts\resources\views\adminContacts\index.blade.php)
Am I missing something? The idea is to pass the $user_id to the adminContact.create form where i can use it to set
$contact->owner_id = $user_id;
so the entered contact apears under that users table.
I don't think you can pass variables into your view like you're doing now.
In your controller, try changing this line:
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
To this:
return view('adminContacts.index')->with('contacts', $contacts)->with('user_id', $user_id);
You could explicitly name the variable when passing it to the view, either this way
return view('adminContacts.index')->with('user_id', $user_id);
Or
return view('adminContract.index', compact('user_id'));
I'm not sure what the name of the variable becomes when passed to the view via withUserId, but I'm guessing this is the issue.
Found the problem.
#extends('layouts.app')
#section('content')
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index', $user_id) }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index', $user_id) }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
#endsection
Anywhere where there is a {{ route('adminContacts.index') }} it also needed the $user_id to be passed along with it because I'm also using it in the routes as '/adminContacts/{user_id}'
Thank you for all the help.
I have an issue with "object not found by the #ParamConverter annotation" on Symfony 3.4 when I try to delete selected items of table. I think it's an issue when I try to get the "spectacle" with the id ("findOneBy()")
This is my code (html.twig) :
<form method="delete" action="{{ path('admin_spectacle_delete_selected') }}">
<button class="content-red btn btn-fabop" type="submit"><i class="fa fa-trash"></i> Tout supprimer</button>
<div class="table-responsive">
<table id="myTable" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th style="text-align:center;"><input type="checkbox" id="all"></th>
<th>Nom</th>
<th>Lieu</th>
<th>Date spectacle</th>
<th>Annee</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for spectacle in spectacles %}
<tr>
<td id="spectacle{{ spectacle.id }}"><input type="checkbox" name='multiSelected[]' value="{{ spectacle.id }}"></td>
<td>{{ spectacle.nom }}</td>
<td>{{ spectacle.lieu }}</td>
<td>{{ spectacle.dateSpectacle }}</td>
<td>{{ spectacle.annee }}</td>
<td>
<a class="content-blue btn-fabop btn" href="{{ path('admin_spectacle_show', { 'id': spectacle.id }) }}"><i class="fa fa-search"></i> Détail</a>
<a class="content-purple btn-fabop btn" href="{{ path('admin_spectacle_edit', { 'id': spectacle.id }) }}"><i class="fa fa-pencil"></i> Edition</a>
<a class="content-red btn-fabop btn" href="{{ path('admin_spectacle_delete_confirmed', { 'id': spectacle.id }) }}"><i class="fa fa-trash"></i> Suppression</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
and controller :
/**
* Confirmation delete
*
* #Route("/deleteSelected", name="admin_spectacle_delete_selected")
*/
public function deleteSelectedAction(Request $request)
{
$items_selected_id = $request->get('multiSelected');
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository(Spectacle::class);
foreach($items_selected_id as $item_id) {
$spectacle = $repository->findOneById($item_id);
if (!$spectacle) {
throw $this->createNotFoundException(
'No spectacle found for id '.$spectacle
);
}
else{
$em->remove($spectacle);
}
}
$em->flush();
return $this->redirectToRoute('admin_spectacle_index');
}
Thank you for your response !!
Please try to change this:
<form method="delete"
to this:
<form method="post"
Because seems that you are get your variables like a POST action not a DELETE
Thank you for all the help in the past.
On my app, when i try to view a certain blade, it returns the error
"Trying to get property of non-object".
It has pointed me to the blade and the section of code where the error comes from:
#extends('layouts.app')
#section('content')
#component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
Create
Activation Pins - {{ $item->name }}
</div>
{{--<div class="panel-body">--}}
{{--#if($pin_item && !$pin_item->pivot->is_paid )--}}
{{--<form action="/activation-pins/{{ $pin_item->id }}" method="post">--}}
{{--<input type="hidden" name="_method" value="PUT">--}}
{{--<input type="hidden" name="vendor_id" value="{{ $item->vendor->id }}">--}}
{{--{!! csrf_field() !!}--}}
{{--<button type="submit" class="btn btn-success">Mark As Paid</button>--}}
{{--</form>--}}
{{--#endif--}}
{{--</div>--}}
<table class="table">
<thead>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Is Active</th>
<th>User</th>
</tr>
</thead>
<tbody>
#forelse($item->activation_pins as $itm)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $itm->pin }}</td>
<td>
<span class="label label-{{ $itm->is_active ? ( $itm->is_valid ? 'info' : 'danger' ) : 'warning' }}">
{{ $itm->is_active ? ( $itm->is_valid ? 'active' : 'expired' ) : 'inactive' }}
</span>
</td>
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
</tr>
#empty
<tr>
<td colspan="6">
<p class="text-center">No Activation Pins</p>
</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#endcomponent
#endsection
With emphasis on:
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
Could I be missing something? Thanks.
This is what it displays now:
I would like to display data based on the chosen values from select box and search box. but select box ( maybe search box has same issue too) get reset whenever I click the page 2,3,4....
I believe I missed something with pagination code in the view / controller.
Please let me know the way to solve this issue.
Controller
class MemberListTestController extends Controller
{
public function search(Request $request)
{
$request->flash();
$memberName = $request->input('memberName');
$chosenCenter = $request->input('chosenCenter');
$memberLists= DB::table('tbMbrMember as Member')
->select(['Org.orgNm',
'Member.firstNm',
'Member.lastNm',
'Member.regDate',
'MshipCd.mshipNm',
'Att.dt',
'Member.phoneCell',
'Member.email',
'Group.groupNm',
DB::raw('MAX(Att.dt) AttDt, GREATEST(MAX(Reg.endDt),MAX(Reg.expDt)) rExpDt')])
->join('tbMbrCenter as Center', 'Member.mbrCd', '=', 'Center.mbrCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->leftjoin('tbMbrGroup as Group','Center.orgCd','=','Group.orgCd')
->leftjoin('tbMbrMshipReg as Reg', 'Member.mbrCd', '=', 'Reg.mbrCd')
->leftjoin('tbCmMshipCd as MshipCd', 'MshipCd.mshipCd', '=', 'Reg.mshipCd')
->leftjoin('tbMbrAtt as Att', 'Att.mbrCd', '=', 'Member.mbrCd')
->where('Center.orgCd', $chosenCenter)
->groupby('Member.mbrCd')
->paginate(25);
$centerLists = DB::table('tbMbrCenter as Center')
->select('Org.orgNm','Center.orgCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->where('Center.isShow','1')
->where('Org.companyCd','c1')
->where('Org.isShowList','1')
->groupby('Center.orgCd')
->orderby('Org.orgNm')
->get();
return view('member.memberList_test')
-> with('memberLists', $memberLists)
-> with('centerLists', $centerLists);
}
}
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('member/test','MemberListTestController#search');
Route::post('member/test','MemberListTestController#search');
});
View
#extends('layouts.master')
#section('content')
<div class="container-fluid">
<p></p>
<div class="row"></div>
<div class="row">
<div class="col-md-4">
<form method="post" action="{{ url('member/test') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<select class="form-control" id="chosenCenter" name="chosenCenter">
<option value="" selected disabled> Choose Center</option>
#foreach ( $centerLists as $centerList )
<option value= {{ $centerList->orgCd }} #if (old('chosenCenter') == $centerList->orgCd) selected #endif > {{ $centerList->orgNm }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control"
placeholder="Search for First Name, Last Name... 'search all' for blank "
name="memberName">
<span class="input-group-btn">
<button class="btn btn-default " type="submit">Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
<div class='row'></div>
<div class="row">
<table class="table table-striped table-condensed">
<thead>
<th>#</th>
<th>Center</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Reg.</th>
<th>Recent Membership</th>
<th>Date of Membership Exp.</th>
<th>Latest Attendance</th>
<th>Cell Phone</th>
<th>Email</th>
<th>Member Group</th>
</thead>
<tbody>
<?php $count = 1; ?>
#foreach($memberLists as $memberList)
<tr>
<td>{{ (($memberLists->currentPage() - 1 ) * $memberLists->perPage() ) + $count++}}</td>
<td> {{ $memberList->orgNm }}</td>
<td> {{ $memberList->firstNm }} </td>
<td> {{ $memberList->lastNm }} </td>
<td> {{ $memberList->regDate }} </td>
<td> {{ $memberList->mshipNm }}</td>
#if (($memberList->rExpDt) >= date('y-m-d'))
<td> {{$memberList->rExpDt }}</td>
#else
<td> Expired on {{ $memberList->rExpDt }} </td>
#endif
<td>{{ $memberList->AttDt }}</td>
<td> {{ $memberList->phoneCell }}</td>
<td> {{ $memberList->email }}</td>
<td> {{ $memberList->groupNm }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $memberLists->render() !!}
</div>
</div>
#stop
You have to pass the parameters via GET and recover the parameters from the URL.
{!! $memberLists->appends(Input::all())->render() !!}
This will send you to page 2 and so on... with all the inputs values.
I found a video in laracasts that explains all the built-in pagination options for your blade template in laravel 5.2 :
https://laracasts.com/lessons/crazy-simple-pagination
you will see that the correct way for a search page is:
{{ $memberLists->appends(Request::except('page'))->links() }}
This way it will keep the current url parameteres.