Laravel how to translate the name of an input field - php

In my laravel-application I have some input fields like this:
<input name="phone" type="tel" pattern="^[0-9-+s()]*$" class="form-control" placeholder="Telefonnr." required>
<input name="subject" type="text" class="form-control" placeholder="Thema" required>
...and so on
and in my Controller I have this:
$validator = Validator::make(request()->all(), [
'email' => ['required', 'email'],
'name' => ['required', 'string'],
'phone' => ['string'],
'subject' => ['required', 'string'],
'message' => ['required', 'string'],
'conditions' => ['accepted', 'boolean'],
]);
So far so good, but now I want to customize the error messages. Right now, the error messages are like:
"subject muss ausgefüllt sein"
or
"phone muss angegeben sein"
So, how can I change it to:
"Thema muss ausgefüllt sein"
or
"Telefonnummer muss angegeben sein"

You can set custom message in third param of make ..
$validator = Validator::make(
request()->all(), // data
[ //rules
'email' => ['required', 'email'],
'name' => ['required', 'string'],
'phone' => ['string'],
'subject' => ['required', 'string'],
'message' => ['required', 'string'],
'conditions' => ['accepted', 'boolean'],
],
[ //messages
'subject.required' => 'Thema muss ausgefüllt sein'
]);
Source Code https://github.com/illuminate/validation/blob/3ec97a34466541c5802c5da44c831499e32c28ef/Factory.php#L98

$messages = [
'subject.required' => 'Telefonnummer muss angegeben sein',
'subject.string' => 'YOUR CUSTOM MESSAGE',
];
$validator = Validator::make($request->all(), [
'email' => ['required', 'email'],
'name' => ['required', 'string'],
'phone' => ['string'],
'subject' => ['required', 'string'],
'message' => ['required', 'string'],
'conditions' => ['accepted', 'boolean'],
], $messages);

Related

how can i send an email when user register but to the specific email in Laravel 8

I need when a user registers successfully to send notification email to the reference email field
the notification include one line text with link buttom and when he click on the buttom will redirect to my website
I need to send that notification to the referance_email field
public function create(array $input)
{ $massage = ['tc_no_pasaport_no.unique'=> 'Sorry, internal error .',
'phone.unique'=>'Sorry, internal error .' ];
Validator::make($input, [
'phone' => ['required', 'string', 'max:255','unique:users'],
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'tc_no_pasaport_no' => ['required','max:11','unique:users'],
'place_of_birth' => ['required'],
'date_of_birth' => ['required'],
'educational_status' => ['required','string'],
'school_department' => ['required'],
'address' => ['required'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
],$massage)->validate();
$users = User::where('email', '=', $input['email'])->first();
if ($users === null) {
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'phone' => $input['phone'],
'password' => Hash::make($input['password']),
'membership_status' =>'pasif',
'membership_type' =>'standard',
'last_activation_date' => Carbon::now(),
'membership_end_date' => Carbon::now(),
'temporary_id' => random_int(1000000, 9999999),
'tc_no_pasaport_no' => $input['tc_no_pasaport_no'],
'place_of_birth' => $input['place_of_birth'],
'date_of_birth' => $input['date_of_birth'],
'educational_status' => $input['educational_status'],
'school_department' => $input['school_department'],
'Institution_and_unit' => $input['Institution_and_unit'],
'address' => $input['address'],
'referance_email' => $input['referance_email'],
'letter_of_Intent'=>$input['letter_of_Intent'],
'created_at' => Carbon::now(),
]);
}
You can use PHP Mailer library to send email in PHP.

how to make name unique if foreign key is same in laravel validate

I have two tables "rooms" and "beds" i want to unique bed name when room_id is same. what should i do.
public function store(Request $request)
{
$roomid = $request->input('room_id');
//
$attributes = request()->validate([
'room_id' => ['required', 'integer', 'min:1'],
'name' => ['required', 'string', 'min:1', 'max:10', 'unique:App\Models\bed,name,room_id' . $roomid],
'type' => ['nullable', 'string', 'min:1', 'max:10'],
'description' => ['nullable', 'string', 'min:1', 'max:20']
]);
bed::create($attributes);
}
~~~
You should use closures instead of a rule object because this validation doesn't seem to be necessary throughout your application and you need it only here.
Change your validation to this:
$attributes = request()->validate([
'room_id' => ['required', 'integer', 'min:1'],
'name' => [
'required', 'string', 'min:1','max:10',
function ($attribute, $value, $fail) {
// Checke if name is unique in that room
$name_exists = \App\Models\Bed::where('name', $value)->where('room_id', request()->input('room_id'))->count() > 0;
if ($name_exists) {
$fail('The '.$attribute.' must be unique in this room.');
}
}
],
'type' => ['nullable', 'string', 'min:1', 'max:10'],
'description' => ['nullable', 'string', 'min:1', 'max:20']
]);
Source
solved
use Illuminate\Validation\Rule;
public function store(Request $request)
{
$roomid = $request->input('room_id');
$attributes = request()->validate([
'room_id' => ['required', 'integer', 'min:1'],
'name' => [
'required',
'string',
'min:1',
'max:10',
Rule::unique('beds')
->where(function ($query) {
return $query->where('room_id', request('room_id'));
})
],
'type' => ['nullable', 'string', 'min:1', 'max:10'],
'description' => ['nullable', 'string', 'min:1', 'max:20'],
]);
bed::create($attributes);
}

Laravel validation rules are not applying properly

I'm trying to apply these validation rules to my controller function, but any of the rules are not applying
Here is my code
if($request->hasFile('propic'))
{
$this->validate($request, [
'name' => 'required', 'alpha','min:2', 'max:255',
'last_name' => 'required', 'alpha','min:5', 'max:255',
'mobile' => 'required', 'string','min:10','max:14', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/',
'email' => 'required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.'',
'propic' => 'required','image','mimes:jpeg,png,jpg,gif,svg','max:2048',
]);
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$setting->propic=$imageName;
$setting->name=$request->input('name');
$setting->last_name=$request->input('last_name');
$setting->mobile=$request->input('mobile');
$setting->email=$request->input('email');
$setting->update();
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
At time of writing, there's two accepted formats for passing in validation rules:
As an array of strings (note the square brackets, which is what you are missing currently):
$this->validate($request, [
'name' => ['required', 'alpha','min:2', 'max:255'],
...
]);
As a single pipe-delimited string:
$this->validate($request, [
'name' => 'required|alpha|min:2|max:255',
...
]);

Laravel: failed to open steam: No such file or directory

I added file upload feature to my controller but, it isn't working. I keep running into this error.
failed to open stream: No such file or directory
I've linked the storage, cleared cache and virtually everything I know, can someone help me figure what's wrong.
Controller
class CandidatesController extends Controller
{
public function create(Request $data)
{
try {
$this->validate($data, [
'middle_name' => ['required', 'string', 'max:255'],
'what_i_do' => ['required', 'string', 'max:255'],
'phone' => ['required', 'string', 'max:20'],
'age' => ['required', 'date', 'max:255'],
'gender' => [
'required',
Rule::in(['male', 'female', 'others']),
],
'religion' => ['nullable', 'string', 'max:255'],
'address_1' => ['required', 'string', 'max:255'],
'address_2' => ['required', 'string', 'max:255'],
'city' => ['required', 'string', 'max:255'],
'highest_qualification' => [
'required',
Rule::in([
'no formal education', 'primary school', 'secondary school', 'technical school', 'nce', 'nd1',
'nd2', 'bsc', 'pgd'
]),
],
'discipline' => ['nullable', 'string', 'max:255'],
'lga' => ['required', 'string', 'max:255'],
'state' => ['required', 'string', 'max:255'],
'country' => ['required', 'string', 'max:255'],
'status' => [
'required',
Rule::in(['hired', 'hunting', 'vacation']),
],
'skills' => ['required', 'string'],
'about' => ['required', 'string'],
'fb_url' => ['nullable', 'string'],
'twt_url' => ['nullable', 'string'],
'ig_url' => ['nullable', 'string'],
'ext_url' => ['nullable', 'string'],
'lnkd_url' => ['required', 'string'],
'img_url' => ['nullable', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
'cv_url' => ['nullable', 'mimes:pdf,doc,docx', 'max:4000'],
]);
if ($data->hasFile('img_url')) {
$user_img = $data->img_url;
$ext = $user_img->getClientOriginalExtension();
$pro_img = random_bytes(7).'.'.$ext;
$img_path = $data->file('img_url')->storeAs('public/pics', $pro_img);
} else {
$img_path = 'default_img.jpg';
}
if ($data->hasFile('cv_url')) {
$user_cv = $data->file('cv_url');
$cv_ext = $user_cv->getClientOriginalExtension();
$cand_cv = random_bytes(7).'.'.$cv_ext;
$cv_path = $data->file('cv_url')->storeAs('public/cvs', $cand_cv);
} else {
$cv_path = 'no file uploaded';
}
return redirect('/');
} catch (Illuminate\Database\QueryException $th) {
return redirect('/candidate-register')->withError($th->getMessage())->withInput();
}
}
}
Don't mind me validating in the controller; I'm just trying to get everything working before cleaning it up

laravel 5.2 Method [validate1] does not exist

My validation fails with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers',$organiser->id,'organisers_id'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
but it works with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
This:
'unique:organisers',$organiser->id,'organisers_id'
needs to be:
'unique:organisers,'.$organiser->id.',organisers_id'
or (note the double-quotes):
"unique:organisers,{$organiser->id},organisers_id"
The , means "new array element", the . means "add to this string".

Categories