Laravel 4 Form Text Input - php

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 :)

Related

Laravel IF Multiple values in Cookie

I have a cookie in Laravel and a variable from my server.
I want to validate the result of my server's variable with the result of the cookie.
Normally I would use this but the detail is that my server's cookie has multiple values separated by commas and inside one of them is the value I need.
#if (Cookie::get('DeliveryToken') == '{{ $orderitemaddons32 }}')
{{ Cookie::get('DeliveryToken') }}
#else
No se encontro
#endif
How do I get it to detect the comma-separated value?
I give you an example.
My variable to compare
"{{ $orderitemaddons32 }}" value = DEL-43421
"DeliveryToken" value: delton,DEL-43421,hp-32
As you can see the exact value after the first comma I need the if to parse all the values after the comma.
Or if it is in the first one to stop there and give the result of the if
A simple inline method for doing this would be convert your comma separated string into an array using explode().
Then check that the value you expect is inside that array with in_array().
For example:
in_array($check_value, explode(',', $cookie))
If this is something you frequently do, you may want to create a service to put this logic and other cookie based manipulations in.

Put a Max value in time input (Laravel Collective)

Does anyone know how to put a maximum value for a time input in Laravel Collective?
I have tried this but it doesn't work:
{!!Form::time('tiempo[]', 'max="04:00"')!!}
Pass your options as the third parameter in an array. The second argument is the value of the input.
{!! Form::time('tiempo[]', null, ['max' => '04:00']) !!}

How to pass input text with commas through laravel form?

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.

Symfony form.vars.data vs form.vars.value

A FormView object in Symfony contains several variables, that can be accessed via twig using the public vars property.
Two of those variables are value and data.
So, supposing that we have our form variable in twig, we can access them using form.vars.data and form.vars.value.
The documentation is clear about the meaning of those properties:
value: The value that will be used when rendering (commonly the value HTML attribute).
data: The normalized data of the type.
but when I use {{ dump(form.vars) }} and compare form.vars.value and form.vars.data they look like identical. Why? What's the correct meaning and proper usage of those two properties?
Take for example a DateType field.
Here, value would be something like the string 2016-06-10.
data on the other hand would be a corresponding DateTime-Object.
When using Text fields, you will not see any difference because in both cases there will be just a string.

Laravel validation double nested

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.

Categories