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.
Related
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').
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.
I have an HTML array of 10 select fields that must be populated by jQuery each time the div-popup is called. (Each one gets the same options) We have periodic needs for hundreds of employees and this form is intended to allow requests staffing to be forwarded to upper management for approval in a batch fashion by department. I’ll be using .serialize to submit the form but I am unclear as to how to initialize this element with the proper information for input selection. All the info I could find was about submitting the form array, which I already knew how to do.
Here is the HTML structure:
<select id="detJobsCR[]" name="detJobsCR[]"> </select>
And here is my attempt to populate the selection fields:
$.post("events.php",{a: 'detadd-joblist', dept_id: deptID}, function(data) {
for(var i=0;i<10;i++){
$('#detJobsCR[i]').html(data);
}
});
(I tried explicitly defining each elements array position [0],[1]..etc and that did not help.)
The data returned is simple HTML like:
<option value='38'>Admin Support Assistant I</option>
<option value='39'>Admin Admin Support Assistant II</option>...
Thanks for whatever assistance you can offer!
FYI Update: Musa's answer worked perfectly. I also had issues with Date & Time pickers not working, it turns out, for the same reason. When I converted them to classes I was able to use this(below), and the are all now working as well:
$(".detDateStart").datepicker();
Ids should be unique so each select should have its own id. If you want to have one identifiier to represent all the selects you could use a class e.g. <select class="detJobsCR" name="detJobsCR[]"> </select> and then select them with $('.detJobsCR') and set their html with .html(data)
$.post("events.php",{a: 'detadd-joblist', dept_id: deptID}, function(data) {
$('.detJobsCR').html(data);
});
Your problem is two fold:
You're putting i inside your quotes, so it's not interpreting the value of the variable i
Even if i wasn't inside the quotes, when jquery parses the [i] in the expression it's looking for elements with id detJobsCR and with attribute i defined.
Since you have only ONE element, with ID detJobsCR[] you would query it like this: $('#detJobsCR\\[\\]') You need to escape the [ and ] character so jquery knows it's a part of the ID and NOT a parsing rule (for parsing/looking for an attribute).
As suggested by Musa, you can make your life much easier by using a simple class name that doesn't require escaping values. But if you do want to select by id, the example above should do the trick.
Im looking for a way to have a form in cakephp that the user can add and remove form fields before submitting, After having a look around and asking on the cake IRC the answer seems to be to use Jquery but after hours of looking around i cannot work out how to do it.
The one example i have of this in cake i found at - http://www.mail-archive.com/cake-php#googlegroups.com/msg61061.html but after my best efforts i cannot get this code to work correctly ( i think its calling controllers / models that the doesn't list in the example)
I also found a straight jquery example (http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/) which does what i would like my form to do but i cannot work out how to use the cakephp form helper with it to get it working correctly and to get the naming correct. (obviously the $form helper is php so i cant generate anything with that after the browser has loaded).
I an new to cake and have never used jQuery and i am absolutely stumped with how to do this so if anyone has a cakephp example they have working or can point me in the right direction of what i need to complete this it would be very much appreciated.
Thanks in advance
I would take the straight jquery route, personally. I suppose you could have PHP generate the code for jquery to insert (that way you could use the form helper), but it adds complexity without gaining anything.
Since the form helper just generates html, take a look at the html you want generated. Suppose you want something to "add another field", that when clicked, will add another field in the html. Your html to be added will be something like:
<input type="text" name="data[User][field][0]" />
Now, to use jquery to insert it, I'd do something like binding the function add_field to the click event on the link.
$(document).ready( function() {
$("#link_id").click( 'add_field' );
var field_count = 1;
} );
function add_field()
{
var f = $("#div_addfield");
f.append( '<input type="text" name="data[User][field][' + field_count + ']" />' );
field_count++;
}
Of course, if a user leaves this page w/o submitting and returns, they lose their progress, but I think this is about the basics of what you're trying to accomplish.
This was my approach to remove elements:
In the view, I had this:
echo $form->input('extrapicture1uploaddeleted', array('value' => 0));
The logic I followed was that value 0 meant, not deleted yet, and value 1 meant deleted, following a boolean logic.
That was a regular input element but with CSS I used the 'display: none' property because I did not want users to see that in the form. Then what I did was that then users clicked the "Delete" button to remove an input element to upload a picture, there was a confirmation message, and when confirming, the value of the input element hidden with CSS would change from 0 to 1:
$("#deleteextrapicture1").click(
function() {
if (confirm('Do you want to delete this picture?')) {
$('#extrapicture1upload').hide();
// This is for an input element that contains a boolean value where 0 means not deleted, and 1 means deleted.
$('#DealExtrapicture1uploaddeleted').attr('value', '1');
}
// This is used so that the link does not attempt to take users to another URL when clicked.
return false;
}
);
In the controller, the condition $this->data['Deal']['extrapicture1uploaddeleted']!='1' means that extra picture 1 has not been deleted (deleting the upload button with JavaScript). $this->data['Deal']['extrapicture1uploaddeleted']=='1' means that the picture was deleted.
I tried to use an input hidden element and change its value with JavaScript the way I explained above, but I was getting a blackhole error from CakePHP Security. Apparently it was not allowing me to change the value of input elements with JavaScript and then submit the form. But when I used regular input elements (not hidden), I could change their values with JavaScript and submit the form without problems. My approach was to use regular input elements and hide them with CSS, since using input hidden elements was throwing the blackhole error when changing their values with JavaScript and then submitting the form.
Hopefully the way I did it could give some light as a possible approach to remove form fields in CakePHP using JavaScript.
I have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.
So my question is, how do I display information using Zend_Form that a user can't edit?
Thanks.
readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.
Finally, if you would like your element to be displayed in a different way than just with readonly, you can do that by changing the element decorators.
I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.
Thanks for your answers.
That's very good solution when you don't need to populate the element value when the form is submitted.
It's equivalent solution is to use the Form Element method setAttrib() and disable the form element
$formElement->setAttrib('disable','disable')
which will only freeze the element.
But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.
$formElement = new Zend_Form_Element_Hidden( 'elName',
array( 'label' => 'elLabel', 'value' => 'elValue' ) );
$formElement->setDescription( 'elValue' );
Then you can render that hidden element and display the value with the
$formElement->getDescription().
$element->setAttrib('readonly', 'true');
http://www.w3.org/TR/html401/interact/forms.html#adef-readonly
According to Amr Mostafa, if you use:
$element->setAttrib('readonly', 'true');
OR
$element->setAttribs(array('disabled' => 'disabled'));
User still send values by POST/GET and they are stored in DB.
The only way for me to don't taking into account the values from POST/GES is:
$element->setIgnore(true)
Example:
$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);