L4: populate multiple text inputs - php

I'm working on an Laravel 4 project and we have an view with an dynamic form.
Form::text('title')
<ul>
<li>Form::text('movie_actor[]')</li>
<li>Form::text('movie_actor[]')</li>
<li>Form::text('movie_actor[]')</li>
...
</ul>
I've read that you need to set the validation for the multi fields to array. So I added the validation rules like so:
$v = Validator::make(Input::all(), array('title' => 'required', 'movie_actor' => 'array'));
When I enter some actors, leave the title empty and submit the form, then the user is redirected to the same page with:
return Redirect::route('movies.create')->withInput();
The problem is that I get the error htmlentities() expects parameter 1 to be string, array given.
The population works when I remove the actor inputs from the view or change the actor text fields to selects fields. But that is not what I want.
How can I populate the multi-text fields?

I had same issue too. I haven't figure out exactly why its happen but looks like those text input name should be "string" not array which means, you should do like follow:
<li>Form::text('movie_actor[0]')</li>
<li>Form::text('movie_actor[1]')</li>
<li>Form::text('movie_actor[2]')</li>
The differences are you need to manually add an index to those name array.

Related

Getting dynamic input field in laravel controller

In normal situation we get input fields values like $request->input('input_name') but in my case my input names are dynamic and can be anything (comes from database).
I want to know how can I get data of such inputs?
Code
my form input is like:
//1
<input type="radio" value="{{$opt->title}}" name="{{$opt->group->title}}">
//2
<select name="{{$opt->group->title}}" class="form-control">
//3
<input class="options-checkbox" type="checkbox" value="{{$opt->title}}" name="{{$opt->group->title}}[]">
so each of this inputs has different names based on database, and i can't predict their names in controller in order to get their values.
Test DD
array:5 [▼
"_token" => "QBY0WqF2WdqALxks22zjqpuBwplviHStBzTqzFzD"
"COLORS" => "blue" //1
"tester_group3ffhshg" => "title 33" //2
"radio_group" => array:1 [▼
0 => "hi" //3
]
"quantity" => "1"
]
Is there anyway to get data from such inputs?
I'm not going to answer in code (Meaning, I won't re-create the whole code) but I can give you the most optimal logic that you can use to solve this problem.
First of all, As your input fields are dynamic, You must have something in frontend controlling these inputs. At least, Javascript. If not, you'll require it.
Once you have Javascript in the frontend, On load and also whenever a user takes an action that might alter your fields, Update an array variable containing the names of those fields. For example, If you have fields named field_1 and field_2 on page load, add those field names to one array called request_fields.
After that, update it every time there is some change with fields. After some changes, let's say now you have three fields that you will require in your controller named field_23,field_34 and field_54 and you are updating it with every action.
Now, On form submit action, just append this array of the field that you require in your POST request of the form and you are good to go! Then, you can scan this array in the backend and get the request data as you desire!
A quick example on the controller might look something like this:
foreach($request->requiredFields as $field){
echo $request->{$field};
}
This will solve your problem very easily as it's easy to implement and also it's easy to understand! Let me know if you have more questions.

laravel - get attribute from html tag

So I have a form in my view:
{{Form::file('projectPicture', ['class' => 'uploadedImage', 'data-some-attribute' => ''])}}
with the attribute data-some-attribute.
And in my route I retrieve it like so:
$request->file('projectPicture');
How do I get a data-some-attribute in the route? Is it even possible?
I know I can use ajax to pass any data, but can it be avoided in this case?
Thank you!
It is not possible how you intend it to work, only because it's not how form data is working under the hood. The second argument in your sample Form::file is just decorating the rendered form element. It has no correlation with the form data that is transferred between the server and client.
For all intents and purposes form data is just a glorified set of key value pairs. If you wanted to pass some-data-attribute to your route controller, you have two options -
Add another form field, and make it empty using Form::hidden. In this case, you would just name the field some-data-attribute.
If your form is submitted through a POST method, you can tack on some-data-attribute onto the form's route and retrieve it from the request.
ie - your/route becomes your/route?some-data-attribute=whatever, and you can retrieve it later with something like $request->input('some-data-attribute').

how to get id in url and display it in a form laravel 5

i have this href that leads you to addeditvisit/.(the chosen row's id)
<td><img src="images/visit.png" style="width:30px; height:30px;"></td>
so the url looks something like this http://localhost:8000/addeditvisit/5
and inside it is a form. where i want to include the $patient->pID and make it a value of my hidden input so that i can include it saving the form.
in my controller I coded this:
$uri = $request->path();
return View::make('pages.addeditvisit', ['uri'=>$uri]);
in my form i have this input text first instead of hidden to display what really is the value and it is displays
addeditvisit/5
what i really want is to only get the 5. what function should i use? Please help me, i'm just new in Laravel 5 and still learning
You can pass your whole Patient object if you want, like this:
$patient = Patient::find($id); // get it from db, or some other data source
return View::make('pages.addeditvisit', ['uri'=>$uri, 'patient' => $patient]);
`
and then in your form you can use the
$patient->pID
or you can just pass the id itself, if you want to have only that:
return View::make('pages.addeditvisit', ['uri'=>$uri, 'pID' => $patient->pID]);

array filed values not working in Laravel blade template system?

I have 2 email fields and I am using
{{ Form::text('email[]', Input::old('email'),array('class' => 'large-2', 'placeholder' => 'email address','id'=>'email')) }}`
if I use [] to get multiple values for same variable, it is giving error in view page if posted back some data. for example if some fields are mandatory and if user fail to fill those, page will be redirected to same view page from where it was launched.
In such cases it is showing error.
How to fix this issue?
One text field can contain only one data. So, you need another text field to achieve that. if you don't want to show the multiple emails, you can use hidden fields.
{{ Form::hidden('email[]', Input::old('email1'))
{{ Form::hidden('email[]', Input::old('email2'))
Or you can use select. Laravel allow array data if use select field.
For example:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small')) }}
http://laravel.com/docs/html#drop-down-lists

Symfony2: string values from choice fields not bound to form object

My app contains a form with three choice fields / dropdown lists.
The first is linked to a "relational" field in the entity, and works fine.
The user is supposed to choose a table link first.
The second and third are both linked to string fields in that same entity.
Through jQuery, these choice fields get populated with values based on the decision the user makes in the first dropdown list.
After submitting the form, there is an error for the second and third field: they contain invalid input. When I check the form object, their values were not bound; their values never arrived.
The choice fields for collecting string data from the user looks like this:
->add('sourceName', 'choice', array
(
'label' => 'Choose source name:',
'empty_value' => 'Please choose a table link first...',
'choices' => array(),
'attr' => array('class' => 'extFieldChoice'),
)
After jQuery has done its job, the html select element looks like this:
<select id="someId" name="someName[sourceName]" required="required"
class="extFieldChoice">
<option value="first">first</option>
<option value="second">second</option>
<option value="manymore">Many more...</option>
</select>
I suspect that the error can be found in the initially empty choices array. However, it would be impossible to fill it with all possible choices, because they run in the hundreds.
I've the same problem few days ago and it drive me nuts to find a solution, I get to this question and even probably it's too late I want to share the solutions I've found in case there'll be someone with the same problem, I have found three of them, but none of them seems to be the perfect solution.
In my case I have to save the city selected by an user based on the zipCode. When a user save a new address he wrotes the zipCode and then via ajax I fill-in the city select with options of the cities.
My "choice" field declaration:
$builder->add('city', 'choice', array(
'label' => 'form.city',
'read_only' => true,
'mapped' => false,
'required' => false,
'empty_value' => false,
'choices' => array('none' => 'form.empty.city'),
'data' => null
));
The problem is that the form validation look for two things:
If the form it's related with an entity looks forward the entity
to validate, you can skip this validation easily with the "mapped"
=> false parameter.
The form validation itself, if you have a "choice" type field with or without choices defined when the form validate look towards the first choices you have declared. And I haven't been able to skip the validation for only this field.
So the three ways I have found:
Use the form event and before whe bind the request to the form $builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent $event) use ($xxx) { ...}); (I have an example of use at the linked article) we make the same query to the database we have made on the ajax, we retrieve the values and add them to the chocie field. This is the better way if we think in code, but we have to do the same query twice, and I don't want to do that.
In my case I have to store a string with the city name so another option is to add the city field as a hidden one and insert the whole select as an element. But I don't like this one for many reasons, two of them: I don't like the idea of insert a hole , with and between the other form fields created by symfony2; the other one is that it requires more jquery that it's necessary from my point of view.
Based on the second option, I was filling in some other hidden fields based on the city selection the user has made, so I only include one more hidden field to save the city name; and when the form is submited I remove all the options from the select, so it matches the choices I have defined.
$('#cityChoice').on({
change: function(){
var optionSelected = $(this).find('option:selected');
$('#city').val(optionSelected.val());
$('#latitude').val(optionSelected.data('lat'));
$('#longitude').val(optionSelected.data('lng'));
}
});
$('#myForm').on({
submit: function(){
$('#mySelect option').remove();
}
});
I've decided for the third option, but I think the three of them have bad sides.

Categories