Laravel 7: Validation error message is not showing using key - php

I want to show validation errors right next to the field.
Therefore I am using this the #error directive.
#error('service_date')
<div class="error">{{ $message }}</div>
#enderror
But the error only shows up when iterating over all of them.
#if($errors->any())
{{ implode('', $errors->all('<div>:key - :message</div>')) }}
#endif
Using the above method, I can see that the key is correct. The output will be as follows:
service_date - The service date is not a valid date.
Other errors in the same form get displayed correctly. Why is behaving like this and how can I fix it?

Related

Laravel with input respond

I am trying to achieve callback message on redirect if you done something. I found that it can be passed like this (in Controller):
return redirect()
->route('users')
->withInput()->with('status', 'Something Updated!');
How it can be achieved in other end after being redirected?
My first question on slack - ty, guys :)
On the blade file, you can get the value in with using session. Here is the example from the official docs
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif

Laravel does not display my errors correctly

I've a little problem with Laravel, i'm displaying my errors like that :
{!! $errors->first('email', '<small class="help-block">:message</small>') !!}
But the error I get is : validation.unique
Do you know the reason ? Is it because i must to write all errors by myself ?
Thanks by advance ;)
I think if you need just error message then you do not need to pass additional field. :message
You can do it by just passing column name.
{{ $errors->first('email') }}
Laravel validation
Try this one
#if($errors->has('email'))
{{ $errors->first('email') }}
#endif

Why is my variable displaying incorrect value in Laravel 5.4 Blade file?

I have a variable $country_code that is displaying the correct value in one part of my form but not in a different part. Why is this happening?
This is my code:
{{ Form::open(['action' => ['PinVerificationController#valid'],'id'=>'pin_code_form']) }}
//$country_code shows 1
We sent a text message to {{$country_code}} {{$phone_number}}. You should receive it within a few seconds.<br><br>
{{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
{{ Form::hidden('country_code', $country_code) }}//<------shows 1-US instead of 1
{{ Form::hidden('phone_number', $phone_number) }}
{{ Form::hidden('type', $pin_notification_type) }}
{{ Form::text('pin_code', null,['placeholder' => 'Pin Code'])}}<br><br>
Enter a 4 digit pin you received by phone.
<br>
<br>
{{ Form::submit('Verify',['name'=>'validate'])}}
{{ Form::close() }}
So if I set $country_code to "1" in my controller it'll display We sent a text message to 1 5555555. You should receive it within a few seconds.
But if I do an inspect element on my hidden form it displays 1-US. I've tried php artisan view:clear and php artisan clear-compiled but the problem still persists.
I've also tried hardcoding a value {{ Form::hidden('country_code', 'asdf') }} and i'm not seeing the change. I tried adding a test {{ Form::hidden('country_code1', 'asdf') }} and see the update.
I also renamed country_code to country_code111 for my hidden field and it displayed the correct value of 1. I thought it was a caching issue but like I mentioned I've tried php artisan cache:clear and the problem is still there.
Since you are using Laravel 5.4, I assume you are using Form from the LaravelCollective, since they were removed from baseline Laravel in 5.x.
LaravelCollective Forms will override the value you provide to the input if it exists in the request data, or in old posted data (the old() function). I suspect this is the case for you.
You can see this behavior implementation here.
To solve this problem, you have a few options:
change the name of the request parameter feeding into the page (if you have control over it)
rename your field name to something that doesn't conflict
Don't use Form:: to generate the form and just use classic html/Blade to create the hidden input automatically
Personally, I would recommend #3 because then you have full control over your code.
<input type="hidden" name="country_code" value="{{ $country_code }}"/>

How can I add a success flash message for the password reset functionality of Laravel?

I'm using Laravel 5.1 and the built-in auth functionality. When testing it, however, I noticed there is no "Success" message when a user fills out the forgotten password form, the page simply refreshes, although the form does work.
How can I set a success variable for the forgotten password tool? I can't find the method that controls this anywhere.
Thanks
If you’re using Laravel’s built-in password reset functionality, then it does have a success message. You can see it here: https://github.com/laravel/framework/blob/5.1/src/Illuminate/Foundation/Auth/ResetsPasswords.php#L95
You merely need to listen for it in your view and display it if it’s there:
#if (session('status'))
<p class="alert alert-success">{{ session('status') }}</p>
#endif
This will display the default message, which is:
Your password has been reset!
If you want to change this message, just change the relevant line in resources/lang/en/passwords.php.
Simply paste this function in your ResetPasswordController
protected function sendResetResponse(Request $request, $response)
{
return redirect($this->redirectPath())
->with('success', 'Password changed successfully.');
}
And to get the message in your blade view, use this code
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif

Showing validation errors to view

I want to show the validation errors to the users in comma seperated
i.e.,
The username field is required, The password field is required
So far i can able to send the validation error messages to the view like this
$validation->messages()
But the only thing i can't able to do
#if(Session::has('Message'))
<p class="alert">{{ Session::get('Message') }}</p>
#endif
or
{{ $errors->first('username', '<div class="error">:message</div>') }}
The only thing i can do is to pass the messages as normal text.
So, How can i pass the validation messages to a view by plain text (rather than array or object)
Update :
I mean to say i can do any works only in controller and not in view
in controller:
return implode(',',$validation->errors()->all());

Categories