How to loop data into textarea thymeleaf? - html

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>

Related

Angular input form values only update after focus

I have an Angular app with this form named 'form' and this page also has another form in the same html called 'form' as well. The second form loads the row first values coming from the ngFor but the rest only load after I click inside the unloaded input. This might be because of the same name?
<div class="all">
<div class="card card-rounded shadow-sm">
<form [formGroup]="form">
<div class="p-3">
<div class="form-row">
<div class="form-group col-md-12">
<label>Tema</label>
<input [ngClass]="cssValidator(f.theme)" class="form-control" formControlName="theme">
<div *ngIf="f.theme.errors?.required && f.theme.touched" class="invalid-feedback">
Obrigatório
</div>
<div *ngIf="f.theme.errors?.maxlength" class="invalid-feedback">
Máximo 50 letras
</div>
</div>
</div>
</div>
</form>
</div>
<div class="card rounded shadow-sm mt-4" *ngIf="editMode">
<div class="p-3">
<div class="d-flex border-bottom">
</div>
<div class="form-row p-1">
<div [formGroup]="form" class="col">
<div formArrayName="details" *ngFor="let detail of details.controls; let i = index">
<fieldset [formGroupName]="i" class="form-group">
<div class="row">
<div class="form-group col-md-4">
<label>Preço</label>
<input [ngClass]="cssValidator(details.get(i+'.price'))" class="form-control" formControlName="price">
</div>
<div class="form-group col-md-4">
<label>Descrição</label>
<input [ngClass]="cssValidator(details.get(i+'.description'))"
class="form-control" formControlName="description" placeholder="Descrição...">
</div>
</div>
</fieldset>
<hr>
</div>
</div>
</div>
</div>
</div>
<br>
</div>
The ts:
get f(): any {
return this.form.controls;
}
get details(): FormArray {
return (this.form.get('details') as FormArray);
}
loadDetails() {
this.detailService.getDetailsByEventId(this.eventId).subscribe(
(detailsResult: Detail[]) => {
detailsResult.forEach(detail => {
this.details.push(this.createDetails(detail));
});
},
(error: any) => {
this.toastr.error('Erro ao tentar carregar detalhes', 'Erro!');
console.log(error);
}
).add(() => this.spinner.hide());
}

Update input type time based on updated FormControl using PatchValue or SetValue in Angular

I have a form group with 3 inputs. The first input is a time input and the other 2 are custom form components. I setup a form.patchValue which updates the form values as seen below, however it does not update the UI, the time input is still empty.
How can I use patchValue to update the input ?
this.scheduleAutoReportForm = new FormGroup(
{
reportAt: new FormControl( '12:00' , Validators.required),
days: new FormControl([],Validators.required),
email: new FormControl([],[Validators.required])
}
);
this.scheduleAutoReportForm.patchValue({
reportAt: '24:00',
days:["2","4"],
email: ['placeholder#gmail.com']
});
this.ref.detectChanges();
Update
I experimented and it turns out that the textbox gets updated but not the time.
<div class="modal-header">
<h4 class="h-primary modal-title">Schedule Auto Report</h4>
<mh1-icon-button [icon]="faTimes" [iconClasses]="['btn', 'btn-sm', 'mx-0']" iconSize="lg" (click)="closeModal()">
</mh1-icon-button>
</div>
<div class="modal-body card">
<!-- This is where the form starts -->
<form [formGroup]='scheduleAutoReportForm' (ngSubmit)='onSubmit()'>
<div class="row">
<div class="col-3">
<label for="reportAt" ><h5>Send Report At:</h5></label>
<input type="time" name='reportAt' formControlName='reportAt' class="form-control" required>
</div>
</div>
<div class="row">
<div class="col-3">
<label for="test" >test</label>
<input type="text" name='test' formControlName='test' class="form-control" >
</div>
</div>
<div class="row mt-2">
<div class="col-12">
<sentryx-day-selector formControlName="days"></sentryx-day-selector>
</div>
</div>
<div class="row mt-2">
<div class='col-12'>
<sentryx-email-chips formControlName="email"></sentryx-email-chips>
</div>
</div>
<div class="row mt-5">
<div class="col-12">
<button type="submit" class="btn btn-md btn-secondary"
[disabled]="!scheduleAutoReportForm.valid">Submit</button>
<button type="cancel" class="btn btn-md btn-secondary">Cancel</button>
</div>
</div>
</form>
</div>
<div class="modal-footer card">
{{scheduleAutoReportForm.value | json}}
{{schedule | json}}
</div>
Did you try calling updateValueAndValidity() after patching the value?
See
What is updateValueAndValidity

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.

Image not uploading to MySQL database in Laravel

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();

ng-click not fire function in controller

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.