in laravel 5.6 app I have table name as vehicles, then I need update some of table values in VehicleController update function with validation as this,
$this->validate($request, [
'provincename' => 'required|min:3',
'districtname' => 'required',
'townname' => 'required',
'brandname' => 'required',
'modelname' => 'required',
'year' => 'required',
'condition' => 'required',
'milage' => 'required',
'data' => 'required',
'price' => 'required',
'telephone' => 'required',
'categoryname' => 'required',
'tramsmission' => 'required',
'fueltype' => 'required',
'enginecapacity' => 'required'
]);
and my update controller is like this,
$vehicle = Vehicle::find($id);
$vehicle->provincename = $request->input('provincename');
$vehicle->districtname = $request->input('districtname');
$vehicle->townname = $request->input('townname');
$vehicle->brandname = $request->input('brandname');
$vehicle->modelname = $request->input('modelname');
$vehicle->modelyear = $request->input('year');
$vehicle->condition = $request->input('condition');
$vehicle->milage = $request->input('milage');
$vehicle->detail = $request->input('data');
$vehicle->price = $request->input('price');
$vehicle->telephone = $request->input('telephone');
$vehicle->categoryname = $request->input('categoryname');
$vehicle->transmission = $request->input('transmission');
$vehicle->fueltype = $request->input('fueltype');
$vehicle->enginecapacity = $request->input('enginecapacity');
$vehicle->user_id = Auth::user()->id;
$vehicle->save();
My update is ok. it is working. but with controller validation it is not updating. without validation it is working. how can fix this? what is the problem with this?
try this kind of validation format:
$validatedData = $request->validate([
'provincename' => 'required|min:3',
'districtname' => 'required',
'townname' => 'required',
'brandname' => 'required',
'modelname' => 'required',
'year' => 'required',
'condition' => 'required',
'milage' => 'required',
'data' => 'required',
'price' => 'required',
'telephone' => 'required',
'categoryname' => 'required',
'tramsmission' => 'required',
'fueltype' => 'required',
'enginecapacity' => 'required'
]);
EDIT
don't forget to use the validator. Add use Validator; after the namespace.
Related
I'm currently working on an API for a car dealership, small school project.
In the CarController, I'm working on the POST request for adding a car and I'm having some problems with the validation of data.
This is a sample of the kind of data I'd be receiving:
{
"model": "Q5",
"brand": "Audi",
"year": 2008,
"price": 44000,
"color": "blue",
"traction": "4x4",
"motor": {
"type": "gasoline",
"hp": 256,
"turbo": false,
"cylinders": 4,
"motor_liters": 2.0
},
"user_id": 1
}
Considering the format, I wrote the validation like this:
$request->validate([
'model' => 'required|string',
'brand' => 'required|string',
'year' => 'required|integer',
'price' => 'required|numeric',
'color' => 'required|string',
'traction' => 'required|string',
'motor' => [
'type' => 'required|string',
'hp' => 'required|integer',
'turbo' => 'required|boolean',
'cylinders' => 'required|integer',
'motor_liters' => 'required|numeric'
],
'user_id' => 'required|integer'
]);
However, when making the request, it gives me the following error from the Validator:
BadMethodCallException: Method Illuminate\Validation\Validator::validateRequired|string does not exist.
At this point I don't know if it's a typo, bad formatting or I have to handle nested JSONs differently, considering I have a UserController with a POST request that looks like this:
$request->validate([
'username' => 'required|string|min:5',
'password' => 'required|string'
]);
And it works perfectly.
If you want to try the API yourselves here's the link to the repo.
to validate nested fields you have to use dot notation and not nest arrays:
$request->validate([
'model' => 'required|string',
'brand' => 'required|string',
'year' => 'required|integer',
'price' => 'required|numeric',
'color' => 'required|string',
'traction' => 'required|string',
'motor.type' => 'required|string',
'motor.hp' => 'required|integer',
'motor.turbo' => 'required|boolean',
'motor.cylinders' => 'required|integer',
'motor.motor_liters' => 'required|numeric',
'user_id' => 'required|integer'
]);
How to validate a laravel request object, I have this conditions
request()->validate([
'avatar' => 'required|image',
'display_name' => 'required|min:3|max:100',
'role_id' => 'required|integer',
'username' => 'required|unique:users|min:4|max:15',
'email' => 'required|email|unique:users',
'phone_number' => 'required|min:3',
'password' => 'required|min:6',
'country_id' => 'required|integer'
]);
But in my request it was stored in an object form, what should I do to make the validations work ?
You validate it the same way you validate a multi-dimensional array:
request()->validate([
'form.avatar' => 'required|image',
'form.display_name' => 'required|min:3|max:100',
'form.role_id' => 'required|integer',
'form.username' => 'required|unique:users|min:4|max:15',
'form.email' => 'required|email|unique:users',
'form.phone_number' => 'required|min:3',
'form.password' => 'required|min:6',
'form.country_id' => 'required|integer'
]);
If your form is an array of object, you can use wildcard
request()->validate([
'form.*.avatar' => 'required|image',
'form.*.display_name' => 'required|min:3|max:100',
'form.*.role_id' => 'required|integer',
'form.*.username' => 'required|unique:users|min:4|max:15',
'form.*.email' => 'required|email|unique:users',
'form.*.phone_number' => 'required|min:3',
'form.*.password' => 'required|min:6',
'form.*.country_id' => 'required|integer'
]);
For unique validation you can pass the field name on which you need to apply validation, by default it takes the key name.
You can use this for unique validation on object data:-
'form.username' => 'required|unique:users,username|min:4|max:15'
I am trying to submit a form data using Laravel controller. But I got this error while submit
local.ERROR: BadMethodCallException: Method [validateTextRequired] does not exist.
controller.php
$this->validate($request, [
'photo' => 'required|mimes:jpg,gif,png,jpe,jpeg',
'title' => 'required|min:3|max:45',
'name' => 'required',
'address' => 'required',
'city' => 'required',
'tribe' => 'required',
'language' => 'required',
'country' => 'required',
'student_id' => 'required',
'tribe_university_name' => 'required',
'student_program_of_study' => 'required',
'faculty' => 'required',
'bank_name' => 'required',
'account_number' => 'required',
'address' => 'required',
'swift_code' => 'required',
'routing_number' => 'required',
// 'categories_id' => 'required',
'goal' => 'required|integer|max:' . $this->settings->max_campaign_amount . '|min:' . $this->settings->min_campaign_amount,
'description' => 'text_required|required|min:20',
]);
if ($this->request->hasFile('photo')) {
$image = $request->photo->store('public/images');
}
$user = User::find(Auth::user()->id);
$user->address = trim($this->request->address);
$user->city = trim($this->request->city);
$user->tribe = trim($this->request->tribe);
$user->tribe_university_name = trim($this->request->tribe_university_name);
$user->student_program_of_study = trim($this->request->student_program_of_study);
$user->faculty = trim($this->request->faculty);
$user->level = trim($this->request->level);
$user->save();
return $id_campaign;
Can't solve the problem. Anybody help please ?
So I have upload_media and url. upload_media is where you have to upload a .pdf file, and url if you want a url.
so what I want is that, if upload_media is not empty then, url should not be required. If the user decided to use the url, then the upload_media will not be required. But if either of them doesn't have a value then it should return a required error.
Here is my validation:
$this->validate($request, [
'title' => 'required',
'viewing_time' => 'required',
'tags' => '',
'description' => '',
'organization' => '',
'upload_media' => '',
'url' => '',
'upload_preview' => 'required|file|image'
]);
You can use required_without rule: https://laravel.com/docs/5.6/validation#rule-required-without
$this->validate($request, [
'title' => 'required',
'viewing_time' => 'required',
'tags' => '',
'description' => '',
'organization' => '',
'upload_media' => 'required_without:url',
'url' => 'required_without:upload_media',
'upload_preview' => 'required|file|image'
]);
$rules = [
'title' => 'required',
'viewing_time' => 'required',
'tags' => '',
'description' => '',
'organization' => '',
'upload_media' => 'required',
'url' => 'required',
'upload_preview' => 'required|file|image'
];
if ($request->file('upload_media')->isValid()) {
$rules['url'] = '';
} elseif (!empty($request->get('url'))) {
$rules['upload_media'] = '';
}
$this->validate($request, $rules);
I am writing Validator in Laravel, and so I am getting json format request.
I have written validate logic for main json's fields such as id, name... Now I have items array of objects in json, and I need to validate each item from this array which is passed.
Here's example of json request: https://api.myjson.com/bins/ob3lh
And here's my validator so far:
private function update()
{
return [
'id' => 'required',
'place_id' => 'required',
'place_table_id' => 'required',
'user_id' => 'required',
'seen' => 'required',
'state' => 'required',
'number' => 'required',
'date' => 'required',
'price' => 'required',
'table_number' => 'required',
'note' => 'required',
];
}
How can I add simply for example nested validator which will validate each of items object?
You can define nested validation rules like this:
private function update()
{
return [
'id' => 'required',
'place_id' => 'required',
'place_table_id' => 'required',
'user_id' => 'required',
'seen' => 'required',
'state' => 'required',
'number' => 'required',
'date' => 'required',
'price' => 'required',
'table_number' => 'required',
'note' => 'required',
'items' => 'required|array|min:1',
'items.*.id' => 'required',
'items.*.name' => 'required',
'items.*.amount' => 'required',
'items.*.price' => 'required',
];
}
You can find more info on this in the docs: https://laravel.com/docs/5.5/validation#validating-arrays