How do i set focus on an invalid input field - laravel

I have an input; it's a string type of input. When I put an invalid input on it, I want it to focus on that invalid input field because the input is wrong, and I have to do a correct input on that invalid input field. How do I do it??
<form action="{{url('/store')}}" method="POST">
#csrf
<div class="form-group">
<label>NIK</label>
<input type="text" id="kredit_nik" name="kredit_nik" class="form-control" autocomplete="off"
value="{{old('kredit_nik')}}" placeholder="Contoh : 3152021502002002">
</div>
#if ($errors->first('kredit_nik'))
<div class="alert alert-danger">{{$errors->first('kredit_nik')}}</div>
#endif
<br>
<div class="form-group">
<label>Nama Lengkap</label>
<input type="text" id="kredit_name" name="kredit_name" class="form-control" autocomplete="off"
value="{{old('kredit_name')}}" placeholder="Contoh : Nathanael Budiman">
</div>
#error ('kredit_name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
<br>
</form>

You can use Laravel's provided #error blade directive to check if the validation error exists for an input/attribute and combine it with Bootstrap's error class as:
#error('input-name') is-invalid #enderror
Example:
<input type="text" required class="form-control #error('input-name') is-invalid #enderror" name="input-name" id="input-name" value="{{ old('input-name') }}">
Now, to scroll to the error field you can use jquery to scroll to the first error field:
Add this before the closing </body> tag:
<script src="https://cdn.jsdelivr.net/npm/jquery#3.6.0/dist/jquery.min.js"></script>
<script>
$(document).ready(function() {
var errors = $('.is-invalid')
if (errors.length) {
$(document).scrollTop(errors.offset().top)
}
});
</script>
Scrolling code is partially taken from: https://github.com/1000hz/bootstrap-validator/issues/52#issuecomment-71472662 (original credit)

Related

Laravel 7- Pass database value or a value stored in a variable, to Laravel Blade

I am trying to obtain value from database and pass it to blade. If there is no value in database, a random number is generated and passed from the controller. However, I am unable to fetch the value even if it exists. I am getting 1 for every value available in database
Here are my codes
Controller
public function new_user2(Request $request){
if(!Auth::check()){
$reference = $request->refno;
if((is_null($reference))){
$ref1 = mt_rand(10,1000);
$ref2 = mt_rand();
$reference = $ref1."-".$ref2;
} else{
$tuser = DB::table('temp_users')->where('referenceno',$reference)->first();
if(is_null($tuser)){
return redirect()->back()->with('error',"Invalid Reference No. Please enter a valid one");
}else{
// return view('multipage1',compact('tuser'));
return view('multipage1',compact('tuser'));
}
}
return view('multipage1')->with('ref',$reference);
}else{
Auth::login();
}
}
Blade
<div class="body1">
<div class="container container2">
<div class="row">
<div class="col-md-8 offset-md-2">
<h1 class="text-white text-center display-2">Step 1</h1>
<div class="login-form">
<form id="newuser2_step1" action="{{url('mpage2')}}" method="post" autocomplete="off">
#csrf
<div class="form-group">
<label for="RefNo">Reference Number: </label>
<input type="text" class="form-control" id="refno" name="refno" value="{{$tuser->referenceno or $ref}}" readonly><br>
</div>
<div class="form-group">
<!-- Alert Message -->
<div class="alert alert-danger " role="alert" id="alerts">
<strong id="emailtaken"></strong>
</div>
<label for="exampleInputEmail1">Email address</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Enter email" value="{{$tuser->temp_email or ''}}">
<div class="error text-danger" ></div>
</div>
<button type="submit" class="btn btn2 btn-primary" id="mstep1">Get Started</button>
</form>
</div>
</div>
</div>
</div>
</div>
Database table
Data exists in referenceno and temp_email columns
Output in Browser
According to this article, the Laravel or operator that you used, is discontinued. So, use null coalesce operator instead.
value="{{$tuser->referenceno ?? $ref}}"
Similarly, replace the email value with similar coede
You have change this type of statements {{$tuser->referenceno or $ref}}
It is basically checking true/false that why you are getting 1.
Do something like this {{ isset($tuser->referenceno) ? $tuser->referenceno : $ref}}

Error when trying to get property 'name' of non-object

hi i am trying to show the value of name which is stored in database but i am getting this error can anyone please guide me how can i rectify my error .
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{ $Task ->name }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ $Task ->email }}" >
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
here is my edit controller
public function edit($Taskid)
{
//
$Edit= Superior::find($Taskid);
return view('Myview.edit')->with('Task','$Edit');
}
here is my route for edit
Route::get('Superior/{Task}/edit','SuperiorController#edit');
i am using Task as a key but it is giving me an error
i also tried but didn't work
{{$Task['name']}}
You should remove ' that wraps $Edit.
return view('Myview.edit')->with('Task', $Edit);
Change your code like this.
Form Code
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{isset($Edit) ? $Edit->name : '' }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ isset($Edit) ? $Edit->email : '' }}">
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
Route
Route::get('Superior/edit/{id}','SuperiorController#edit');
Controller Code
public function edit($id){
$Edit= Superior::find($id);
return view('Myview.edit',compact('Edit'));
}
Hopefully it'll solve the problem
Create function like this. if Taskid in your database column.
public function edit(Request $Request, $Taskid)
{
//
$Edit= Superior::where('Taskid', $Taskid)->first();
return view('Myview.edit', compact(Edit));
}
In your html it will like this
{{ $Edit->name }}
In your Route
Route::get('Superior/{Taskid}/edit','SuperiorController#edit');

how to display validations at angular2

I followed same sample with the code and tried to show validation when user removes the text in the input and show display messages.Unfortunately, when I remove the text field it does not show anything. Could you please check the code and tell me why I can not show the validation message ?
Regards
Alper
<label for="name">SA / Rentennummer 005 :</label>
<input type="text" class="form-control" id="name" required
[(ngModel)]="Input.name" name="name" #name="ngModel">
<div [hidden]="name.valid || name.pristine"
class="alert alert-danger">
name is required
</div>
In The typescript :
Input= { name:'Alper'};
form : FormGroup;
this.form = fb.group({
name : new FormControl({value: null}, Validators.compose([Validators.required, Validators.maxLength(100)]))
});
<form class="form-details" role="form" name="registrationForm" [formGroup]="userForm">
<div>
<div class="row input-label">
<label class="form-label" for="name">First name</label>
<input
[formControl]="form.controls['name']"
type="text"
class="form-control"
id="form"
name="form">
</div>
<div *ngIf="!form.controls['name'].valid">field is required</div>
</div>
</form>

Edit db record with modal window

I'm trying to edit some database record based on an ID that I'm saving into a button value.
#foreach ($employment as $empl)
<button data-toggle="modal" data-target="#edit-empl" href="#edit-empl" class="btn btn-default editbtn-modal" value="{{ $empl->id }}" type="button" name="editbtn">Edit</button>
<h3 class="profile-subtitle">{{ $empl->company }}</h3>
<p class="profile-text subtitle-desc">{{ $empl->parseDate($empl->from) }} - {{ $empl->parseDate($empl->to) }}</p>
#endforeach
As you can see here, I have an edit button with an id attached.
When I click edit I open a modal window to edit the fields and later on submit the form.
The thing is, I'm not sure how to get that id from the button into the modal window so I can compare the values and display the correct fields..
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="">
#foreach ($employment as $empl)
#if ($empl->id == buttonidhere)
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
#endif
#endforeach
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
I was able to pass the button value into the modal using javascript.. I put it into a hidden input but that doesn't help me at all because I can't get the input value in order to compare the values..
Solution 1: Using ajax
Step 1: Create a route in laravel which will return a JSON object containing employing data of requested employee.
For e.g,
/profile/employment/data/{empl_id}
Will get you employement data of id empl_id.
Step 2: Change your form as below
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
<input class="editID" type="hidden" name="editID" value="">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 3: Use javascript(jQuery) to get the data using ajax and load it into the form in modal.
jQuery code:
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$.ajax({
url: url,
method: "get"
}).done(function(response) {
//Setting input values
$("input[name='editID']").val(id);
$("input[name='company']").val(response.company);
$("input[name='to']").val(response.to);
$("input[name='from']").val(response.from);
//Setting submit url
$("modal-form").attr("action","/profile/employment/edit/"+id)
});
});
Solution 2: Using remote modal
Step 1:
Create another blade file for eg. editEmployee.blade.php and add the above form in it.
<form class="app-form" id="modal-form" action="/profile/employment/edit/{{ $empl->id }}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="{{ $empl->id }}">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 2: Create a controller which would return the above form as HTML.
Tip: use render() function. example
Step 3: load the form into modal window before showing using javascript(jQuery)
considering your modal id is "emp-modal"
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$('#emp-modal').modal('show').find('.modal-body').load(url);
});
One solution would be to send the details you want the same way you send the id to the modal.
and the proper way to send variables to modal is to include this in the button that opens modal:
data-variablename="{{$your-variable}}"
use this jQuery to get the values of your variables to modal. where edit-empl is the id of your modal and data-target of your button
$('#edit-empl').on('show.bs.modal',function (e) {
var variablename= $(e.relatedTarget).data('variablename');
$(e.currentTarget).find('input[id="yourinputID"]').val(variablename);

Laravel Edit Post Content using CKeditor

I am trying to edit existing post using CKeditor. Content isn't loading.
<div class="form-group">
<label>Content</label>
<textarea id="editor1" name="content" class="form-control" rows="3" placeholder="Enter ...">
{{ Request::old('content') }}
</textarea>
</div>
Also im having trouble getting date from the database too.
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" name="published_at" class="form-control" value="{{ $post->published_at->format('M jS Y') }}">
</div>
For the textarea, check for old input, but fall back to defaulting to using the model's content attribute (the post's content)
<textarea ....>{!! Request::old('content', $post->content) !!}</textarea>
For your date issue, I don't know what problem you are having.

Resources