laravel - Display validation error in the proper tab - html

I am using the bootstrap tab to navigate between three different tabs for a user. First tab(primary tab) is to display user details and second tab is to edit the information and the third tab allows user to change the password.
Whenever I made a validation error, say in the edit tab, then it will return to the first tab and I'll have to select the edit tab to see the error. Similarly, any validation error from the third tab will also return to my first tab.
show.blade.php:
div class="container">
<div class="row my-2">
<div class="col-lg-8 order-lg-2">
<ul class="nav nav-tabs">
<li class="nav-item">
Profile
</li>
#can('update',$user)
<li class="nav-item">
Edit
</li>
#endcan
<li class="nav-item">
Change Password
</li>
</ul>
<div class="tab-content py-4">
<div class="tab-pane active" id="profile">
<h5 class="mb-3">User Profile</h5>
<div class="row">
<div class="col-md-6">
<h6>Name</h6>
<p>
{{$user->name}}
</p>
<h6>Email</h6>
<p>
{{$user->email}}
</p>
</div>
</div>
<!--/row-->
</div>
<div class="tab-pane " id="edit">
<form method="POST" action="{{route('profile.update',[$user])}}">
#csrf
#method('PATCH')
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Name</label>
<div class="col-lg-9">
<input class="form-control" type="text" id="name" name="name" onkeyup="checkInput()" value="{{$user->name}}" >
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Email</label>
<div class="col-lg-9">
<input class="form-control" type="email" name="email" value="{{$user->email}}" disabled>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label"></label>
<div class="col-lg-9">
<input type="reset" class="btn btn-secondary" value="Cancel">
<button type="submit" id="save-btn" class="btn btn-primary" disabled>Save Changes</button>
</div>
</div>
#if(count($errors))
<ul class="alert alert-danger">
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
#endif
</form>
</div>
<div class="card tab-pane " id="change-password">
<div class="card-header">Change password</div>
<div class="card-body">
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<form class="form-horizontal" method="POST" action="{{route('changePassword')}}">
#csrf
<div class="form-group{{ $errors->has('current-password') ? ' has-error' : '' }}">
<label for="new-password" class="col-md-4 control-label">Current Password</label>
<div class="col-md-6">
<input id="current-password" type="password" class="form-control" name="current-password" required>
#if ($errors->has('current-password'))
<span class="help-block">
<strong>{{ $errors->first('current-password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('new-password') ? ' has-error' : '' }}">
<label for="new-password" class="col-md-4 control-label">New Password</label>
<div class="col-md-6">
<input id="new-password" type="password" class="form-control" name="new-password" required>
#if ($errors->has('new-password'))
<span class="help-block">
<strong>{{ $errors->first('new-password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="new-password-confirm" class="col-md-4 control-label">Confirm New Password</label>
<div class="col-md-6">
<input id="new-password-confirm" type="password" class="form-control" name="new-password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Change Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
ProfileController:
public function update(UpdateProfileRequest $request, User $user){
$this->authorize('update',$user);
$validated = $request->validated();
$user->update([
'name' => $request->input('name')
]);
return redirect($user->path())->with('message',"Profile has been updated");;
}
ChangePasswordController:
public function changePassword(Request $request){
if (!(Hash::check($request->input('current-password'), Auth::user()->password))) {
// The passwords matches the password of the user in the database
return redirect()->back()->with("error","Your current password does not matches with the password you provided. Please try again.");
}
if(strcmp($request->get('current-password'), $request->get('new-password')) == 0){
//Current password and new password are same
return redirect()->back()->with("error","New Password cannot be same as your current password. Please choose a different password.");
}
$validatedData = $request->validate([
'current-password' => 'required',
'new-password' => 'required|string|min:8|confirmed',
]);
//Change Password
$user = Auth::user();
$user->password = bcrypt($request->input('new-password'));
$user->save();
return redirect()->back()->with("success","Password changed successfully !");
}
I tried adding :
...
<div class="tab-pane {{ count($errors) ? '' : 'active' }}" id="profile">
...
<div class="tab-pane {{ count($errors) ? 'active' : '' }}" id="edit">
...
but it only works if there are two tabs. How can I tackle this?

You can activate tabs with specific field errors checking. This approach works because you also separated forms. There will be no errors in separated forms at the same time.
...
<div class="tab-pane {{ ($errors->has('name') || $errors->has('email')) ? 'active' : '' }}" id="profile">
...
<div class="tab-pane {{ ($errors->has('current-password') || $errors->has('new-password') || $errors->has('new-password_confirmation') ) ? 'active' : '' }}" id="edit">
...

Use the hidden field in your form like below.
<input type="hidden" name="tab" value="settings">
Then in your controller redirect like below.
$tab = $request->get('tab');
if($validator->fails()) {
return back()->withInput(['tab'=>$tab])->withError($validator->messages()->first());
}

Related

How to allow same day booking in t-datepicker

Hi everyone!
I'm trying to allow same day booking for my calendar using the
t-datepicker. I'm trying to use different methods from the
documentation to allow same day booking. For example to allow users to
book from 6th of june till 6th of June. However it's not allowing that
and always adds an extra day when booking. The code is below.
<div class="modal fade" id="myModal">
<div class=row>
<div class="col-sm-10 offset-sm-1">
<div class="modal-dialog modal-lg">
<form action="" method="POST">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Quick Booking</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
{{ csrf_field() }}
#isset($error_msg)
#foreach ($error_msg as $error)
<li class="text-danger">{{ $error }}</li>
#endforeach
#endisset
<div class="form-group row">
<label for="placeid" class="col-md-4 col-form-label text-md-right">{{ __('Place ID') }}: </label>
<div class="col-md-6">
<input type="text" class="form-control" name="place_id" required>
</div>
</div>
<div class="form-group row">
<label for="placeid" class="col-md-4 col-form-label text-md-right">{{ __('Arrival day') }}: </label>
<div class="col-md-6">
<div class="container11">
<div class="t-datepicker">
<span id="tdatepik" class="t-check-in"></span>
<span id="searchdate_numberofdays" class="t-check-out"></span>
#isset($maparray['err_msg'])
<span id="errormsg_txt" style="color:red;"> {{ __('You can book maximum') }} {{ $maparray["set_admin"]->max_no_days }} {{ __('days') }}.</span><br>
#endisset
<span id="errormsg_txt" style="color:red;display:none;"> {{ __('Arrival day is not selected') }}. </span><br>
</div>
</div>
</div>
</div>
<div class="form-group row">
<label for="fullname" class="col-md-4 col-form-label text-md-right">{{ __('Full Name') }}: </label>
<div class="col-md-6">
<input type="text" class="form-control" name="user_fullname" required>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Email address') }}: </label>
<div class="col-md-6">
<input type="email" class="form-control" name="user_email" required>
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone') }}: </label>
<div class="col-md-6">
<input type="tel" class="form-control" name="user_phone" required>
</div>
</div>
<div class="form-group row">
<label for="geust" class="col-md-4 col-form-label text-md-right">{{ __('Number of adults') }}[1-4]: </label>
<div class="col-md-6">
<select class="form-control booking_inp_textbox_style" id="numberofguest" name="numberofguest" onchange="addFieldsadmin(this.value);">
<option value="0" active>1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="babies" class="col-md-4 col-form-label text-md-right">{{ __('Number of babies') }}[1-4]: </label>
<div class="col-md-6">
<select class="form-control booking_inp_textbox_style" id="numberofbabies" name="numberofbabies" onchange="addFieldsadmin2(this.value);">
<option value="0" active>0</option>
<option id="hidden_op_style1" value="1">1</option>
<option id="hidden_op_style2" value="2">2</option>
<option id="hidden_op_style3" value="3">3</option>
<option id="hidden_op_style4" value="4">4</option>
</select>
</div>
</div>
<div class="containerinput">
{{-- dynamic input surname using javascript --}}
</div>
<div class="containerinput2">
{{-- dynamic input surname using javascript --}}
</div>
<div class="form-group row">
<label for="promo" class="col-md-4 col-form-label text-md-right">{{ __('Promo Code') }}: </label>
<div class="col-md-6">
<input type="text" class="form-control" name="user_promo">
</div>
</div>
<div class="form-group row">
<label for="book" class="col-md-4 col-form-label text-md-right">{{ __('Booked By') }}: </label>
<div class="col-md-6">
<label>{{ Auth::user()->name }}</label>
</div>
</div>
#if (Auth::user()->role == 'admin')
<div class="form-group row">
<label for="Ammount" class="col-md-4 col-form-label text-md-right">{{ __('Payment Type') }}: </label>
<div class="col-md-6">
<input type="radio" name="user_payment_type" value="Entrance">
<label for="Entrance" class="booking_payment_type_style">{{ __('Entrance (Cash)') }}</label>
<input type="radio" name="user_payment_type" value="Admin" checked>
<label for="Entrance" class="booking_payment_type_style">{{ __('Admin (Free)') }}</label><br>
</div>
</div>
#else
<div class="form-group row">
<label for="promo" class="col-md-4 col-form-label text-md-right">{{ __('Payment Type') }}: </label>
<div class="col-md-6">
<input type="radio" name="user_payment_type" value="Entrance" checked>
<label for="Entrance" class="booking_payment_type_style">{{ __('Entrance (Cash)') }}</label>
</div>
</div>
#endif
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">{{ __('Save') }}</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
var today = new Date();
tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 60);
var date = tomorrow.getFullYear()+'-'+(tomorrow.getMonth()+1)+'-'+tomorrow.getDate();
$(document).ready(function(){
// Call global the function
$('.t-datepicker').tDatePicker({
// options here
titleDateRange :'{{ __('day') }}',
titleDateRanges:'{{ __('days') }}',
titleToday :'{{ __('Today') }}',
limitDateRanges :'{{ $set_admin->max_no_days+300 }}',
titleCheckIn :'{{ __('Check In') }}',
titleCheckOut :'{{ __('Check Out') }}',
numCalendar : 1,
//endDate: date,
nextDayHighlighted :false,
toDayHighlighted :true,
toDayShowTitle :true,
autoClose: true,
dateRangesHover: false,
});
});
// $('.t-datepicker').tDatePicker('setEndDate','2020-07-28');
</script>
<script type="text/javascript">
#isset($error_msg)
#if (count($error_msg) > 0)
$('#myModal').modal('show');
#endif
#endisset
</script>

Error in Sending form data to Django views through AJAX

I have a HTML form, which takes in input from the user. On clicking the submit button, form data needs to be sent to the views.py in a JSON format, through AJAX. I tried out something, but I am getting an error the JSON object must be str, not 'NoneType' . The JSON data is being loaded in the views.py through:
form = json.loads(request.POST.get('form'))
The data is returned in form of a dictionary called 'searchdata'.
The form:
<form method="POST" id="inputForm">
<div class="container" style="margin-bottom: 50px;width:100%">
<div class="container-fluid mt-3">
<div class="card" id="statform">
<div class="card-header">
<div class="card-title">
<div style="font-size: 25px">Filter</div>
</div>
</div>
<br>
<div class="card-body">
<div class="row">
<div class="col-xs-6">
<div class="col-xs-6">
<label name="start_date" class="control-label" style="width:35%">Start date</label>
<input type="date" id="start_date" style="color:black;width:100px" ></input>
</div>
</div>
<div class="col-xs-6">
<label name="end_date" class="control-label" style="width:35%">End Date(Default: Current Date)</label>
<input style="color:black;width:100px" id="end_date" type="date"></input>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6">
<label name="fruit_name" class="control-label" style="width:35%; padding-left:15px">Select a Fruit</label>
<select class="js-example-placeholder-single1 js-example-basic-multiple form-control" id="fruit_name" placeholder="Select" style="width:210px" multiple="multiple">
</select>
</div>
<div class="col-xs-6">
<label name="vendor_test" class="control-label" style="width:35%">Select Vendor_TEST</label>
<select class="js-example-placeholder-single2 js-example-basic-multiple form-control" style="width:210px" id="vendor_test" multiple="multiple">
</select>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<label name="release_version" class="control-label" style="width:35%; padding-left:15px">Select Release version</label>
<select class="js-example-placeholder-single3 js-example-basic-multiple form-control" style="width:210px" id="release_version" multiple="multiple">
</select>
</div>
<div class="col-xs-6">
<label class="control-label" style="width:35%">Error Message</label>
<input type="text" placeholder="Type message here" style="width:210px">
</div>
</div>
<div class="row">
<div class="col-xs-6">
<label class="control-label" style="width:35%; padding-left:15px">Error Message List</label>
<select class="js-example-placeholder-single4 js-example-basic-multiple form-control" style="width:210px" id = "error_message" multiple="multiple">
</select>
</div>
</div>
<div class="text-center">
<button class="btn btn-md btn-default" type="submit" name="search" value="search" onclick="loadResults()">
<i class="fa fa-arrow-circle-right"></i> Submit
</button>
<button class="btn btn-md btn-default" type="reset" name="searchreset" value="reset">
<i class="fa fa-arrow-circle-right"></i> Reset
</button>
</div>
</div>
</div> <!--form-->
</div> <!-- row -->
</div> <!--container main-->
</form>
The AJAX call I have written:
$.ajax({
url : window.location.href,
type : 'POST',
cache : false,
data:{form:JSON.stringify(formdata)},
success:function(res){
if(typeof res.searchdata != 'undefined'){
self.$dispatch('searchdataevent',res.searchdata);
}
if(typeof res.message != 'undefined'){
self.message = res.message;
}
self.loading = false;
},
error : function(err){
self.message = 'Error communicating with server';
self.loading = false;
}
});
Where am I going wrong? Any help is appreciated.

error when updating table (crud) in laravel

When I get ready to edit the table I get the following error:
"The POST method is not supported for this route. Supported methods:
GET, HEAD."
<?php
Route::get('/crear',[
'uses'=>'CarController#mostrar',
'as'=>'cars.create'
]
);
Route::post('/crear',[
'uses'=>'CarController#crear',
'as'=>'cars.crear'
]);
Route::get('/', 'CarController#casa' );
Route::post('cars/{id?}/editar', 'CarController#edit')->name('editarcar');
Route::post('cars/{id?}/editar', 'CarController#update');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('cars', 'CarController');
In View
#extends('layouts.app')
#section('title', 'Contact')
#section('content')
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" method="post">
#foreach ($errors->all() as $error)
<div class="alert alert-danger">{{ $error }}</div>
#endforeach
#if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
{!! csrf_field() !!}
<fieldset>
<legend>Editar </legend>
<div class="form-group">
<label for="patente" class="col-lg-label">patente</label>
<div class="col-lg-10">
<input type="text" name="patente"size="6" maxlength="6" class="form-control"required>
</div>
</div>
<div class="form-group">
<label for="marca" class="col-lg-label">marca</label>
<div class="col-lg-10">
<input type="text" name="marca" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="modelo" class="col-lg-label">modelo</label>
<div class="col-lg-10">
<input type="text" name="modelo" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="color" class="col-lg-label">color</label>
<div class="col-lg-10">
<input type="text" name="color" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancelar</button>
<button type="submit" class="ntm btn-primary">Actualizar</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
#endsection
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
namespace App\Http\Controllers;
use App\Car;
use Illuminate\Http\Request;
public function edit($id)
{
$car = car::whereid($id)->firstOrFail();
return view('edit', compact('cars'));
}
public function update(Request $request, $id)
{
$car = car::whereid($id)->firstOrFail();
$car->patente = $request->post('patente');
$car->marca = $request->post('marca');
$car->modelo = $request->post('modelo');
$car->color = $request->post('color');
$car->save();
return redirect(action('CarsController#edit', $car->id))->with('status', 'El car ' . $id . ' ha sido actualizado');
}
Update your controller > edit action with correct variable compact.
public function edit($id)
{
$car = car::whereid($id)->firstOrFail();
return view('edit', compact('car'));
}
Add a form action like {{ route("cars.update", ['car' => $car->id]) }} so your view looks like:
#extends('layouts.app')
#section('title', 'Contact')
#section('content')
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" action="{{ route("cars.update") }}" method="post">
#foreach ($errors->all() as $error)
<div class="alert alert-danger">{{ $error }}</div>
#endforeach
#if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
{!! csrf_field() !!}
<fieldset>
<legend>Editar </legend>
<div class="form-group">
<label for="patente" class="col-lg-label">patente</label>
<div class="col-lg-10">
<input type="text" name="patente"size="6" maxlength="6" class="form-control"required>
</div>
</div>
<div class="form-group">
<label for="marca" class="col-lg-label">marca</label>
<div class="col-lg-10">
<input type="text" name="marca" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="modelo" class="col-lg-label">modelo</label>
<div class="col-lg-10">
<input type="text" name="modelo" class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="color" class="col-lg-label">color</label>
<div class="col-lg-10">
<input type="text" name="color" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancelar</button>
<button type="submit" class="ntm btn-primary">Actualizar</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
#endsection

Laravel 5.4 UnexpectedValueException in Response.php line 444 in form submition

I am making multiauth. When I submit admin email and password from admin login view I got bellow error
"UnexpectedValueException in Response.php line 444:
The Response content must be a string or object implementing __toString(), "boolean" given."
here is my form
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Admin Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('admin.login.submit') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
And here is route
Route::prefix('admin')->group(function (){
Route::post('/login', 'Auth\AdminLoginController#login')->name('admin.login.submit');
});
And the controller is here
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AdminLoginController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function showLoginForm()
{
return view('admin.auth.login');
}
public function login(Request $request)
{
return true;
}
}
What wrong here?
And How can I solve it?
Thanks in advance.
I found that problem. that was in my controller. Exactly in login function. I returened true. Here is the problem. When I return 1 then it worked.

How to clear form fields after a submit in laravel

<div class="form-group">
<label>Class</label>
<input type="text" class="form-control text-capitalize" name="user_class" value="{{Input::old('user_class')}}" placeholder="Class">
<div class="col-sm-12">
#foreach($errors->get('user_class') as $user_class)
<div class="alert alert-danger error">{{$user_class}}</div>
#endforeach
</div>
</div>