Put a Max value in time input (Laravel Collective) - php

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']) !!}

Related

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.

CakePHP form - remove month field name

I'm setting up a Stripe payment form, so I need to remove the names of my month and year fields so they aren't sent to my server. The following code though, still gives the field a name of '[month]' and if the text of the array's name variable were 'xyz', the field would be named 'xyz[month]'. How can I remove the entirety of the field's name?
echo $this->Form->month('expiration_month', array('name' => '', 'data-stripe' => 'exp_month', 'default' => 'January'));
According to the documentation, the name of the <select> element is derived from the first function argument ("expiration_month" in your example.) If you take a look at the code, you can see the value "month" is hard-coded.
The only way around this is to manually build your own <select> element, or just ignore the value when it comes to your server. But why make users fill out a form element that isn't going to be processed by your server?
So, the quick and dirty way around this is to find the select field with js and overwrite the name attribute.

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.

Laravel 4 Form Text Input

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

drupal7: using form_set_value()

I am trying to update a value during the validate phase of a node-form, i.e. if the custom validation error is fired, just empty one of the fields.
for the last 30 hours I am trying to make sense of the drupal api, but I am giving up. I just do not seem to get the idea what the different values mean.
the function is: form_set_value($element, $value, &$form_state)
now i understand that the last value is simply the $form_state, that I am having through the validate function. but what about $element and $value?
I was trying a lot and apparently the desired value resides in $form['field_name']['und'][0]['value']['#value'] and only there.
but when I am trying form_set_value($form['field_name']['und'][0]['value']['#value'],'foo',$form_state) it raises
Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /includes/form.inc on line 2436 and defined in drupal_array_set_nested_value()
and when I am trying:
$newvalue = $form['field_name']['und'][0]['value'];
$newvalue['#value']='foo';
form_set_value($form['field_name']['und'][0]['value'],$newvalue,$form_state);
it raises:
Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen()
Thanks for any help!
Unless I'm really mis-understanding what you're trying to do this will work:
$form_state['values']['field_name']['und'][0]['value'] = '';
When the form is re-built after a validation error the values in $form_state['values'] are used to re-populate the fields in the form. So, if you reset the value in the $form_state['values'] array it will not be there when the form is shown with the validation errors.
After a lot of debugging, I finally managed to make this work. The trick lies inside $form['complete form']. but first things first, how does form_set_value() work and what does it do?
the form_set_value() function
as the docs suggested:
if you want to update the value of $form['elem1']['elem2'], which should be stored in $form_state['values']['elem1']['elem2'], you would set $element['#parents'] = array('elem1','elem2').
now what does that mean? in my case, I had a textfield called 'field_event_title', which is the name I gave it on creation. in $form, all fields have a sub-array in $form['field_name'], which is in my case $form['field_event_title']. this is where the submitted value also is stored. now since it is a textfield, drupal maintains both the language and the delta [question for editors: is this right?] of the inputted data. so in fact, the value is not stored in $form['field_name']['value'], but in $form['field_name']['und'][0]['value'] (['und']=language; [0]=delta). note that 'und' is the drupal key for the default language of the site, if it is, say, in german, then it would be 'de', however, in most cases it should be 'und'.
to actually change the value using form_set_value(), one is ought to invoke the function by writing:
form_set_value($form['field_name'],array('und' => array(0 => array('value' => 'foo'))),$form_state);
i.e. $element = $form['field_name'] $value=array('und' => array(0 => array('value' => 'foo')))
updating a form to repopulate it with different values than submitted (or clearing them)
but that did not work in my case since I wanted to clear fields once a custom validation error has been invoked. now one would suspect that the form repopulates itself using the values inside $form_state['values'] (which is actually the place where the values are stored, the actual place that gets updated when using form_set_value() and the place which generates the $form later.), but that is not the case: it uses the values inside $form_state['complete form'], which is a 'copy' of $form (notice that it is spelled 'complete form', with a space, not an underscore).
so using $form_state['complete form']['field_name']['und'][0]['value']['#value']='foo'; is what updates the values that actually repopulate the form on a validation error. (note: you can, as do I in my usecase, set it to =NULL to simply empty the field).
summary
now where is the difference between $form['field_name'] (e.g. updating through form_set_value()) and $form['complete form']? well, the former updates the actual value, which then gets stored inside the database, the latter is being used to repopulate a form when it failed a validation.

Categories