In Laravel 4 I can create text inputs with code like:
{{ Form::text('title', '', ['placeholder' => 'Ex. title', 'id' => 'title']) }}
This is very useful, because if other fields of my form fail validation, I can just do...
return Redirect::back()->withErrors($validator)->withInput(Input::all());
And when the form is shown again, the field title will conserve the value that was submitted before.
Good stuff.
Now here is my problem: What about input arrays?
I added the following input fields to my form:
<input type="text" name="option[]" />
<input type="text" name="option[]" />
<input type="text" name="option[]" />
<input type="text" name="option[]" />
And I submitted the form, knowing that would fail, to see if when the form appears again, the values on these fields still remained. Instead Laravel threw an exception.
ErrorException
htmlentities() expects parameter 1 to be string, array given
Even if I have just 1 option, it still gives me the same error:
{{ Form::text('option[]') }}
Now, I know that I could handle the filling of the array option manually, maybe like this:
$options = Input::get('option');
return Redirect::back()
->withErrors($validator)
->withInput(Input::except('option'))
->with('options', $options);
and then just looping through the $option array on the blade template.
Yes, that would probably work. But that is kinda hacky, and it still misses the powerful workflow of Laravel to autofill fields after failed submission, like I did back with title. So my question is:
Is there any built-in way in Laravel 4 to handle Input Arrays under these situations?
If you add an index to each input like:
{{ Form::text('option[1]') }} {{ Form::text('option[2]') }} it will work.
You can see how this works in vendor\laravel\framework\src\Illuminate\Html\FormBuilder.php in the function that creates the input
Related
I have a simple form in react, which lives in a modal. If a user was to use autofill for an email field for example, it would update other fields including fields that I've already filled in. This would lead users to submitting data, not knowing that fields out the view have been updated.
I've tested this in non-react forms and Google Autofill works fine, in that it would not overwrite existing values in fields. But in react lets say I inserted firstname = john, and then use autofill on the email...it would over 'John' and use whatever is saved in Autofill.
Is anyone aware of a way around this? I'm not going to turn autocomplete off as I still want users with the ability, anyway I've tried variations of autocomplete=off as suggested else where but still no result
You can use autocomplete="off" in your input that you do not wish to autofill.
Please also make sure your input types are correct.
example: <input type="text" name="foo" placeholder="foo" autocomplete="off">
You can even do this using JS:
inputElm.setAttribute( "autocomplete", "off" );
as an example.
regards
Aaron
Try to create hidden input right before your input and add random number for your original input name where you don't want Chrome to autofill values:
<input type="text" name="" value="" readOnly={true} style={{display: "none"}}/>
<input
type="text"
name={"address " + Math.random()}
/>
This may seem obvious to some of you, but I really am struggling to find a straight answer. I've generally googled, as well as read both the CakePHP manual and the API for an answer to the following question:
When creating an input, the following code creates the following outputs:
// in the view
echo $this->Form->input('notes');
// resultant html
<div class="input textarea">
<label for="notes">Notes</label>
<textarea id="notes" rows="5" name="notes"></textarea>
</div>
Note: this is consistent across most input types; and because it's consistent it's great for formatting.
However, with a checkbox:
//In the view
echo $this->Form->input('ticket_required',
['type' => 'checkbox']
);
// resultant HTML
<div class="input checkbox">
<input type="hidden" value="0" name="ticket_required">
<label for="ticket-required">
<input id="ticket-required" type="checkbox" value="1" name="ticket_required">
Ticket Required</label>
</div>
[Note: I understand the need/desire for the hidden field]
Now.. surely it can't be an uncommon requirement to simply want the same format approach as every other standard input?
My question - how do I make CakePHP create a checkbox element as follows:
// desired HTML
<div class="input checkbox">
<input type="hidden" value="0" name="ticket_required">
<label for="ticket-required">
Ticket Required</label>
<input id="ticket-required" type="checkbox" value="1" name="ticket_required">
</div>
To be clear: the order of visible elements is the same as other generated elements (label before input, and all encased in the wrapping div).
Please note.. i have tried the 'nestedInput' => false option. This actually gets rid of the checkbox input entirely from the div.
I can't understand why this isn't done that way... but even if it was, I can't fathom why this isn't an obvious question for the documentation.
Oh well.. hopefully someone can help me here.
Thanks in advance.
Rick
I would have thought the nestedInput would work but even if it did, you don't want to add that to every input you create throughout the website.
CakePHP 3 uses string templates to build form controls. You can modify them to suit your needs.
By default the checkbox is using the nestingLabel template so if you want to stop all inputs from being nested you can change the template.
// src/View/AppView.php
$this->loadView('Form', [
templates => [
'nestingLabel' => '<label{{attrs}}>{{text}}</label>{{hidden}}{{input}}'
],
// [More helper default config overrides][2]..
])
For more control over your helpers you can create your own that extends one of the core helpers.
I'm trying to solve an issue with formatting some input fields.
When I enter the value I want the model to updated as I type, like this:
I enter ex '1200000', i want it to display in the input field '1.200.000' and the model should update as 1200000.
I also want the field to pre-formatted if the model already has a value.
Likewise I need to format in percentage, please see my example:
<div ng-controller="testController as testCtrl">
{{ 'Model: ' + testCtrl.value1 }}
<input type="text" cr-numeric ng-model="testCtrl.value1" /> <br />
{{ 'Model: ' + testCtrl.value2 }}
<input type="text" cr-numeric="interest_3_decimals" ng-model="testCtrl.value2" />
The formatting work fine when I'm entering the values, but I can't make it to work when the model already has a value and to update to value accordingly when i'm entering data.
Please anyone help.
If You need format only in view, You can use number filter;
Just:
Model: {{ testCtrl.value1 | number:2 }}
So, in model You still have original value;
I have a form that POST all data that is entered, when saving. But when I refresh the page, not all entries are bound to their respective input fields.
It is a bit strange because I am using ng-model on all the fields.
Here is an example of what doesn't bind:
<input name="full_name" ng-model="user.full_name" type="text" required></input>
and here is one that does bind:
<input name="address" ng-model="user.address" type="text" required></input>
Has anyone run into this issue, or notice something I may be missing?
It could be the browser remembering your last input in the forms. So after refreshing, the browser pre-populates the form and angular doesn't update the scope. There is a Google Groups thread about that. The best solution I found is to add autocomplete="off" in the inputs of the forms. Because after refreshing, there is no way angular could be remembering your last input in the form, unless you are using cookies for that wich you are obviously not.
I am implementing an auto complete box using the Ajax.autocompleter method of the scriptaculous.js framework.
This is the auto complete box and the div where the auto suggested entries are populated.
<?php echo $form->create('Share', array('url' => '/forms/share')); ?>
<label for="shareWith">Share Form with</label>
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" class="autocomplete"></div>
<input type="hidden" id="sharedUserId" name="sharedUserId"/>
<?php echo $form->end('Share');?>
This is the JQuery function to get the auto-suggested list and to get the id of the selected entry which is stored in the hidden field of the form.
new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
"http://localhost/FormBuilder/forms/autoComplete",
{
tokens: ',',
afterUpdateElement : getSelectedId
}
);
function getSelectedId(text, li) {
$("#sharedUserId").val(li.id);
}
Suppose if I select multiple entries,how to send those values?
Can I have an array as a hidden field, so that I can have an array of the selected elements and save that array as a hidden field?
Simply create a new hidden input field for every selected ID, and make sure that for each you have name="sharedUserId[]". This doesn't follow the CakePHP form element naming convention, but it will make sure that the POSTed value of sharedUserId is an array.
serialize with json and parse it in the server back. PHP 5.2 can parse json natively.
Not related to your question though..
http://docs.jquery.com/Plugins/Autocomplete
jQuery Auto complete you