Multiple select edit form selected values - many-to-many

Struggling with an issue in Laravel 4, in a "contact" model edit form, I can get all fields current values except those from the multiple select which is to establish a relation with another model "company". It's a many-to-many relationship. I'm getting the list of companies, but none are selected even if a relation exists.
Here's my edit form:
{{ Form::model($contact, array('route' => array('crm.contacts.update', $contact->id), 'id' => 'edit-contact')) }}
<div class="control-group">
{{ Form::label('first_name', 'First Name', array( 'class' => 'control-label' )) }}
{{ Form::text('first_name') }}
</div>
<div class="control-group">
{{ Form::label('last_name', 'Last Name', array( 'class' => 'control-label' )) }}
{{ Form::text('last_name') }}
</div>
<div class="control-group">
{{ Form::label('email', 'Company Email', array( 'class' => 'control-label' )) }}
{{ Form::text('email') }}
</div>
<div class="control-group">
{{ Form::label('company_ids', 'Company', array( 'class' => 'control-label' )) }}
{{ Form::select('company_ids[]', $companies, array('',''), array('multiple'), Input::old('company_ids[]')) }}
</div>
{{ Form::close() }}
My controller:
public function edit($id)
{
$contact = Contact::find($id);
$company_options = Company::lists('name', 'id');
return View::make('crm.contacts.edit')
->with('contact', $contact)
->with('companies', $company_options);;
}
Any ideas on how to have the multiple select field pre-filled with existing values?
Thanks

Laravel does not support multi-select fields by default you need to use a Form::macro
The example below by #Itrulia was correct, you can simply do:
$users = array(
1 => 'joe',
2 => 'bob',
3 => 'john',
4 => 'doe'
);
echo Form::select('members[]', $users, array(1,2), array('multiple' => true));

Laravel does support multiselect by default.
{{ Form::select('members[]', $users, null, array('multiple' => true)); }}

Related

Form not submitting with Laravel Collection

I'm trying to make a simple create form for a new post using Laravel v.8.
The first time I tried the form it worked fine, but after I tried to add TinyMCE using CDN online, and deleted it, the form stopped working for some reason.
Here's my blade
<div class="box-body">
{!! Form::model($post, [ 'method' => 'POST', 'route' => 'storepost' ]) !!}
<div class="form-group {{ $errors->has('title') ? 'hasError' : '' }}">
{!! Form::label('title') !!} {!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
#if($errors->has('title'))
<span class="help-block"> {{ $errors->first('title') }} </span> #endif
<div class="form-group {{ $errors->has('kategori') ? 'hasError' : '' }}">
{!! Form::label('kategori') !!}
<br> {!! Form::select('kategori', ['anime' => 'Anime', 'news' => 'News', 'medicine' => 'Medicine'], null, ['placeholder' => 'Pick a category...']) !!}
</div>
#if($errors->has('kategori'))
<span class="help-block"> {{ $errors->first('kategori') }} </span> #endif
<div class="form-group {{ $errors->has('body') ? 'hasError' : '' }}">
{!! Form::label('body') !!} {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
#if($errors->has('body'))
<span class="help-block"> {{ $errors->first('body') }} </span> #endif
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary']); !!}
</div>
{!! Form::close() !!}
</div>
Here's my controller
public function create(Post $post)
{
return view('layouts.backend.admin.create', compact('post'));
}
This is where 'dd' doesn't work.
public function store(Requests\PostRequest $request)
{
$request->user()->posts()->create($request->all());
return redirect('admin/all')->with('message', 'Data saved!');
}
And this is my Route
Route::get('/admin/create', [App\Http\Controllers\BackEnd\BlogController::class, 'create'])->name('createpost');
Route::post('/admin/store', [App\Http\Controllers\BackEnd\BlogController::class, 'store'])->name('storepost');
I have other routes too for update, but those worked fine.
It was something on my PostRequest that I forgot to change.
From this:
'category' => 'required',
To this:
'kategori' => 'required',
I'm not sure why it doesn't pass as an error when there's something missing like this, though.

How to add placeholder on input in Symfony 4 form?

In my Symfony 4 form, I am try to get a placeholder for my input. I tried the below but I get an error that it is not allowed. Any ideas how else I can achieve this?
->add('firstname', TextType::class, ['label' => 'Vorname ', 'placeholder' => 'Your name',])
As suggested in the documentation I have also tried the below, here I am not getting an error, just nothing is rendering.
->add('firstname', TextType::class, ['label' => 'Vorname '], ['attr' => ['placeholder' => 'Vorname ']])
You need to do like this :
->add('firstname', TextType::class, array(
'label' => 'Vorname ',
'attr' => array(
'placeholder' => 'hereYourPlaceHolder'
)
))
As they say in the documentation
This also works, by rendering the items individually and adding an attribute in the twig!
{{ form_label(form.firstname) }}
{{ form_widget(form.firstname, {'attr': {'placeholder': 'FIRSTNAME'}}) }}
The documentation of Symfony 6 suggests two ways.
The first option is inside the yourForm.php:
->add('name', TextType::class,
[
'label' => 'Name *',
'attr' => ['placeholder' => 'Name *'],
'required' => true
])
The second option is inside the twig template:
{{ form_label(form.name) }}
{{ form_widget(form.name, {'attr': {'placeholder': 'Name*' }}) }}
You can find more information in the official documentation of Symfony 6 FormType
This is worked in Symfony 4
->add('fullName', null, [
'label' => 'Kontaktperson',
'attr' => [
'placeholder' => 'Contact person name'
],
])
More details: http://toihid.com/?p=326

How to remove default upload field label?

In one of my uploader field, I see a caption displays as the field label which I have failed to change or remove.
Seriously I don't know where it comes from
How to disappear that default label? What to change in my html code? Actually i am using twig php to call the field from an Class object.
profile.html.twig
<div class="row">
<div class="col-md-12 text-center">
<h4>{{user.civilite}} {{ user.nom }} {{ user.prenom }}</h4>
</div>
<div class="col-md-12">
<img class="img-responsive" id="profile-image" src="{{ asset('uploads/profile/images') }}/{{ user.imageprofil }}" alt="">
</div>
</div>
ProfileFormType.php
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('imageProfil')->add('imageFile', VichImageType::class, array('label' => false, 'required' => false ))
->add('civilite', ChoiceType::class, array('choices' => array('M.' => 'M', 'Mme.' => 'Mme'),
'attr' => array(
'class' => 'form-control',
'placeholder' => 'Nom de Famille',
'style' => "margin-bottom:5px;"
)))
->add('nom', TextType::class, array('label' => 'Nom de Famille', 'attr' => array(
'class' => 'form-control',
'placeholder' => 'Nom de Famille',
'style' => "margin-bottom:5px;"
)))
->add('prenom', TextType::class, array('label' => 'Prénom', 'attr' => array(
'class' => 'form-control',
'placeholder' => 'Prénom',
'style' => "margin-bottom:5px;"
)))
->add('email', EmailType::class, array('label' => 'Email', 'attr' => array(
'class' => 'form-control',
'placeholder' => 'Email',
'style' => "margin-bottom:5px;"
)))
Where i can code to disappear the label caption?
i.e. "Aucun fichier choisi"
Solution
<style>
#app_user_profile_imageFile_file {
display: block;
color: transparent;
}
#app_user_profile_imageProfil{
width: 100px;
}
</style>
This is native part of webkit browsers.
You can't remove it the normal way, but you can trick the browser not to show it with little bit of css - just make the font color transparent.
input[type='file'] {
color: transparent;
}
Just add option parameter ['label' => false]

Dynamically upload an image

is it possible to retrieve an image from the database and show it in the upload field of a form?
For example :
I could retrieve a name from the database and display it on the form like this :
{{ Form::text('Location', $variable->name, array('class' => 'form-control', 'required' => '', 'placeholder'=>'Location', 'readonly' => 'readonly')) }}
Can I do the same with an image? I have tried this :
{{ Form::file('Photo', $variable->photo, array('class' => 'form-control', 'required' => '')) }}
but I get error : Illegal string offset 'name'. Any ideas?

Using HTML Placeholder with class in Laravel 4

While using HTML Placeholder with class in Laravel 4, shows me this error.
Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected ';'
I've used
{{ Form::text('name', null, array('class' => 'form-control', 'placeholder' => 'Your Name') }}
You didn't close your parentheses () for Form::text() .
{{ Form::text('name', null, array('class' => 'form-control', 'placeholder' => 'Your Name')) }}