I'm having issues with double nested validation
My form is rather large and contains some nested data. Two of the fields:
{!! Form::text('address[city]', null, [] !!}
{!! Form::text('address[country[printable_name]]', null, [] )) !!}
For example this works:
'address.city' => 'required|max:255',
but
'address.country.printable_name' => 'required|max:255|country
throws "The address.country.printable name field is required." even though it has a valid country.
If I try to print all with $request->all() I get the following:
...,"address":{"city":"Maribor","country[printable_name":"Slovenia"},...
So there is missing ] after printable_name.
If I try to print
$request->input('address.country.printable_name')
I don't get anything, but it works when I try this:
$request->input('address')["country[printable_name"]
Did I do something wrong, is this not supported in Laravel or a bug? Either way, how can I get it work?
A workaround would be this
'boat.country[printable_name' => 'required|max:255',
but if I leave this the next developer to look at the code will probably want to kick my ass.
If you want to nest array items in request parameters you should do it like this:
{!! Form::text('address[country][printable_name]', null, []) !!}
Then you can access them as you've initially tried:
$request->input('address.country.printable_name')
Just think of structuring it as you would access it in an associative array in PHP. If you pass a parameter with this name in your form:
address[country][printable_name]
Then using plain PHP you would access it like this:
$_REQUEST['address']['country']['printable_name'];
The above example illustrates the equivalent structure.
Related
I would like to pass string input with commas through laravel form text field. You can provide multiple numbers separated by commas, the application will handle imploding into array.
The issue is that laravel (or web browser) by default will change comma to '%2C'. I know it's a safety feature, but this is not too big concern in case of this application.
Is there a way to disable this?
Already tried to use {!! !!} instead of standard {{ }}
{{ Form::text('number', null) }}
Try using POST method in form tag instead of GET method.
It will not show field value in URL.
I'm using Laravel collective form builder for building forms. I have used select like this:
<div class="form-group">
{!! Form::label('module', 'Modul'); !!}
{!! Form::select('module_id', [$modules], $data->module_id) !!}
</div>
From the unknown reasons, the tag appears in my code. I don't want it there, but I'm really unable to find a simple way how to remove it. Thank you for a help
Reviewing the source code for the package and the tests we can see that the optgroup is included if the array passed is multidimensional, you can see that within this test.
Reviewing the code you've provided we can see that you're creating a new array containing $modules:
Form::select('module_id', [$modules], $data->module_id)
Which means that if $modules is already an array, you're creating a multidimensional array. This is what the select() method is receiving:
[
[
"a",
"b",
"c",
],
There's no key present so the array is keyed numerically, starting at 0, hence the optgroup label value is 0. You should be passing a single-level array if you want to have a single level of options in your select, e.g:
[
"a",
"b",
"c",
]
You can fix this by not nesting your array in another array, [$modules] becomes $modules:
<div class="form-group">
{!! Form::label('module', 'Modul'); !!}
{!! Form::select('module_id', $modules, $data->module_id) !!}
</div>
In the Laravel Docs on validation they speak of 'nested parameters':
If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
What would the HTML look like for this nesting? I googled around, and found nothing except things about form nesting. Also, the "dot" syntax, is this specific to Laravel?
The dot notation is for easily accessing array elements, and making their selectors more "fluent".
Validating author.name would be the equivalent of having checking the value of the input <input type="text" name="author[name]" />.
This makes having multi model forms or grouping related data much nicer =). You can then get all the data for that thing by doing something like $request->request('author'); and that would give you the collection/array of all the values submitted with author[*]. Laravel also uses it with its config accessors - so config.setting.parameter is the equivalent of config[setting][parameter]
Basically makes working with array data easier.
See https://github.com/glopezdetorre/dot-notation-access for some examples!
The Html form will look like Nothing More
Hello I'm currently working with Laravel 4 forms. I'm struggling to generate a text input with a specific class without choosing a 'default value'. I want to do the following:
{{ Form::text('first_name', array('class' => 'first_name')) }}
However I get this error (htmlentities() expects parameter 1 to be string, array given.) unless I add a default value:
{{ Form::text('first_name', 'Some Value', array('class' => 'first_name')) }}
The default value then populates the field and needs to be deleted before entering a new value. So it can't even be used like a place holder.
Thank you in advance,
Dan
Instead of a value, supply null. (do no supply empty string "")
This will come in handy in the future if you are going to work with Form Model Binding (http://laravel.com/docs/html#form-model-binding) because null will give the value of the given model attribute.
You can pass an empty value "" like,
{{ Form::text('first_name', '', array('class' => 'first_name')) }}
Because Laravel 4's HTML Form Builder API will accept first parameter as name, second parameter as value which is null by default and the third parameter as options array which is an empty array by default.
So basically you can build text input by passing only name like,
{{ Form::text('first_name') }}
And if you are planning to pass options which is the third argument, you must pass second argument also.
See API Doc here http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#235-246
I found it better to use the Input::old('first_name') for your default value instead of just "", like:
{{ Form::text('first_name', Input::old('first_name')) }}
This way, if you go back to the form with invalid data and pass the form input, it will reload the old input that the user previously inputted. In this case, first_name is/can be bound to the first_name field in your database table as well.
Edit: Yes, the third option is an array of input options, such as text field id, size, etc. or any other HTML attribute.
Keenan :)
I have a render array for a form in my D7 module that looks like this:
'form' =>
array
'#id' => string 'demo-form' (length=9)
'#action' => string '/sprint07/' (length=10)
'name' =>
array
'#title' => string 'Username' (length=8)
'#maxlength' => int 13
'#size' => int 15
'#type' => string 'textfield' (length=9)
'#required' => boolean true
'#input' => boolean true
'#autocomplete_path' => boolean false
... snip ...
I'm using twig-for-drupal to create a theme, but I can't seem to get the values back, I've tried {{ form['name']['#type'] }}, {{ form.name.#type }}, {{ form.name['#type'] }} and so on, but they all come back blank.
Other fields such as form['#action'] works fine, so how should I access the information more than one array in? I can't change the module too much as the php template in its native state uses render(form);
-- Update --
Added some more debugging to this, and seems that I can use {{ form|dump('v') }} to show me the whole array as above, but when I do {{ form.name|dump('v') }} I just get a string, which I assume means that twig is automatically detecting its a render array, and "helpfully" converting it for me.. So I guess that makes my question: how do I turn off the auto-rendering?
"name" sub array don't contain the "#id" key.
try if work
{{ form.name.#title }}
Turns out it isn't currently possible to do this on a case-by-case basis, TFD is currently set up to auto-render anything it thinks is a render array, and to turn this off you need to set autorender to FALSE in twig_get_instance(). I've tried this myself however and it still seems to be turning the array into a string, so this could be a bug within TFD, or I just don't understand the usage, which is equally, if not more, likely.
From ReneB's sandbox:
Autorender
This version of the TWIG engine uses auto render to prevent themers get RSI from typing {{node.field_somefield|render}} for every single field they want to render from the render array (of doom) so the can safely type {{node.field_something}}
On rendering of the compiled template TFD check if the called variable is a string, callable or array. If it's a string it simple does echo $string, if it's a callable it return a proper method() for it. And if it's an array, it assumes it's a renderable array and maps it to the render($string); method of drupal.
This way the objects hidden with hide() are respected.
To turn this is for now, you have to set autorender to FALSE in the twig_get_instance() method of twig.engine.
I'am working on a {% noautorender %} {% end noautorender %} block structure.