The form portion of my view file is:
<form action="/admin/sliders" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row">
<div class="col-sm-3 form-group">
<h4 class="slide">Slide photo name</h4>
</div>
<div class="col-sm-8 form-group">
<input class="form-control inbox-del" id="slide_name" name="slider_name" placeholder="Add slide name" type="text">
</div>
</div>
<div class="row">
<div class="col-sm-3 form-group">
<h4 class="slide">Add photo for slide</h4>
</div>
<div class="col-sm-6 form-group slide">
<input type="file" id="slider_link" name="slider_link" placeholder="Add a photo" >
</div>
<div class="col-sm-2 form-group">
<button class="btn btn-primary inbox-del pull-right"> Add Slider </button>
</div>
</div>
</form>
And the controller file is as follows:
public function store(Request $request){
$image = $request->file('image');
$filename = time().$image;
$gallery = new Slider;
$gallery->slider_name= $request->slider_name;
$gallery->slider_link = $filename;
$gallery->save();
$request->slider_link->move(public_path('uploaded'),$gallery->slider_name);
return $request->slider_link;
}
And I get the error: FatalErrorException
Call to a member function move() on null. All the text data is inserted into the database. But the image is not placed in the uploaded directory.
You need to use getClientOriginalName function to get filename.
$image = $request->file('image')->getClientOriginalName();
Related
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());
}
Do you know how can we loop data into textarea in thymeleaf?
Please you help to me a sample or any suggestion to do this.
Thanks.
java code
#GetMapping("/import_billing")
public String getImportBilling(Model model) {
model.addAttribute("importBilling", new ImportBilling());
return "importBilling";
}
#PostMapping("/import_billing")
public String postImportBilling(ImportBilling request, Model model, BindingResult result) {
if (result.hasErrors()) {
for (FieldError error : result.getFieldErrors()) {
System.out.println(error.getField() + ": " + error.getDefaultMessage());
}
model.addAttribute("importBilling", request);
return "importBilling";
}
ApiResponse response = cardHolderService.importBillingData(request);
model.addAttribute("response", response.getData());
return "importBilling";
}
Html code
<div class="container-fluid" layout:fragment="content">
<div class="card shadow mb-4">
<div class="collapse show" id="collapseCardExample">
<div class="card-body">
<form class="page-information" th:action="#{/import_billing}" th:object="${importBilling}"
method="post">
<div class="row">
<div class="col-md-4">
<input type="number"
th:value="*{from}"/>
</div>
<div class="col-md-4">
<input type="number"
th:value="*{to}"/>
</div>
<div class="form-group col-md-4">
<button class="btn btn-primary" type="submit">
<i id="_spinner">Execute</i>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
<label>Output console</label>
<div class="card shadow mb-4">
<div class="card-body">
<div class="chart-area">
<div class="form-group" th:each="temp:*{response}" >
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" th:text="${temp}"/>></textarea>
</div>
</div>
</div>
</div>
</div>
In this case, after on click button Execute I would like all response display in textarea
Use th:inline="text" and write your loop using textual syntax. For example, instead of:
<div class="form-group" th:each="temp:*{response}" >
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" th:text="${temp}"/>></textarea>
</div>
Do this:
<div class="form-group">
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" th:inline="text">
[# th:each="temp: *{response}"]
[[${temp}]]
[/]
</textarea>
</div>
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.
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
The bug is very weird, I have been stucking here for hours just can find why my function in controller is fired.
Here is my HTML code:
<div class="col-md-9 clearfix" id="customer-account" ng-controller='ProfileController'>
<div class="box clearfix">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="password_old">oldpassword</label>
<input type="password" class="form-control" id="password_old" ng-model="passwordold">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="password_1">newpassword</label>
<input type="password" class="form-control" id="password_1" ng-model="passwordnew1">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="password_2">retype</label>
<input type="password" class="form-control" id="password_2"ng-model="passwordnew2">
</div>
</div>
<p>{{passwordnew2}}</p>
</div>
<!-- /.row -->
<div class="text-center">
<button type="submit" class="btn btn-primary">
<i class="fa fa-save" ng-click="changePwd(passwordold,passwordnew1,passwordnew2)"></i> save
</button>
</div>
<div class="text-center" >
<p>{{errorMessageChangepwd}}aaaa</p>
</div>
</div>
</div>
After I click the Button, which ng-click attribute as you can see, nothing happen.
Here is my controller code:
controller('ProfileController', ['$scope','UserFactory','SharedDataFactory',
function($scope,UserFactory,SharedDataFactory){
$scope.user = UserFactory.user;
$scope.passwordold;
$scope.errorMessageChangepwd='error';
$scope.showErrMsgChangepwd = false;
$scope.passwordnew1;
$scope.passwordnew2;
$scope.changePwd = function(passwordold,passwordnew1,passwordnew2){
console.log("aaaaaaaaaa");
if (passwordnew1!==passwordnew2){
$scope.showErrMsgChangepwd= true;
$scope.errorMessageChangepwd = 'error';
}else{
UserFactory.changePwd(passwordnew1)
.catch(function(err){
console.log(err.data);
}).then(function(response){
console.log(response);
});
}
};}]);
I called console.log("aaaaaaaaaa"); in the first line of my function, but after I click the button, nothing is shown on console.
And also
<div class="text-center" >
<p>{{errorMessageChangepwd}}aaaa</p>
</div>`
does not show error aaaa as expected but aaa on the browser.
what could be wrong?
Thanks.
You need to add ng-controller="ProfileController" in the top of your form.
Which looks like missing on your code.