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

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

Related

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;

How to switch destination mail address by selecting check box in Laravel contact form

This is contact form and sorking.
I would like to switch email destination by User selecting check box.
For example. Here is a column called genders.
When user check 'man' select box, email destination will be [TO]'man_survey#12345677.site' and [CC] is 'man_cc_survey#12345677.site'.
When user check 'female', email destination will be [TO] 'female_survey#12345677.site' and [CC] is 'female_cc_survey#12345677.site'
Could you teach me how to add this function to my current code?
My Laravel framework is 5.7.28
public function complete(ContactRequest $request)
{
$input = $request->except('action');
if ($request->action === 'back') {
return redirect()->action('ContactsController#index')->withInput($input); }
if (isset($request->type)) {
$request->merge(['type' => implode(', ', $request->type)]);
}
// store data
Contact::create($request->all());
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => 'survey#12345677.site',
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
}
}
UPDATE THis worked
public function complete(ContactRequest $request)
{
$input = $request->except('action');
if ($request->action === 'back') {
return redirect()->action('ContactsController#index')->withInput($input); }
if (isset($request->type)) {
$request->merge(['type' => implode(', ', $request->type)]);
}
// store data
Contact::create($request->all());
if($request->gender == 'male') {
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'man_survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => $email,
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
} else {
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'female_survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => $email,
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
}
}
You can use a radio button in the form to see what Gender the user has selected.
In HTML
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="no_answer">Prefer Not to Answer
In the controller you can use something like this
public function complete(ContactRequest $request)
{
$input = $request->except('action');
if ($request->action === 'back') {
return redirect()->action('ContactsController#index')->withInput($input); }
if (isset($request->type)) {
$request->merge(['type' => implode(', ', $request->type)]);
}
// store data
Contact::create($request->all());
if($request->gender == 'male') {
$email = 'man_survey#12345677.site';
$cc_email = 'man_cc_survey#12345677.site'
} else {
$email = 'female_survey#12345677.site';
$cc_email = 'female_cc_survey#12345677.site'
}
// send mail
\Mail::send(new \App\Mail\Contact([
'to' => $request->email,
'to_name' => $request->name,
'from' => 'survey#12345677.site',
'from_name' => 'from name',
'subject' => 'Thank you',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
]));
// recive mail
\Mail::send(new \App\Mail\Contact([
'to' => $email,
'to_name' => 'to name',
'from' => $request->email,
'from_name' => $request->name,
'subject' => 'you got mail',
'type' => $request->type,
'gender' => $request->gender,
'body' => $request->body
], 'from'));
return view('contacts.complete');
}

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',
];
}

laravel how to validate as equal to a variable

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

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