Do you know why it appears "BadMethodCallException Method [validateEquired] does not exist"? - php

I have the method store() to create a new registration type for the conference. But it appears an error "BadMethodCallException
Method [validateEquired] does not exist." in the file "Illuminate/Validation/Validator.php".
Do you know why?
public function store(Request $request, $id)
{
$rules = [
'registration_type_name' => 'required',
'registration_type_capacity' => 'required|integer|min:0',
'registration_type_price' => 'required|integer|min:0',
'registration_type_minimum' => 'equired|integer|min:1',
'registration_type_maximum' => 'rgt:registration_type_minimum|required|integer|min:1',
];
$customMessages = [
'registration_type_name.required' => 'The field name is rqeuired.',
'registration_type_capacity.integer' => 'The field capacity needs to be a positive integer.',
....
];
$this->validate($request, $rules, $customMessages);
$conference = Conference::find($id);
RegistrationType::create([
'name' => $request->registration_type_name,
'description' => $request->registration_type_description,
'capacity' => $request->registration_type_capacity,
'price' => $request->registration_type_price,
'min_participants' => $request->registration_type_minimum,
'max_participants' => $request->registration_type_maximum,
'conference_id' => $conference->id
]);
Session::flash('success', 'Registration type created.');
return redirect()->back();
}

You're missing an r here:
'registration_type_minimum' => 'equired|integer|min:1',
^

Related

My custom message Request validator not work Laravel

I use the Request validation but I don't get message from request. Look my code and see my mistake. Only important is store function which work good if all fields is fullfiled but if any field not filled i don't get my custom message from request. For not filled field I don't give error just laravel home page.
This is my request file
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CarRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'car_type' => 'required',
'mark' => 'required',
'model' => 'required',
'fuel' => 'required',
'circuit' => 'required',
'chassis' => 'required|numeric',
'bill_type' => 'required',
'first_registration' => 'required|date',
'km' => 'required|numeric',
'structure' => 'required',
'motor_vehicle_check' => 'required|boolean',
'warranty' => 'required|boolean',
'year' => 'required|date',
'import_vehicle' => 'required|boolean',
'know_damage' => 'required',
'car_accessories' => 'required',
'email' => 'required|email'
];
}
public function messages()
{
return [
'car_type.required' => 'A car_type is required',
'mark.required' => 'A mark is required',
'model.required' => 'A model is required',
'fuel.required' => 'A fuel is required',
'circuit.required' => 'A circuit is required',
'chassis.required' => 'A chassis is required',
'bill_type.required' => 'A bill_type is required',
'first_registration.required' => 'A first_registration is required',
'km.required' => 'A km is required',
'structure.required' => 'A structure is required',
'motor_vehicle_check.required' => 'A motor_vehicle_check is required',
'warranty.required' => 'A warranty is required',
'year.required' => 'A year is required',
'import_vehicle.required' => 'A import_vehicle is required',
'know_damage.required' => 'A know_damage is required',
'car_accessories.required' => 'A car_accessories is required',
'email.required' => 'A email is required'
];
}
}
And this is my controller code
<?php
namespace App\Http\Controllers;
use App\Car;
use App\CarImages;
use App\Http\Requests\CarRequest;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
class CarController extends Controller
{
public function index()
{
$cars = Car::with(['images'])
->orderByDesc('car.created')
->get();
return response()->json($cars, 200);
}
public function search($name){
$cars = Car::where('mark', '=' , $name)->get();
return $this->response($cars);
}
public function create()
{
//
}
public function show($id)
{
$car = School::with(['images'])->find($id);
if (!$car) {
return response()->json(['message' => 'No Car found'], 404);
}
return response()->json($car, 200);
}
public function store(CarRequest $request)
{
$car = Car::create([
'car_type' => $request->input('car_type'),
'mark' => $request->input('mark'),
'model' => $request->input('model'),
'fuel' => $request->input('fuel'),
'circuit' => $request->input('circuit'),
'chassis' => $request->input('chassis'),
'bill_type' => $request->input('bill_type'),
'first_registration' => $request->input('first_registration'),
'km' => $request->input('km'),
'structure' => $request->input('structure'),
'motor_vehicle_check' => $request->input('motor_vehicle_check'),
'warranty' => $request->input('warranty'),
'year' => $request->input('year'),
'import_vehicle' => $request->input('import_vehicle'),
'know_damage' => $request->input('know_damage'),
'car_accessories' => $request->input('car_accessories'),
'email' => $request->input('email')
]);
return response()->json([
'message' => 'Your car has been successfully added',
'car' => $car
],201);
}
public function destroy($id)
{
$car = Car::destroy($id);
return response()->json($id);
}
}
I use the Request validation but I don't get message from request.
When expecting a json response, don't forget to add this header when making your requests (client side):
Accept: Application/json // <--
If I want set in my custom message example km must be numberic , how do that in messages function?
You need to specify your message for every rule like this. Let's do it for the km validation:
MyCustomRequest.php
public function rules()
{
return [
// ...
// 'first_registration' => 'required|date',
'km' => 'required|numeric', // <---
// 'structure' => 'required',
// ...
];
}
Given that km has two validations, just add one element more in the messages() function specifying the rule you want to modify:
MyCustomRequest.php
public function messages()
{
return [
// ...
'km.required' => 'A km is required',
'km.numeric' => 'The km needs to be numeric dude!', // <---
// ...
];
}
Regarding this last subject, check the documentation.
As I see you have done all things correctly. The only you missed is a little one in your store() method :
$validator = $request->validated();
right at beginning of the method's body

Laravel 5 Update Validation email needs to be unique

I'm trying to update a user, as an admin.
I'm changing the username, but it says email must be unique.
How do I fix this.
public function update($id, PutUser $request)
{
if (auth()->id() == $id) {
return redirect()->back()->withFlashDanger('Permission Denied, You can not edit own profile here.');
}
$user = User::find($id);
$user->update((array_merge($request->validated(), ['county' => request('county')])));
//Update model_has_roles model with assignees_roles
return redirect()->route('users.index')->withFlashSuccess(trans("alerts.users.updated"));
}
This is the request class
public function authorize()
{
return true;
}
public function rules()
{
$user_id = $this->input('id');
return [
'name' => 'required|string',
'username' => 'required',
'email' => 'required|email|unique:users,email'.$user_id,
'gender' => 'required',
'phone' => 'sometimes|numeric',
'address' => 'sometimes|string',
'country_id' => 'required',
];
}
}
I keep getting a failed email validation. 'Email has already been taken'. Any idea
You are missing a comma after the email label in your validation:
return [
'name' => 'required|string',
'username' => 'required',
'email' => 'required|email|unique:users,email,'.$user_id,
'gender' => 'required',
'phone' => 'sometimes|numeric',
'address' => 'sometimes|string',
'country_id' => 'required',
];
Since Laravel 5.3 (I believe), you can also use rule builders for more descriptive validation rules. Those are better to read and interpret for humans so it would result in a lower error rate:
use Illuminate\Validation\Rule;
return [
'email' => [
'required',
Rule::unique('users', 'email')->except($user_id),
]
];
https://medium.com/#tomgrohl/why-you-should-be-using-rule-objects-in-laravel-5-5-c2505e729b40

Can't get data to save in MySQL using Laravel

I'm trying to get the ticket data to save in the database and when I submit the form I get no error, but it does not insert the data into the database. Added my Route just adding random text because the post
Controller Code
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'title' => 'required|min:15',
'category' => 'required',
'priority' => 'required',
'message' => 'required|min:100',
]);
$ticket = new Ticket([
'user_id' => Auth::user()->id,
'category_id' => $request->input('category'),
'ticket_id' => strtoupper(str_random(10)),
'name' => $request->input('name'),
'title' => $request->input('title'),
'priority_id' => $request->input('priority'),
'message' => $request->input('message'),
]);
$ticket->status_id = '1';
$ticket->save();
return 'Success';
}
Model Code
class Ticket extends Model
{
protected $fillable = [
'user_id', 'category_id', 'ticket_id', 'name', 'title', 'priority_id', 'message', 'status_id',
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function priority()
{
return $this->belongsTo(Priority::class);
}
public function status()
{
return $this->belongsTo(Status::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
Routes
Route::get('/', 'HomeController#index')->name('home');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/compliance', 'HomeController#Compliance')->name('compliance');
Route::get('/messages', 'HomeController#Messages')->name('messages');
Route::get('/tickets', 'TicketsController#userTickets')->name('tickets');
Route::get('/tickets/create', 'TicketsController#create')->name('tickets/create');
Route::post('/tickets/store', 'TicketsController#store');
Thanks for all the help I figured it I had two mistakes in the controller this took me 2 days to figure it out LOL
$this->validate($request, [
'name' => 'required', // I removed this
'title' => 'required|min:15',
'category' => 'required',
'priority' => 'required',
'message' => 'required|min:100',
]);
and in the $ticket
$ticket = new Ticket([
'user_id' => Auth::user()->id,
'category_id' => $request->input('category'),
'ticket_id' => strtoupper(str_random(10)),
'name' => $request->input('name') // I replaced this with 'name' => Auth::user()->name,
'title' => $request->input('title'),
'priority_id' => $request->input('priority'),
'message' => $request->input('message'),
]);

Returning Laravel validation errors to Ajax call

I'm trying to figure out how to pass back to my ajax call that there were validation errors if there is so that I can prevent the page from continuing on and display those errors to the user.
/*
* Create User After they complete the first part of the form.
*
*/
public function createUserAndOrder(Request $request)
{
$validation = $this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|confirmed|unique:users,email',
'email_confirmation' => 'required'
]);
$credentials = [
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
'password' => Hash::make(str_random(8)),
];
$user = Sentinel::registerAndActivate($credentials);
$user->role()->attach(5);
return response()->json([
'success' => true,
'errors' => null
]);
}
You can try it by using Validate Facade as:
$validator = \Validator::make($request->all(), [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|confirmed|unique:users,email',
'email_confirmation' => 'required'
]);
// Validate the input and return correct response
if ($validator->fails())
{
return response()->json(array(
'success' => false,
'errors' => $validator->getMessageBag()->toArray()
), 422);
}
This would give you a JSON response like this:
{
'success': false,
'errors': {
'first_name': [
'The first name field is required.'
],
'last_name': [
'The last name field is required.'
]
}
}

Laravel routing to controller - routing to wrong route

I have a very strange bug.
Running Laravel 4.1 as my company hasn't upgraded their PHP version yet.
The routes file has this:
Route::controller('survey', 'SurveyController');
And, when you go to /survey/new-survey it ends up being sent to postNewQuestion function instead of postNewSurvey.
public function postNewSurvey() {
$input = Input::all();
//dd($input);
$rules = array(
'name' => 'required|unique:lime_surveys_languagesettings,surveyls_title',
'language' => 'required'
);
...... etc
}
public function postNewQuestion() {
$input = Input::all();
//dd($input);
$rules = array(
'sid' => 'required',
'gid' => 'required',
'type' => 'required',
'question' => 'required',
'mandatory' => 'required',
);
$validator = Validator::make($input, $rules);
if($validator->fails()) {
return Response::json(array('success' => false, 'error' => 'validation',
'messages' => $validator->messages(), 'failed' => $validator->failed()));
}
......... etc
}
Thanks.

Categories