Hi there!
It seems I found some nasty bug. I have a simple registration form which ask for name, last name, email and password (and verification)
I was testing the validation when I realized that whenever I put something into the password field, this exception arises:
Warning: Illegal string offset 'name' in /Applications/MAMP/htdocs/laravel-develop/vendor/laravel/framework/src/Illuminate/Html/FormBuilder.php line 199
This is the code of the template in that area:
<div>
{{ Form::label('email', 'Email', array('class' => $errors->has('email') ? 'error' : '')); }}
{{ Form::text('email', Request::old('email'), array('class' => $errors->has('email') ? 'error' : '')); }}
@if ($errors->has('email'))
<small class="error">{{ $errors->first('email') }}</small>
@endif
</div>
<div>
{{ Form::label('password', 'Password', array('class' => $errors->has('password') ? 'error' : '')); }}
{{ Form::password('password', Request::old('password'), array('class' => $errors->has('password') ? 'error' : '')); }}
@if ($errors->has('password'))
<small class="error">{{ $errors->first('password') }}</small>
@endif
</div>
<div>
{{ Form::label('password_confirmation', 'Password confirm', array('class' => $errors->has('password_confirmation') ? 'error' : '')); }}
{{ Form::password('password_confirmation', Request::old('password_confirmation'), array('class' => $errors->has('password_confirmation') ? 'error' : '')); }}
@if ($errors->has('password_confirmation'))
<small class="error">{{ $errors->first('password_confirmation') }}</small>
@endif
</div>
And this is the controller:
// Declare the rules for the form validation.
//
$rules = array(
'first_name' => 'Required',
'last_name' => 'Required',
'email' => 'Required|Email|Unique:users',
'password' => 'Required|Confirmed',
'password_confirmation' => 'Required'
);
// Get all the inputs.
//
$inputs = Input::all();
// Validate the inputs.
//
$validator = Validator::make($inputs, $rules);
// Check if the form validates with success.
//
if ($validator->passes())
{
// Create the user.
//
$user = new User;
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
// Redirect to the register page.
//
return Redirect::to('account/register')->with('success', 'Account created with success!');
}
// Something went wrong.
//
return Redirect::to('account/register')->withInput($inputs)->withErrors($validator->messages());
What could be wrong? Is there any bugs? I need to go to a previous route and then go back to this. If I just reload the page the same issue will arise.
Never heard of anything like this happening to anyone. Do you have a
full stack trace or something? Pretty much going to be impossible for us
to figure it out with this information.
Form::password does not accept flashed data (hooray!). You need to remove Input::old as second parameter, that parameter is for options, and is thus expected to be an array. You pass a string, so 'oldpassword'['name'] gives an error, obviously.
@JoostK thanks for that. I just felt like a dumb. I think that maybe the error could be more clear? Thanks again!
It way be a good idea to add type hints to the options parameter, so you can only pass arrays.
Wow this really helped @JoostK. I dont have a password field, and to confuse matters i have an input called name, and that wasnt the one bugging out. I also realized the Laravel output has 2 separate error messages, it shows the 2nd one first, then the 1st one on the bottom half of the page. inspecting the 2nd error message, I noticed , because of your mention, this particular in the 1st block (below the 2nd block, so wayyyyy down on the page
(1/2)聽ErrorExceptionIllegal string offset 'name'
--
in聽FormBuilder.php聽(line 501)
at聽HandleExceptions->handleError(2, 'Illegal string offset \'name\'', '/home/vagrant/code/xapp/Laravel/vendor/laravelcollective/html/src/FormBuilder.php', 501,聽array('name' => 'comments', 'value' => 'comments', 'options' => 'comments'))in聽FormBuilder.php聽(line 501)
Once I realized comments was a Form:textarea, based on your comment, i changed old('comment') to null, and VIOLA, error gone. Ill have to figure out a way to repopulate a textarea box.
EDIT
Ok, I had the parameters wrong, I was passing one too many args, which is what caused this to come up,
I was creating the text area like this
{{Form::textarea('comments','comments',old('comments'),
[
'id'=>'comments',
'class'=>'form-control',
'placeholder'=>'Enter Comments',
'required'
])}}
But now that i removed that extra parameter, things are working as expected.
{{Form::textarea('comments', old('comments'),
[
'id'=>'comments',
'class'=>'form-control',
'placeholder'=>'Enter Comments',
'required'
])}}
There should probably be an exception catch with a copious error output there, when you pass a string, stating that the parameters are passed incorrectly, otherwise this was really cryptic to catch.
I got this issue in Laravel version 5.6.16, whenever I do edit for existing record with image upload field it throws this error:
<div class="form-group">
{{ Form::label('image','Uplaod Image :') }}
{{ Form::file('image','',array('class'=>'form-control')) }}
</div>
ErrorException (E_ERROR)
Illegal string offset 'name' (View: /var/www/html/myapp/resources/views/banner/edit.blade.php)
Full error:
/var/www/html/myapp/vendor/laravelcollective/html/src/FormBuilder.php
{
return $value ?: ucwords(str_replace('_', ' ', $name));
}
/**
* Create a form input field.
*
* @param string $type
* @param string $name
* @param string $value
* @param array $options
*
* @return \Illuminate\Support\HtmlString
*/
public function input($type, $name, $value = null, $options = [])
{
$this->type = $type;
if (! isset($options['name'])) {
$options['name'] = $name;
}
// We will get the appropriate value for the given field. We will look for the
// value in the session for the value in the old input data then we'll look
// in the model instance if one is set. Otherwise we will just use empty.
$id = $this->getIdAttribute($name, $options);
if (! in_array($type, $this->skipValueTypes)) {
$value = $this->getValueAttribute($name, $value);
}
// Once we have the type, value, and ID we can merge them into the rest of the
// attributes array so we can convert them into their HTML attribute format
// when creating the HTML element. Then, we will return the entire input.
$merge = compact('type', 'value', 'id');
$options = array_merge($options, $merge);
return $this->toHtmlString('<input' . $this->html->attributes($options) . '>');
}
Arguments
"Illegal string offset 'name' (View: /var/www/html/myapp
@shahzadthathal Try to:
{{ Form::file('image', array('class'=>'form-control')) }}
tnx a lot man , it works for me
Most helpful comment
Form::passworddoes not accept flashed data (hooray!). You need to removeInput::oldas second parameter, that parameter is for options, and is thus expected to be an array. You pass a string, so'oldpassword'['name']gives an error, obviously.