Laravel form binding - laravel

Hi I am using laravel form binding along with
jqBootstrapValidation . In order to successfuly have the validate the input fields, I must pass something like "required" (without quotes) in the tag . Can you please let me know how can I achieve this ?
FYI .. the minlength works fine but the required does not work.
For example one of input elements currently looks as such
{{Form::text('username', null, array('class'=> 'form-control tip', 'data-toggle'=> 'tooltip', 'data-placement'=> 'bottom', 'title'=>'Enter your username that you have been using till now. This is a compulsory field.','placeholder'=>'Username ( must be filled )','minlength'=>'2'))}}
Thanks

You can't, take a look at the source code that builds the attributes:
protected function attributeElement($key, $value)
{
if (is_numeric($key)) $key = $value;
if ( ! is_null($value)) return $key.'="'.e($value).'"';
}
You'll always get a <attribute>=<name>, what you can test is to use it this way:
{{ Form::text('username',
null,
array( 'class'=> 'form-control tip',
'data-toggle'=> 'tooltip',
'data-placement'=> 'bottom',
'title'=>'Enter your username that you have been using till now. This is a compulsory field.',
'placeholder'=>'Username ( must be filled )',
'minlength'=>'2',
0 => 'required'
)
)
}}
It will build a tag
<... required="required" ...>
And it might work for jqBootstrapValidation.
Otherwise you'll have to create those inputs manually.

I've used this with the HTML attributes required="required" and also required="" works too. Changing the example they provide and adding in the Laravel Blade tags gives
<form class="form-horizontal" novalidate>
<div class="control-group">
<label class="control-label">Type something</label>
<div class="controls">
{{ Form::text('name', null , array('required' => '')) }}
<p class="help-block"></p>
</div>
</div>
{{ Form::submit('submit')}}
</form>
This worked fine and produced the error "This is required". I swapped in your form field and added in the
'required' => ''
to the end of your attribute array and that too worked just fine. Alternatively you could instead add in
'required' => 'required'
as jqBootstrapValidation picks up either. Good luck.

Related

How can I receive a specific index of an validator array of array validation in Laravel?

I validate an array in laravel controller and receive message by using $errors->first('field_name') or $errors or $errors->has('field_name). Now the problem is, I validate an array named vaccine_certificate and I can not receive the error message. Actually I receive the message when show all erros at once. but I want to show it with it's input area. How can I solve it?
$this->validate($request, [
'name' => 'required',
'employee_no' => 'required',
'vaccine_certificate.*' => 'image|mimes:png,jpg|max:2048|dimensions:max_height=200,max_width=200',
//'expertise' => 'required',
]);
Input field looks like
<div class="col-6 form-group">
<label class="control-label " for="vaccine_certificate"><h5 class="h6">Vaccine Certificate <small>(<2mb,png,jpg) </small> </h5></label>
<input class="form-control" type="file" name="vaccine_certificate[]" id="vaccine_certificate" multiple/>
#if ($errors->has('vaccine_certificate'))
<p id="employee_no_checker_message" class="text-danger p-2" onload="changeInputBorderColor('vaccine_certificate')"> {{ $errors->first('vaccine_certificate') }}</p>
#endif
</div>
You can use and see all errors in the error bag of form.
$errors->all()
However, as far as I can see there is no "required" in vaccine_certificate, and your rule is vaccine_certificate.*, that means it should be an array. If you make it vaccine_certificate and required like you did in the name field you can see the error message.
If you need to use array, you can use:
$errors->first('vaccine_certificate.*')
Since it is not "required" it will not throw an error and you can not see it in the $errors variable.

How oin blade form show old value?

In laravel 8/ bootstrap 4 / laravelcollective/html 6.2 app
I use old in form field definition :
{!! Form::text(
'name',
!empty(old('name'))? Purifier::clean(old('name')): $user->name,
['placeholder' => 'Name','class' => 'form-control editable_field', 'autocomplete'=>'of']
) !!}
#error('name')
<p class="validation_error" role="alert">{{ $message }}</p>
#enderror
as it works ok, expect cases when from database value was read but cleared by operator:
then old value is visible.
I tried this way
isset(old('name'))? Purifier::clean(old('name')): $user->name,
But got error:
Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Can you advice proper way ?
Thanks!

Laravel - what does this bit of code mean?

<div class ="form-group">
{!! Html::decode(Form::label('first_name','<strong>First Name:</strong>')) !!}
{{Form::text('first_name', old('first_name'), ['class' => 'form-control '.($errors->has('first_name') ? 'is-invalid': ''), 'placeholder' => '', 'required'])}}
</div>
What does this bit of the code mean?
($errors->has('first_name') ? 'is-invalid': '')
"If the first_name field has errors, print is-invalid, otherwise print nothing."
Basically, it's applying the is-invalid class to fields that are invalid (per the Laravel validator). In Bootstrap, this typically turns the field red so the user can see it's incorrect.
That is called a Ternary. Shorthand for if/else
Here is post on those that is helpful:
https://davidwalsh.name/php-shorthand-if-else-ternary-operators

Laravel custom validation message for multiple fields with the same name

I have the following validation in the controller's action:
foreach ($request['qtys'] as $key => $val){
if (!$this->_validateMinQty($key, $job, $val)){
$customerTitle = $job->customers()->where('customer_id',$key)->first()->title;
return redirect()->back()->withErrors(['qtys' => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
}
}
This check multiple form's input named qtys in the view:
#foreach($job->customers as $customer)
<div class="form-group {{$errors->first('qtys has-error')}}">
{!! Form::label('qtys-'.$customer->id, __('Qty').' '.$customer->title) !!}
<div class="row">
<div class="col-md-9">
{!! Form::text('qtys['.$customer->id.']',$customer->pivot->e_production,['class' =>'form-control qtys', "data-sumequal"=>"qty",'required' => 'required','title' => $customer->pivot->aid,'id' => 'qtys-'.$customer->id]) !!}
<div class="help-block with-errors"></div>
#php ($eleE = $errors->first('qtys'))
#include('layouts.form-ele-error')
</div>
<div class="col-md-3">
<i class="fox-add"></i>{{__('Add Storage')}}
</div>
</div>
</div>
#endforeach
The above code works, but with the following limitation:
The error message is rendered under every input named qtys[x] where x is an integer and the first input only Testana has the invalid qty, like the following screen shot:
In the controller's action return message, I have tried to use indexed name for the input like the following:
return redirect()->back()->withErrors(['qtys.10' => ....
However, it prevents rendering the error message under any qtys field. Is there any solution?
The solution that I have found starts from the definition of first method found in the view :
#php ($eleE = $errors->first('qtys'))
This, in my code, should be changed to:
#php ($eleE = $errors->first('qtys.'.$customer->id))
Because the multiple fields have gotten keys equals to the customer id. This is a technique I usually use, when I want to send double piece of data in single post or in single form element.
Then in the controller, I keep the first try,
return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
Where $key is an integer.

Laravel 5: Check for Specific Error

I have Password and Confirm Password inputs. I have coded these inputs so that in the event of an error the inputs highlight, like so:
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}>
<label>Password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="form-group {{ $errors->has('password_confirmation') ? 'has-error' : '' }}>
<label>Password</label>
<input type="password" name="password_confirmation" class="form-control">
</div>
In my Controller, I validate these inputs as follows:
$this->validate($request, [
....
'password' => 'required|min:8|confirmed',
'password_confirmation' => 'required',
....
]);
When either input is null and/or less than 8 characters, the inputs highlight as expected. However, when the inputs don't match, I would logically expect the Confirm Password input to highlight. However, it is the Password field that highlights, because that is the input that the "confirmed" rule is set to.
The "$errors->has()" method simply checks if an error exists for an input. Is there a way to check if an input has a specific kind of error?
Yes, you can get the array of errors for a given input name by using:
{{ $errors->get('password') }}
So you can check if the password errors array has the confirmation error and then add the additional class to your confirmation input field:
#if (in_array(trans('validation.confirmed', ['attribute' => 'password']), $errors->get('password'))) has-error #endif
And the helper methods may vary between Laravel versions:
Laravel < 5.4: trans('validation.confirmed', ['attribute' => 'password'])
Laravel 5.4: __('validation.confirmed', ['attribute' => 'password'])

Resources