I have a many to many relation between two entities.
I display then a form to add entityA to entityB.
Isn't possible to add a customized form (I mean in the twig view) in order to enable the user sometimes to select one value and sometimes more than one?
when I want the user to select more than one value, I use this
<select multiple>
{% for entity in entitys %}
<option>
{{entity.id}}
</option>
{%endfor%}
</select>
otherwise this
<select >
{% for entity in entitys %}
<option>
{{entity.id}}
</option>
{%endfor%}
</select>
but now the problem is how to submit the form. the
<button type="submit" class="btn btn-info" value="NEXT STEP " />
Here is the whole form
<form method="post">
<select >
{% for entity in entitys %}
<option>
{{entity.id}}
</option>
{%endfor%}
</select>
<input type="submit" />
</form>
no longer submit the form. any ideas plz??
here is my entire twig view
<h2> STEP {{step}} </h2>
<form method="post">
<select >
{% for entity in entitys %}
<option value="{{entity.id}}">
{{entity.id}}
</option>
{%endfor%}
</select>
<input type="submit" class="btn btn-info" />
</form>
<br>
<br>
In your formbuilder you can add some options like in my exemple:
The aim is to map your field to an entity (to setup the list). Don't forget to add a method _tostring to your mapped entity to make symfony able to represent your entity as a text in your select.
In your formType
public function buildForm(FormBuilder $builder, array $options) {
$id = $this->id;
$builder->add(
'addressees',
'entity',
array(
'class' => 'Pref27\MailBundle\Entity\Addressee',
'property' => 'name',
'multiple' => true,
'expanded' => false,
'required' => true,
'label' => 'mail.add.theme';
}
)
);
}
In your formControler
$editForm = $this->createForm(new FormType(), $entity);
return array(
'form' => $editForm->createView()
);
In your view
<form action="{{ path('theControllerActionWitchIsResponsibeOfRecordingIntoDatabase' }}" method="post" {{ form_enctype(edit_form) }}>
{{ form_widget(edit_form) }}
<p>
<button type="submit">Next step</button>
</p>
</form>
The type of field rendered will depend on setting of multiple and expended
select tag expanded=false multiple=false
select tag (with multiple attribute) expanded=false multiple=true
radio buttons expanded=true multiple=false
checkboxes expanded=true multiple=true
you can find more information about entity type in form here : http://symfony.com/doc/2.0/reference/forms/types/entity.html
EDIT:
From your twig view form's action is missing
try to add
<form method="post" action="{{ path("theRouteOfYourControllerWitchRecordTheData")}}">
don't forget to add {{form_rest(form) }} To tell twig to add the CSRF token
and don't forget to add value in your select's option
<select multiple>
{% for entity in entitys %}
<option value="{{entity.id}}">{{entity.name}}</option>
{%endfor%}
</select>
Related
I am trying to push multiple selections from a drop down of specialties being displayed from my $specialties variable in an #foreach in a laravel application. I am able to capture 1 id and push it to the database, but multiple selections do not work. It only pushes one ID to the database. When I try name="specialties[]" as below I get error: Array to string conversion. How do I push my selection into an array using eloquent?
My form View:
<div class="container">
{{ csrf_field() }}
<strong>Select Specialty:</strong>
<select id="multiple-checkboxes2" multiple="multiple" name="specialties[]" value="specialties">
#if($specialties)
#foreach($specialties as $specialty)
<option value=" {{$specialty->id}} ">{{$specialty->name}}</option>
#endforeach
#endif
</div>
</select>
{!! Form::close() !!}
<script type="text/javascript">
$(document).ready(function() {
$('#multiple-checkboxes2').multiselect();
});
Client Store Controller
public function store(Request $request)
{
//
Client::create([
'specialties' => $request->specialties,
] );
return redirect('/accounts');
}
Client display controller
public function display($id)
{
$specialties=Specialty::select( 'name', 'id')->orderBy('name', 'asc')->get('id');
return view('accounts/display', compact('accounts', 'specialties', )->withAccount($accounts);
}
}
try to use array
<select id="multiple-checkboxes2" multiple="multiple" name="specialties[]">
#if($specialties)
#foreach($specialties as $specialty)
<option value= "{{ $specialty->id }}">{{$specialty->name}}</option>
#endforeach
#endif
</select>
Hi so I'm trying to change my forms for creating an appointment, I currently have a foreach loop for radio buttons and it works but ofcourse it doesn't look great. So i'm trying to instead use a select menu so it look better, but when I try submit my new form it tells me the doctor field must be selected, so it's not getting the actual value of what's selected.
addappointmentform.blade (OLD VERSION as stated above this works but looks bad)
<fieldset>
<legend>Select the Doctor</legend>
#foreach($doctors as $doctor)
<div>
<label for="dirBtn{{$doctor->id}}">
<input id="dirBtn{{$doctor->id}}" type="radio" name="doctor" value="{{$doctor->id}}">Dr
{{$doctor->surname}}
</label>
</div>
#endforeach
</fieldset>
addappointmentform.blade(NEW VERSION, this is what i'm trying to get working)
<fieldset>
<select name="doctorsSelect">
<option selected disabled>Please select a Doctor</option>
#foreach($doctors as $doctor)
<option value="{{ $doctor->id }}"
name="doctor" input id="dirBtn{{$doctor->id}}" value="{{$doctor->id}}">
Dr {{$doctor->surname}}</option>
#endforeach
</select>
</fieldset>
AppointmentController
function addAppointmentDatabase(Request $request)
{
$this->validate($request, [
'time' => 'required|',
'date' => 'required|min:10|max:10|filled',
'doctor' => 'required',
'user' => 'required',
]);
//create a appointment
$appointment = new Appointment();
$appointment->time=$request->time;
$appointment->date=$request->date;
//assign doctor to appointment
$doctor = Doctor::find($request->doctor);
$appointment->doctor()->associate($doctor);
$user = User::find($request->user);
$appointment->user()->associate($user);
//save the appointment
$appointment->save();
return redirect('all');
}
You don't need an id for the option, Also your select needs to have the name of the previous input.
Something like this:
<fieldset>
<select name="doctor">
<option selected disabled>Please select a Doctor</option>
#foreach($doctors as $doctor)
<option value="{{$doctor->id}}">Dr {{$doctor->surname}}</option>
#endforeach
</select>
</fieldset>
Hope this helps.
Your select name is doctorsSelect, change it to doctor:
<select name="doctor">
I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}
I need to add a custom class to my select tag (<select>) generated by an FormType with symfony2.
All options are generated by Symfony
$builder->add(
'birthday',
null,
array(
'label' => 'form.birthday'
, 'translation_domain' => 'FOSUserBundle'
)
)
and the generate code is:
<div id="fos_user_registration_form_birthday" placeholder="Mot de passe" class="form-control">
<select id="fos_user_registration_form_birthday_day" name="fos_user_registration_form[birthday][day]">
<option value="1">1</option>
...
</select>
<select id="fos_user_registration_form_birthday_month" name="fos_user_registration_form[birthday][month]">
<option value="1">jan</option>
...
</select>
<select id="fos_user_registration_form_birthday_year" name="fos_user_registration_form[birthday][year]">
<option value="1994">1994</option>
...
</select>
</div>
And, I need to add a custom class to this:
<select id="fos_user_registration_form_birthday_day" name="fos_user_registration_form[birthday][day]">
I've tried this solution in my html content:
{{ form_widget(form.birthday, {'attr': {'placeholder': 'form.password'|trans, 'class': 'test'}}) }}
But the test class is added to my div (<div id="fos_user_registration_form_birthday" placeholder="Mot de passe" class="form-control"></div>), not to my select tag.
Any suggestions?
In your Form class, you can set the class using the attr option.
You can already do it inside your Form Type, or later in your template, like you just did.
It PROBABLY does not use your 'test' class, because the birthday widget does not use it. Please search your whole project for "birthday_widget" and see if it uses a hardcoded form-control class. If it does, you can write an if statement checking if attr.class is definer or add something like this:
{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}
Edit: Can you also try rendering each day,month and year separately (I'm not sure if that will work, because I do not know the birthday type), like so...
form_widget(form.birthday.day, {'attr': {'class': 'test'}})
Another thing you can try is to edit your Form Type.
Try to add this after you add method:
$builder->get('birthday')->get('day')->setAttribute('class', 'test'); // Also try with ->get('days')
I'm almost sure something above will work.
I'm fairly new to Symfony2 and am working with the form builder trying to create a dropdown menu from the database. I can populate the dropdown without problem but each options value attribute just gets set to a number when it needs to be set to either the options text or no value attribute but I can't find anything in the documentation about setting the value.
$builder->add('institution', 'entity', array(
enter code here 'class' => 'JacksonFramesStoreBundle:Institution',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('i')
->where('i.type = :type')
->setParameter('type', 'University');
},
'empty_value' => 'Select:',
'empty_data' => null,
));
This outputs:
<select id="selectUniversity" name="selectUniversity">
<option value="0"></option>
<option value="1">Australian Catholic University</option>
<option value="2">Australian National University</option>
<option value="3">Bond University</option>
</select>
EDIT - this is the relevant section of the twig
<!-- University Panel -->
<div id="uniPanel" style="display:none;">
<p>{{ form_label(form.institution) }}<br />
<span class="inputLine">
{{ form_errors(form.institution) }}
{{ form_widget(form.institution) }}
</span>
</p>
</div>
In the controller, after if ($form->isValid()), try using:
$value = ($entity->getInstitution()) ? $entity->getInstitution()->getId() : null);
I don't really know if I really understood your question