This must be a simple question, but unable to find what I am doing wrong. I have a form (shown below) with a multi-select input. It gets sent to the validator as an array. I want to make sure the values selected in the client-side are in the pre-defined list of values.
<form>
<select multiple name="names[]">
<option value="Jerry">Jerry</option>
<option value="Susan">Susan</option>
<option value="Tim">Tim</option>
</select>
</form>
public function postNames(Request $request)
{
$this->validate($request, [
'names.*' => 'in:Jerry,Susan,Tim',
]);
}
When I open the dev tools in the browser and change the value of them and then submit the form, the validator lets it pass even though the name in the request is not in the list as defined in the validator.
Thank you.
UPDATE:
When I dump the request it looks like this.
+request: Symfony\Component\HttpFoundation\ParameterBag {#52 ▼
#parameters: array:1 [▼
"names" => array:1 [▼
0 => "Robert"
]
]
}
Related
I have an Array to string conversion error, when trying to realize multiple upload files function in Laravel 5.8.38
Cant find any decision about it
In blade form I have simple thing:
<form class="form-horizontal" action="{{route('admin.estates.store')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<label for="estate_image" class="mt-4">Images</label>
<input type="file" name="estate_image[]" multiple>
<input class="btn btn-primary" type="submit" value="Сохранить">
<input type="hidden" name="created_by" value="{{Auth::id()}}">
</form>
In store function I have:
The function creates an estate(one property). If user adds some images for it, we adds this images in local path and adds them in database
if I comment $estate = Estate::create($request->all()); it works fine
but in this case estate doesnt adds in database
public function store(Request $request)
{
$estate = Estate::create($request->all());
if($request->hasFile('estate_image')) {
foreach ($request->file('estate_image') as $image) {
// do some image resize and store it on local path
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images\\' . $filename);
Image::make($image)->resize(800, 400)->save($location);
// add image info in database
$estateimage = new EstateImages();
$estateimage->image_path = $location;
$estateimage->image_alt = 'testalt';
$estateimage->save();
}
}
}
Am array I have from input
array:5 [▼
"name" => array:2 [▼
0 => "image1.jpg"
1 => "image2.jpg"
]
"type" => array:2 [▼
0 => "image/jpeg"
1 => "image/jpeg"
]
"tmp_name" => array:2 [▼
0 => "C:\OSPanel\userdata\php_upload\phpCB51.tmp"
1 => "C:\OSPanel\userdata\php_upload\phpCB52.tmp"
]
"error" => array:2 [▼
0 => 0
1 => 0
]
"size" => array:2 [▼
0 => 164808
1 => 58217
]
]
As understand, foreach doesnt start, but dont understand why (tried to delete all code in foreach, and leave there just simple echo 'Hello!'; , have the same error.
Saw the same problems in StackOverflow, but any of it helped me...
The problem was in first line
$estate = Estate::create($request->all());
So, from blade I receive a name="estate_image[] data which is an array, and Laravel tried to adds an array in database cell. In database cell cold be added just string value and through that, I had that error.
Solve it, by deleting this column from database and deleting this from $fillable variable in main model.
Quite stupid and easy thing from me, but hope, this answer helps someone. :-)
$DOB = $request->input('DOB');
$allowed = Carbon::now()->subYears(26)->format('Y-m-d');
$validatedData = $request->validate(
['DOB' => 'required|date|before_or_equal:'.$allowed],
);
<input type="date" name="DOB" class="form-control" required>
I am having a hard time validating dates in Laravel.
If I enter 01/01/2018 in the form, the validation does not catch it. Below it what is returned if I dd() the variables.
dd([$DOB, $allowed]);
array:2 [▼
0 => "2018-01-01"
1 => "1992-04-26"
]
No idea where I am going wrong.
After get all categories and user selected categories for selected post with this result:
$content_categories = ContentCategories::all()->pluck('title', 'id');
Collection {#209 ▼
#items: array:3 [▼
1 => "laravel"
2 => "nodejs"
3 => "php"
]
}
$selected_categories = $manage_content->categories()->get()->pluck('title', 'id');
Collection {#223 ▼
#items: array:2 [▼
1 => "laravel"
2 => "php"
]
}
I'm trying to implement this result on html select and do check selected items when we have in $selected_categories, this code only show $content_categories and can not be check items with $selected_categories by default
{{
Form::select('categories[]',
$content_categories,
$selected_categories,
array('multiple'=>'multiple'))
}}
result:
<select class="multiselect-success" multiple="multiple" name="categories[]">
<option value="1">laravel</option>
<option value="2">nodejs</option>
<option value="3">php</option>
</select>
Get IDs of selected categories without titles:
$selected_categories = $manage_content->categories()->pluck('id');
Or use keys() to get keys only:
Form::select('categories[]', $content_categories, $selected_categories->keys(), array('multiple'=>'multiple'))
I modified the register controller AND the user table to have a role attribute as a string:
Schema::table('users', function (Blueprint $table) {
$table->string('role')->nullable()->index();
});
and i have created an enums.php in config folder like this:
return [
'role_types' => [
'ADMIN' => "Admin",
'TEACHER' => "Teacher",
'STUDENT' => "Student",
]];
on the register user view (which is scaffold by default with bootstrap)
i made select box to accommodate the selection:
<select class="form-control" id="role" name='role'>
#foreach (Config::get('enums.role_types') as $role)
<option value="{{ $role }}">{{$role}}</option>
#endforeach
</select>
But up on registering the user, the value is NULL.. what am i doing wrong ?
When using Model::create() you have to set your model's fillable array:
protected $fillable = [
'email',
'name',
'role',
...
];
Laravel does not let you mass assign a row using data it cannot trust. Check the docs: https://laravel.com/docs/5.3/eloquent
I have been reading Laravel validation documentation. I am not clear how to combine two rules.
For example:
<input type="checkbox" name="has_login" value="1">
<input type="text" name="pin" value="">
If has_login checkbox is ticked then Input text pin value is required.
If has_login is not ticked then Input text pin is not required.
Laravel Validation:
public function rules()
{
return [
'has_login' => 'accepted',
'pin' => 'required',
];
}
Use required_with or required_if
required_with:foo,bar
The field under validation must be present and not empty only if any of the other specified fields are present.
return [
'has_login' => 'sometimes',
'pin' => 'required_with:has_login,on',
];
--
required_if:anotherfield,value
The field under validation must be present and not empty if the anotherfield field is equal to any value.
return [
'has_login' => 'sometimes',
'pin' => 'required_if:has_login,on',
];
--
https://laravel.com/docs/5.2/validation
--
Also, if the checkbox has_login is not checked, it will not send as part of the form submission