How oin blade form show old value? - laravel

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!

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.

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

Form blade, can't understand translating from blade to html

I have this form in blade:
{{ Form::open(['action' => ['SearchController#searchUser'], 'method' => 'GET']) }}
{{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter name'])}}
{{ Form::submit('Search', array('class' => 'button expand')) }}
{{ Form::close() }}
How can I translate it into html?
If you're using Laravel 5.2, you must install HTML and Form as module.
follow these steps and it must be rendered your code:
https://laravelcollective.com/docs/5.2/html
If you are new to Laravel and feel its confusing with all those syntax you can use normal HTML that you already know. Its not a compulsion to use Laravel's blade shortcut.So something like
<form action="SearchController#searchUser" method="get">
//All your inputs
</form>
And to use form in your project you have to get a facade. You can follow the tutorial below :
http://www.easylaravelbook.com/blog/2015/02/09/creating-a-contact-form-in-laravel-5-using-the-form-request-feature/

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