Laravel - what does this bit of code mean? - laravel

<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

Related

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 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'])

How to avoid assigning the index of selected drop box item when inserted to db instead of actual value in drop box?

{!! Form::select('subject', array('' => 'Select A Subject')+$subject, null,['class' => 'form-control']) !!}
Above is the drop box code. That takes values from db and display correctly at the drop box. But after it is selected it gets the index.The inspect of that output as follows.
<select class="form-control" name="subject"><option value="" selected="selected">Select A Subject</option><option value="1">English</option><option value="3">Environmental Studies</option><option value="2">Maths</option></select>
How to fix that problem?
Try This:
$subList = ['' => 'Select A Subject'] +$subject;
{!! Form::select('name', $subList , Input::old('name') , array('class'=>'form-control')) !!}

How can I include html within a form label using Laravel Collective?

Reading through this SO thread I have read that I can create a new macro to create custom form inputs.
I am brand new to Laravel development (big surprise) & it seems a tad overkill for such a small thing. Is there a "simpler" way to have something like this:
blade template
{!!Form::label('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}
html
<label for="firstName">First Name*</label>
<input type="text" name="firstName" class="required">
Or, is this a case where I just write the html, and then use the form service to create the inputs?
Thank you for your patience and insight.
The simple way to do this is
{!! Form::label('labelFor','labelText',[],false) !!}
the last parameter is $escape_html which default value is "true".
The Form class attributes will always be escaped (they were in Laravel 4 and still are with Laravel Collective's 5+ support), therefore no HTML is allowed. Since you're needs are so simple, I'd suggest just writing plain HTML.
If you want overkill, maybe something like this in your AppServiceProvider.php:
Form::macro('labelWithHTML', function ($name, $html) {
return '<label for="'.$name.'">'.$html.'</label>';
});
Then, in your Blade templates:
{!! Form::labelWithHTML('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}
{!! Html::decode(Form::label('email', 'E-Mail Address', ['class' => 'text-muted'])) !!}
that is much better to fix these type of problems
Maybe it's late to answer, but you could do this:
{!! Html::decode(Form::label('firstName','FirstName: <sup>*</sup>')) !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}

Laravel form binding

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.

Resources