Showing validation errors to view - php

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

Related

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?

Accessing object properties in blade when using table

I have created a form where an Admin can create text and also use objects as used in Blade. I want to store the text in a table and then display it in HTML with the objects working properly.
For example, I would have a form with this input in my view
<div class='form-group'>
<textarea placeholder="" name='comments' type='' rows='10' class='form-control' id='' value = '{{ old('comments') ?? $plansubmission->comments }}'>{{ $plansubmission->comments }}</textarea>
<div>{{ $errors->first('comments') }}</div>
</div>
In that form input, I have entered the following:
Dear Employee, {{ $plansubmission->id }}
This input validates and the input is sent to comments column in the proper table in my database.
Now, I want to return the comments column back into the view with all the spacing that was submitted into the input (therefore, I use the 'pre' tags):
<pre> {{ $plansubmission->comments}} </pre>
The plan text and spacing is maintained but the blade part simply comes out as {{ $plansubmission->id}} instead of what the actual property is.
It's a major security problem to let users submit blade templates to display data. Blade is compiled to PHP so you would be essentially allowing users to execute any PHP code they want. I would recommend you use something like mustache to let users inject variables into the output.
In the controller that passes the data to the view, you can pass the $plansubmission->comments through a mustache parser. This will treat the comments field as a template, and the second parameter sets the variables that the template has access to. This way you can explicitly set what the comment template has access to so you don't let users leak more data than is required.
$m = new Mustache_Engine;
$comments = $m->render($plansubmission->comments, $plansubmission->toArray());
Then users can put something like this in the comments field
Dear Employee, {{ id }}
Then in the view do
<pre> {{ $comments }} </pre>
and it will output
<pre> Dear Employee, 123 </pre>

Laravel - Show information from 2 tables

I create a customer with the CRUD from Laravel. I add some base data like "Name, Birthday, Sex etc.).
Now I use the "show" route from Laravel and see the data that I saved in the database table (kundens).
From this view, I generate a PDF with a button. All this works for me. All the data come from my table "kundens". The view data come from this table, too. But how can I show data from more than one table in the same "view"? I need to add some more data in the PDF - not only from the "kundens" table.
My code for the customer view is this:
<div class="row">
<div class="col-md-12 col-md-offset-12">
<h4>Übersicht</h4>
<h5>Persönliche Daten</h5>
{{ $kunden->vorname }}<br>
{{ $kunden->nachname }}<br>
{{ $kunden->strasse }}<br>
{{ $kunden->plz }}
{{ $kunden->wohnort }}<br>
<button class="btn btn-danger" style="color: #fff"><a style="color: #fff" href="{{asset('admin/generate_offer')}}{{ '/'.$kunden->id }}">Angebot erstellen</a></button>
<ul>
#foreach($kunden['offer'] as $offer)
<li>{{$offer->id}} </li>
#endforeach
</ul>
</div>
</div>
There you see that I take the information from $kunden - but I need to take more information that saved in another table.
Does anyone know a solution?
So first of all, you have to write your model and controller so we can see it better than what you write but here, for example, you want to make some other model, to show up here. So here is how you do it:
In your main model, kundens model you write like this:
public function somedata() {
return $this->hasOne('App\YOURAPP','id','Table_id'); // in case you have the name of tables as ID not the Table_id you must not the second and third argument here
}
and in your controller, you must write like this:
$kundens= Kundens::with('somedata')->get(); // here some data is the name of the function that u inserted in your controller
So you have to customize this a bit into your needs.
I hope this helps...
Apparently, you are generating a PDF file in the admin/generate_offer related controller.
So in order to have more data, just get your data in that controller (that is responsible for generating PDF file) and simply add all of them no matter what and how many tables you want to get.

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

Categories