I have created the form and created multiple fields.
<input name="members[]" type="text" class="form-control">
<input name="members[]" type="text" class="form-control">
<input name="members[]" type="text" class="form-control">
set the validation from the Form Request for the input fields
public function rules()
{
return [
'password' => 'required|max:30',
'members.*' => 'required|max:12',
];
}
How can we check the members' field value exists in the database using the validation?
For password using like this
'password' => ['required', function ($attribute, $value, $fail) {
if (!\Hash::check($value, $this->user()->password)) {
$fail('Old Password did not match to our records.');
}
}],
You want to use the exists validation rule.
Just extend your existing validation rules for members:
'members.*' => 'required|max:12|exists:{phone number table},{phone nummber column}',
Related
Is there a way I can check if a user didn't manipulate the form? In my form, I get all available entries, but if a user changes the value id in the browser, I would just get an error. Any tips:)?
<div class="form-group-row club col-lg-10">
<label>Choose Product</label>
<select name="product_id" class="form-control" required>
#foreach($products as $product)
<option value="{{$product->id}}">{{$product-> product}}</option>
#endforeach
</select>
</div>
You could use a Rule to validate the received data like :
use Illuminate\Validation\Rule;
Validator::make($data, [
'product_id' => [
'required',
Rule::in([/*array of products ids here*/]),
],
]);
Take a look at https://laravel.com/docs/5.8/validation#rule-in
You could use exists like :
Validator::make($data, [
'product_id' => [
'required',
'exists:table_name,column_name'
],
]);
you can do select readonly
<select name="product_id" class="form-control" required readonly>
but user can change html by devtools
also you can check it on backend side like this
if ($products->pluck('id')->diff(request()->input('product_id'))->empty()) {
//not changed
}
I used 5.4 and I've an index action in convert controller which shows the form and then have another action calculate in the convert controller. So the form has from-currency, amount, to-currency input and all of them are required.
Here's the validation I've for calculate action:
$this->validate(request(), [
'from_currency' => 'required|min:3|max:3|alpha',
'to_currency' => 'required|min:3|max:3|alpha',
'amount' => 'required|numeric',
]);
If the validation failed I want when showing the errors and the form it will prepopulate the input already.
Is there like a function I can use for Request ? I know how to get the domain/path inside blade like Request::root() and I also tried Request::input('from_currency) in the view but not work.
I even tried to set the view data like 'from_currency' => request('from_currency') and it's blank. Any idea?
When you are validating your form your request if it fail you can redirect to the same page with all the input which was submited
$validator = Validator::make($request->all(), [
'from_currency' => 'required|min:3|max:3|alpha',
'to_currency' => 'required|min:3|max:3|alpha',
'amount' => 'required|numeric',
]);
if ($validator->fails()) {
return redirect('index')
->withErrors($validator)
->withInput();
}
and in your blade view you can show the old value by ussing the old helper like this
<input type="text" name="from_currency" value="{{ old('from_currency') }}">
<input type="text" name="to_currency" value="{{ old('to_currency') }}">
<input type="text" name="amount" value="{{ old('amount') }}">
Try this
In your blade file, make sure your inputs have this:
<input type="text" ... value="{{ old('from_currency') }}" ... >.
Then in your controller...
if($validation->fails()) {
return redirect()->back()->withInput();
}
You can also user Validate instead of Validator::make.
eg
$this->validate($request, [
'question' => "required|min:10|max:100",
'answer' => "required|min:20|max:300",
'rank' => "required|numeric|gt:0|lt:100",
]);
Then in your form use
<input type="text" class="form-control" id="question" name="question" value="{{ old('question') }}">
This will automatically redirect back with input if the validator fails.
This way, you DO NOT have to include
if($validation->fails()) {
return redirect()->back()->withInput();
}
In Laravel, how do I do validation to check if checkbox is checked and text input is not empty.
For example:
If checkbox is ticked
<input type="checkbox" name="has_login" checked="checked" value="1">
and pin is not empty then validation should be passed.
<input type="text" name="pin" value="">
In Request file file:
public function rules()
{
return [
'has_login' => ??,
];
}
You should try this way:
public function rules()
{
return [
'has_login' => 'accepted',
'pin' => 'required',
];
}
I am creating referral system so I have the following routes
// Registration routes...
Route::get('auth/register/{id}', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
and my RegisterUser.php is changed to
public function getRegister($id)
{
return view('auth.register')->withName($id);
}
and my blade looks like
<div class="form-group">
<label class="col-md-4 control-label">Company</label>
<div class="col-md-6">
<input type="text" class="form-control" name="company" value="{{ old('company') }}" readonly disabled>
</div>
</div>
in AuthController I have:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'company' => $data['company'],
'password' => bcrypt($data['password']),
]);
}
and the value="{{ old('company') }}" is causing the problem. When it is like that it works. But I want the value to be value="{{$name}}" given from return view('auth.register')->withName($id); So when I go to route auth/register/something in the input field I have got the 'something' so it is working but I have the error code "Undefined index: company". When I remove the value at all it is working but I need this value. Any suggestions would be helpful.
The Problem of your code is the disabled attribute in the input "company", why ? well a disabled element isn't editable and isn't sent on submit. so Laravel doesn't receive it so you will be able to access via the helpers old.
Remove the disabled attribute and the magic happens.
<input type="text" class="form-control" name="company" value="{{ old('company') }}" readonly >
I have a form using cloneya jQuery plugin to clone form elements. The elements that will be cloned looks like so:
<div class="form-group">
<label for="name">Item name</label>
<input class="form-control" name="name[]" type="text">
</div>
<div class="form-group">
<label for="count">Item count</label>
<input class="form-control" name="count[]" type="text">
</div>
As you can see, each input will be an array instead of string. I want to validate those using Laravel Form Request. Here's my rules:
public function rules()
{
return [
'name' => 'required|between:3,50',
'count' => 'required|integer|min:1',
];
}
But that's not working. When I submitted the form, I got the following error message:
htmlentities() expects parameter 1 to be string, array given
I've been searching for solution, but can't find the appropriate one. Any suggestion would be appreciated!
Basically, in your rules() method, you need to determine how many name and count elements there are in the POST and then create rules for each of them:
public function rules()
{
$rules = [];
foreach ($this->request->get('name') as $index => $val) {
$rules['name.' . $index] = 'required|between:3,50';
}
foreach ($this->request->get('count') as $index => $val) {
$rules['count.' . $index] = 'required|integer|min:1';
}
return $rules;
}
Please check this post.