I'm trying to edit users via form.
So far, so good.
<div class="form-group">
{{ Form::label('admin_level', 'Admin?') }}
{{ Form::select('admin_level', array(0 => 'no', 1 => 'yes'), null, array('class' => 'form-control')) }}
</div>
My problem is: In the rendered version of this form, the value changes to string, but I need an integer value to update the user.
Tried to delete the '' but without any changes. (as you might see above)
DD is giving me this:
"admin_level" => "0"
How to get the value in 'admin_level' to an integer value?
UPDATE
I just added this to my UserController function update:
$request['admin_level'] = (int)$request['admin_level'];
If I dd($request) in the attributes it's now an integer, but I don't get it stored with:
$user->update($request->except(['_token']));
The value keeps the same.
I solved it!
You have to add the attribute to the /app/User.php in array $fillable:
protected $fillable = [
'name', 'email', 'password', 'admin_level',
];
Related
Hi I'm using Laravel version 5.6 and got a problem in view with Form::select.
I already made some Form::open with 'text' and 'textarea' they all worked fine, but the Form::select do not generate the select fild on my view.
I used this code:
{!! Form::label('isPropaganda', 'Propaganda:') !!}
{!! Form::select('isPropaganda', ['Não' => '0', 'Sim' => '1'], null, ['class'=>'form-control','multiple']) !!}
I found the example here http://laravel-recipes.com/recipes/163/creating-a-select-box-field but didn't workout. How can I fix it?
Try flipping the keys and values. Keys cannot have special characters in them. Also, I'm not sure what that 'multiple' attribute is doing there. If it's meant to be a css class, place it inside the 'class' array key, otherwise add it to a new attributes array.
{!! Form::select('isPropaganda', ['0' => 'Não', '1' => 'Sim'], null, ['class'=>'form-control multiple']) !!}
I need advice how to store an array to database. For example i have an input with name="user_phone[]" and i want to store to database the value of this input.
I have a form like so, also there other inputs but i copy just one:
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
and the controller:
public function postAddOrder(Request $request)
{
$this->validate($request, [
'receipt_date' => 'date|required',
'return_date' => 'date|required',
'user_name' => 'required',
'user_phone' => 'required',
'work_sum' => 'integer|required',
'user_descr' => 'required',
'foruser_descr' => 'required'
]);
$user = new User;
$user = $user->all()->find($request->input('user_name'));
$order = new Order([
'receipt_date' => $request->input('receipt_date'),
'return_date' => $request->input('return_date'),
'user_name' => $user->fio,
'user_phone' => $request->input('user_phone'),
'device' => $request->input('device'),
'work_sum' => $request->input('work_sum'),
'master_name' => $request->input('master_name'),
'user_descr' => $request->input('user_descr'),
'foruser_descr' => $request->input('foruser_descr'),
'type' => $request->input('type'),
'status' => $request->input('status'),
'engineer' => $request->input('engineer'),
'partner' => $request->input('partner'),
'office' => $request->input('office')
]);
$order->save();
return redirect()->route('admin.orders.index');
}
The problem is when i'm submitting the form and getting the error:
htmlentities() expects parameter 1 to be string, array given
Also i'm using casts to store an array to DB:
/**
* The attributes that should be casted to native types.
*
* #var array
*/
protected $casts = [
'user_phone' => 'array',
];
The values are storing correctly, the main problem is that the validate() method is not catching the errors. For example im not filling some data in inputs which are required. When instead of getting the error like something is required im getting error
htmlentities() expects parameter 1 to be string, array given
When im filling all input with data everything goes ok.
I think the problem comes from your rule
'user_phone' => 'required
To validate array values you should use the array validation. (Link)
rewrite your rule like so
"user_phone.0" => "required"
this will ensure that at least one user_phone is provided.
In case you wanna validate phone format just go with:
"user_phone.*" => "{Insert validation here}"
Found the definition.
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[0]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
We must pass the index in inputs. Like name="user_phone[0]" after that we are not getting the error:
htmlentities() expects parameter 1 to be string, array given
And validate() method catching the errors. It was the only solution for me.
I have the following stripped form:
{{ Form::text('invoicerow[]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
You can add a row by JS.
When i post this and validates it false it's return to the form page with this error.
htmlentities() expects parameter 1 to be string, array given (View: /home/xxx/public_html/resources/views/invoice/create.blade.php)
How can i obviated this?
Using forms that create arrays cause problems with Laravel. The workaround for this is to add explicit indexes to your form names (as below). This should resolve the issue for you, and allow you to correctly validate each field.
{{ Form::text('invoicerow[0]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
{{ Form::text('invoicerow[1]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
{{ Form::text('invoicerow[2]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
...
However, it does mean that the + and x buttons you have in the form need to recalculate the indexes for the fields when a new row has been added, or deleted.
E.g. if I delete invoicerow[1], your JavaScript will need to recalculate the index for the other fields, so invoicerow[2] is renamed to invoicerow[1].
Similarly, if I add a new row to the end, the name for the new field should be invoicerow[3] (assuming I've already added 3 fields before), i.e. invoicerow[n+1]
How do I set a certain option as currently selected?
My current code is:
{{ Form::select('make',$makes , NULL, ['class' => 'form-control']) }}
$makes is an array of car makes.
Lets say the array looks like:
[
0 => 'Audi',
1 => 'BMW',
2 => 'Mercedes'
]
If I am editing a car, I know that in my DB I have said that car is X make.
In this case, lets say its a BMW. When I create this select input I want BMW to already be shown as the selected option. I have this value already, its just getting the blade input to set it to selected.
I tried:
[$tMake->display_name] + $makes
But that just add a new option to the list, which is not what I want.
Thanks.
You need to tell the select the ID you want pre-selected.
You can do this:
{{ Form::select('make',$makes , $tMake->id, ['class' => 'form-control']) }}
And if you are using validation - you'll want to do something like this, so that the old input is shown if the validation fails
{{ Form::select('make',$makes , Input::old('make', $tMake->id), ['class' => 'form-control']) }}
I have a form containing text inputs and select box. I was trying to apply laravel validation. Now i wanted to retain the user inputted values, if validation doesn't success.
I am able to get this done on input box, but not on select box. How to show previously selected value(in select box) if validation doesn't pass.
This is my code
{{Form::select('vehicles_year', $modelYears, '-1', ['id' => 'vehicles_year'])}}
<span class="help-block" id="vehicles_year_error">
#if ($errors->has('vehicles_year')) {{$errors->first('vehicles_year')}} #endif
</span>
-1 is the key of default value that i am showing when the form loads.
What I do is that I add "default" option which value is set to "nothing".
In validation rules I say this value is required. If user does not pick
one of the other options, validation fails.
$options = [
'value' => 'label',
'value' => 'label',
'value' => 'label',
'value' => 'label',
];
$options = array_merge(['' => 'please select'], $options);
{{ Form::select('vehicles_year', $options, Input::old('vehicles_year'), ['id' => 'vehicles_year']) }}
#if ($errors->has('vehicles_year'))
<span class="help-block" id="vehicles_year_error">
{{ $errors->first('vehicles_year') }}
</span>
#endif
// Validation rules somewhere...
$rules = [
...
'vehicles_year' => 'required|...',
...
];
The controller code is missing. I will suppose your handle the POST in a controller method and then you Redirect::back()->withInput();
Thanks to ->withInput() You can use in your view something like Input::old() to get the Input values of your previous request.
So to select your previous item AND have -1 by default, you can use Input::old('vehicles_year', -1)
The first argument is the Input name, and the second is the default value.
{{Form::select('vehicles_year', $modelYears, Input::old('vehicles_year', -1), ['id' => 'vehicles_year'])}}
Hope this helps