laravel 5.2 Method [validate1] does not exist - php

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".

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.

Laravel array distinct validation

I have the following request validation rules in an controller.
public function __invoke(Request $request)
{
$this->validate($request, [
'name' => ['required', 'unique:games,name', 'string'],
'variant' => ['required', 'string'],
'rules' => ['string'],
'is_anonymous' => ['boolean', 'required'],
'message_mode' => ['required', new EnumRule(MessageModeEnum::class)],
'powers' => ['required', 'array'],
'powers.*' => ['distinct:ignore_case', 'required', 'string'],
]);
}
Below is the test I'm running against the controller. I would expect the powers.* validation to throw an error as the two powers are equal and not distinct.
/**
* #test
*
*/
public function endpoint_validation()
{
$user = User::factory()->create(['rank' => RankEnum::A()]);
$response = $this->actingAs($user)->post(route('game.create'), [
'name' => '::name::',
'variant' => '::variant::',
'rules' => '::rules::',
'is_anonymous' => false,
'message_mode' => 'none',
'powers' => [
'::power::', '::power::'
],);
$response->assertSessionHasErrors('powers');
}
However, this test is failing. What am I doing wrong? How can I fix this?

Unique field validation issue in laravel

I'm trying to validate a unique entry in my laravel app
following is my validation array,
$website = $websiteModel->find($id);
$this->validate($request, [
'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
'subDomainSuffix' => ['required'],
'packageType' => ['required'],
'themeid' => ['required'],
'lang' => ['required'],
'user' => ['required'],
'domain' => [
'required',
'string',
'min:2',
'max:255',
Rule::unique('apps')->ignore($website)
],
], $request->all());
My validation working properly BUT,
When i tried to enter a duplicate value for my domain field, It get validated properly but not showing the error message, saying sorry the name is already exists...
<input type="text" id="domain" class="form-control" name="domain" >
{!! $errors->first('domain', '<span class="help-block" role="alert">:message</span>') !!}
Here in this span it shows nothing but in the common error message area it shows sorry the form cannot be updated... So how can I validate the field properly and display the relevant error message
Do something like this:
On insert request use
'domain' => [
...
'unique:websites,domain'
]
On update request use
'domain' => [
...
"unique:websites,domain,{$this->website->id}"
]
Or
'domain' => [
...
Rule::unique('websites', 'domain')->ignore($this->website)
]
You passed $request->all() as validation messages.
Please Try:
$website = $websiteModel->find($id);
$request->validate([
'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
'subDomainSuffix' => ['required'],
'packageType' => ['required'],
'themeid' => ['required'],
'lang' => ['required'],
'user' => ['required'],
'domain' => [
'required',
'string',
'min:2',
'max:255',
Rule::unique('apps')->ignore($website)
],
]);
don't you need to pass duplicate column in ignore Rule To instruct the validator to ignore the website domain, except for it self ? for example like
Rule::unique('apps')->ignore($website->id)
please try this one . it helps to solve your problem
use exception and validator in top of the file
use Exception;
use Validator;
$rules = [
'subDomainName' => 'required|unique:sub_domain_name',
];
$validator = Validator::make($request->all(), $rules, $message);
if ($validator->fails()) {
throw new Exception(implode('\n', $validator->errors()->all()));
}
sub_domain_name : this is database column name

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 how to translate the name of an input field

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);

Categories