Laravel does not display my errors correctly - php

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

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 7: Validation error message is not showing using key

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?

Symfony 4: How to edit a given form control?

I can create a form view and even edit a submitted one.
But how can I do if I need for example to edit ONLY an user email.
Here's basically what I try:
{{ form_start(form) }}
{{ form_widget(form.id)}}
{{ form_widget(form._token) }}
{{ form_widget(form.email) }}
{{ form_widget(form.submit) }}
{{ form_end(form, {'render_rest': false}) }}
When I submit the form and dump the user after called $form->handleRequest($request), all the rest fields are reset to them default values except the ones I rendered in my twig view (id email).
So what the right way to achieve my goal?
I'm wondering what if I had differents FormType accordingly?
Dont know if this is the recommended way but it works!

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 }}"/>

Laravel php controller generate error

Why i am getting this error, here is my controller file,
squared code generate error, if I comment this code It'll save post to database
Here is error when i request to post, it give me error :
I think you are using wrong varaibale it should be $posts->is_public.
check your
{!! Form::open() !!}
{!! Form::close() !!}
under your
{!! Form::hidden('id', $posts->id) !!}

Categories