Laravel Defender (Spam protection) using OLD and display errors - php

I'm using https://github.com/thomastkim/laravel-spam-prevention but I want to display validator messages and old content back to user but Idk if it's possible if names are randomized. What I'm currently doing:
Example of input:
<div class="form-group{{ $errors->has(Defender::get('firstname')) ? ' has-error' : ''}}">
<label for="{{ Defender::get('firstname') }}" class="control-label">First name:</label>
<input type="text" name="{{ Defender::get('firstname') }}" value="{{old(Defender::get('firstname'))}}" class="form-control" id="{{ Defender::get('firstname') }}">
<span class="text-danger"> {{ $errors->first(Defender::get('firstname'))}} </span>
</div>
and in my request there is rule:
Defender::get('firstname') => 'required',
and there is message:
Defender::get('firstname').'.required' => 'You must enter your first name',
But obviously when form gets submited {{ $errors->first(Defender::get('firstname'))}} this won't be same as before... So any idea what I should do here in order to display messages to user and get old input ?

Related

old() value function using data from database not working in Blade

I have a form with 4 input fields, all of them to which I applied an old() function so whenever users load the page after submitting the form, the data that they filled in would retain in the fields for better UX.
My issue is that out of these 4 fields, only 1 has the old() function working. The difference between this field and the others is that:
The name and id attributes for this field are the same, while other fields have different names and IDs respectively.
The input type for this field is type="number" while the other fields are type="text".
Based on these differences, I tried modifying the code to standardize them, and only one solution works which is that I change the input types to "number", however, not all my inputs are numbers, so it is not an ideal solution.
Form blade file:
<div class="form-group mb-3">
<input type="text" class="form-control" name="emp_name" id="company_name"
value="{{ old('emp_name' , (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')) ) }}">
#if ($errors->has('emp_name'))
<span class="text-danger">{{ $errors->first('emp_name') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="number" class="form-control" name="emp_length" id="emp_length"
value="{{ old('emp_length' , (isset($user->emp_detail->emp_length) ? $user->emp_detail->emp_length : old('emp_length')) ) }}">
#if ($errors->has('emp_length'))
<span class="text-danger">{{ $errors->first('emp_length') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="text" class="form-control money-prefix" name="emp_monthly_gross_salary" id="monthly_salary"
value="{{ old('emp_monthly_gross_salary' , (isset($user->emp_detail->emp_monthly_gross_salary) ? $user->emp_detail->emp_monthly_gross_salary : old('emp_monthly_gross_salary')) ) }}">
#if ($errors->has('emp_monthly_gross_salary'))
<span class="text-danger">{{ $errors->first('emp_monthly_gross_salary') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="text" class="form-control money-prefix" name="monthly_allowance" id="bonus"
value="{{ old('monthly_allowance' , (isset($user->emp_detail->emp_monthly_allowance) ? $user->emp_detail->emp_monthly_allowance : old('monthly_allowance')) ) }}">
#if ($errors->has('monthly_allowance'))
<span class="text-danger">{{ $errors->first('monthly_allowance') }}</span>
#endif
</div>
To be clear, I also have several other forms in which I applied the same old() function, with texts as well as numbers, and they work fine, so I am unsure why the old() function won't work on this form specifically. I also dd() the variables I wanted to output as the old function, so I am sure my controller is already passing the values, my blade just isn't showing it.
Can anyone advise me on this problem?
maybe the issue is the default value, may be set but empty
try to check it with old directly without the default value
ex:
replace
{{ old('emp_name' , (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')) ) }}
with
{{ old('emp_name') }}
if it works fine with you then you need to enhance the default value condition
ie: (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')

How to show error message below input field in blade file - Laravel

I am validating input using rules in laravel, Currently, I am using the below code to show errors in my view if validation fails.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
The problem with the above code is all the errors are coming in one place. How can I show errors below their respective inputs (Like Name, Email, Password, etc). And where can I define my custom error messages? Thanks
As of Laravel 5.8.13, you can use the #error blade directive. Just place your desired error markup below your input field:
#error('field-name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
If there was an error validating field-name, the message related to the error will be displayed.
As for customising your validation error messages, check the Laravel documentation on Custom Error Messages for further information.
You can use like that for every field
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}"
name="name" value="{{ old('name') }}" autofocus>
#if ($errors->has('name'))
<span class="invalid feedback"role="alert">
<strong>{{ $errors->first('name') }}.</strong>
</span>
#endif
</div>
</div>

Modal doesn't redirect to login after failed login

I have the following HTML inside of my view. It's using a boostrap modal. It works fine for logging-in and redirecting, but upon a failed login attempt it's not redirecting the user back to te actual login page. action="{{ route('login') }}"> shouldn't this be taking the user back to the login page?
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<div class="col-md-12">
<input id="email" type="email" placeholder = "Email address.." class="form-control" name="email" value="{{ old('email') }}" required autofocus>
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<div class="col-md-12">
<input id="password" type="password" placeholder = "Password" class="form-control" name="password" required>
</div>
</div>
</div>
<p class = "small" style="padding:10px;">By signing up, you are indicating that you have read and agree to the Terms of Use and Privacy Policy.</p>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Enter</button>
</div>
</form>
The actual redirecting problem might be caused by the called controller.
Please check the function/method behind the post login and check if you have sth. like
return Redirect::back()->withErrors(['msg', 'The Message']);
at the end of the validation.
Here you can find a related post about redirecting.
For further informations about redirecting have a look at the documentation. There are also pretty good examples about the redirect function with parameters

Merge create and edit blade files to 1 file?

I have created a create form create.blade.php and a edit form edit.blade.php - both form fields are identical.
It quite annoying when I add new fields in the create.blade.php, I also need to add fields in edit.blade.php.
How can I merge into 1 blade file it so it work for both way for creating and editing (view via db).
For example in create.blade.php I have this:
<label>Name</label>
<input type="text" name="name" value="{{ old('name') }}">
#if ($errors->has('name'))
<label class="error">{{ $errors->first('name') }}</label>
#endif
In edit.blade.php
<label>Name</label>
<input type="text" name="name" value="{{ old('name', $category->name) }}">
#if ($errors->has('name'))
<label class="error">{{ $errors->first('name') }}</label>
#endif
Just check if model exists:
{{ old('name', empty($category) ? '' : $category->name) }}
Also, you may want to check if it's create or update form and change form url accordingly:
{{ route(empty($category) ? 'article.create' : 'article.update' ) }}
<input type="text" name="name" value="{{ $category ? $category->name : old('name') }}">
you can use a partial (fancy name for a smaller template) for your form, and pass that partial the data you require.
Using the example of a booking system:
In your create blade file:
#include('bookings._forms', ['booking' => new \App\Booking])
and the edit file ($booking being retrieved on the controller):
#include('bookings._forms', ['booking' => $booking])
And then your form partial can look like this:
<label for="name" class="label">Name</label>
<input type="text"
class="input"
name="name" id="name"
placeholder="Guest name"
value="{{ old('name') ?? $booking->name }}">
The good thing with this method is that now you can have ONLY your form elements on the partial and only have to update 1 file when you add fields, AND the actual tag stays on your create and edit template, so no worries about that either.
Hope that's clear and it helps.

How to Display Validation Errors Next to Each Related Input Field in Laravel 5?

Default solution is trivial:
#if (count($errors) > 0)
<ul id="login-validation-errors" class="validation-errors">
#foreach ($errors->all() as $error)
<li class="validation-error-item">{{ $error }}</li>
#endforeach
</ul>
#endif
and I can include errors.blade.php anywhere.
Is there any way to extract each element and display it next to input field that holds the value that failed?
I assume that would require me to define a lot of conditional if statements next to each input, right?
How to sort this problem? Could you give me any examples?
Thanks.
You can use something like this :
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
<label for="name" class="col-sm-3 control-label">Name: </label>
<div class="col-sm-6">
<input class="form-control" required="required" name="name" type="text" id="name">
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
#Zorx has given a right solution. But what if there are multiple errors and you want to display all of them at once.
According to the documentation you could use:
Retrieving All Error Messages For A Field
foreach ($errors->get('email') as $message) {
//
}
If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:
foreach ($errors->get('attachments.*') as $message) {
//
}
Retrieving All Error Messages For All Fields
foreach ($errors->all() as $message) {
//
}
Laravel Introduced The #error Directive in version 6 and 7
<input id="title" type="text" name="title" class="#error('title') is-invalid #enderror">
#error('title')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
documentation
Validation individual error message and input border with red color using bootstrap classes and Laravel's $errors directive
Input field with red border color
<input type="text" name="fullName" class="form-control {{($errors->first('fullName') ? 'is-invalid' : '')}}" value="{{old('fullName');}}">
//Or
<input type="text" name="fullName" class="form-control #error('fullName') is-invalid #enderror" value="{{old('fullName');}}">
Individual validation error message
#error('fullName') <div class="alert alert-danger">{{ $message }}</div> #enderror

Categories