laravel how to validate as equal to a variable - laravel

in laravel validation (registering) i want to compare one of the fields with a php variable (it should be equal with that)
how can i do this?
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
}

You can do it for example for name field like this:
$variable = "something"
return Validator::make($data, [
'name' => [
'required',
Rule::in([$variable]),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Remember to import Rule Class (use Illuminate\Validation\Rule;)
You can get more info in: https://laravel.com/docs/5.4/validation#rule-in
EDIT
As suggested by #patricus, you can also concatenate the variable
$variable = "something"
return Validator::make($data, [
'name' => 'required|in:'.$variable,
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
EDIT2
If you have a variable that is an array:
$variable = ['one','two'];
return Validator::make($data, [
'name' => 'required|in:'.implode(",", $variable),
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Or
$variable = ['one','two']
return Validator::make($data, [
'name' => [
'required',
Rule::in($variable),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);

Related

Check if Stripe customer exists

I would like to check if a customer already exists so that I can update his details or if he does not exist create it by starting a new subscription.
I created this, but I believe there is something wrong.
if(Auth::user()->subscribed('default')) {
$stripe->customers->update(Auth::user()->stripe_id, array([
'email' => Auth::user()->email,
'name' => 'Example Example',
'phone' => '322234455556',
'address' => [
'city' => 'Alcatraz',
'line1' => 'Via Dalle Palle',
'postal_code' => '96011',
'state' => 'Italia'
]
]));
} else {
$stripe->customers->create([
'description' => 'Example Example',
'email' => Auth::user()->email,
'name' => 'Example Example',
'phone' => '322234455556',
'address' => [
'city' => 'Alcatraz',
'line1' => 'Via Dalle Palle',
'postal_code' => '96011',
'state' => 'Italia'
]
]);
}
I think there is a mistake, because when I go to create a new subscription with a newly registered user, therefore not present on Stripe as a customer, it does nothing but create me two entries as in the image below.
I solved it this way
$stripeCustomer = Auth::user()->createOrGetStripeCustomer([
'description' => 'Example Example',
'email' => Auth::user()->email,
'name' => 'Example Example',
'phone' => '322234455556',
'address' => [
'city' => 'Alcatraz',
'line1' => 'Via Dalle Palle',
'postal_code' => '96011',
'state' => 'Italia'
]
]);

how to exit when I return laravel Validation?

i' new to laravel here I have created a registration API and private function for validation but not show validation.
that is my validation function
`private function validateInfo($request, $toValidate) {
$validator = Validator::make($request->all(), $toValidate);
if ($validator->fails()) {
$errors = $validator->errors()->toArray();
$values = array_values($errors);
$arr = [];
foreach ($values as $errorlst)
array_push($arr, $errorlst[0]);
$error = implode(',', $arr);
return response()->json(['error' => true, 'message' => $error], 400);
exit;
}
}`
call function under switch case :-
switch ($request->user_type) :
case "teacher":
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;
it is not showing validation error. Any Mistake?
Thank You
instead of this you can create a form request to validate your request.
class UserRequest extends FormRequest
{
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(
response()->json([
"message" => implode(',',$validator->errors()->all())
],422));
}
public function rules(){
return [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
}
}
please check docs for more info https://laravel.com/docs/8.x/validation#form-request-validation
You Need to return For Display Like
switch ($request->user_type) :
case "teacher":
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;

Why is the style different? something is missing? [duplicate]

This question already has answers here:
How do I style the HTML form validation error messages with CSS?
(4 answers)
Closed 3 years ago.
I modified the code like this: ArticleRequest.php
public function rules()
{
return [
'title' => 'required|min:2|max:255',
'slug' => 'unique:articles,slug,'.\Request::get('id'),
'content' => 'required|min:2',
'date' => 'required|date',
'status' => 'required',
'category_id' => 'required',
'description'=> 'required',
];
}
ArticleCrudController.php
$this->crud->addField([
'name' => 'description',
'label' => 'Description',
'type' => 'text',
'placeholder' => 'Description meta tag',
'attributes' => [
'required' => true,
],
]);
Why is the style different? something is missing?
thanks, you are very kind
Marco
public function rules()
{
return [
'title' => 'required|min:2|max:255',
// you should add validation rule for description attr
'description' => 'required',
'slug' => 'unique:articles,slug,'.\Request::get('id'),
'content' => 'required|min:2',
'date' => 'required|date',
'status' => 'required',
'category_id' => 'required',
'description'=> 'required',
];
}

dump($request); does not work in this code. Laravel 5.6

The code does not have any syntax error, but does not work. It seems the code has no interaction with database.
public function execute(Request $request) {
if($request->isMethod('post')) {
$messages = [
'required' => "Campo :attribute rellenado",
'email' => "Este :attribute campo debe rellenarse con su email"
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
dump($request);
edit your rules
$messages = [
'name.required' => 'This Field Name is required ',
'name.max' => 'The name should be :max in length',
'email.required' => 'the email field is required',
'email.email' => 'the email should be valid email',
'text.required' => 'the the text is required',
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
the dump($requset) will not work in this case because you are using request trait where $this->validate function automatically redirect back if validation fails
Try to change dump($request) to dd($request)
if($request->isMethod('post')) {
$messages = [
'required' => "Campo :attribute rellenado",
'email' => "Este :attribute campo debe rellenarse con su email"
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
dd($request);

October CMS - required checkbox

How could I validate if the checkbox is ticked?
public $rules = [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'terms' => 'required',
];
'terms' is the name of the checkbox. Now I get the alert about required field even if it is checked.
HTML:
<input type="checkbox" name="terms" value="1" id="terms"/>
Simple HTML5 attribute required is also not working.
Wouter Van Damme has it right. You simply need to use the trait listed in that documentation. like so:
public $rules = [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'terms' => 'required|accepted',
];
here is an october specific link although the answers are the same.http://octobercms.com/docs/services/validation#rule-accepted

Resources