I'm facing an issue while calling a Play Scala controller method with a large amount of data. I have file upload method which takes 2 uploaded files and processes them. If the file size is big, the controller method is getting called twice. If the file size is less, it works fine and is getting called only once. I'm not sure what is happening behind the scene.
routes.conf
POST /test/upload controllers.TestController.upload(id:Int, isTest: Boolean)
TestController.scala
def upload(id: Int, isTest: Boolean) = Action(parse.multipartFormData) { implicit request: Request[play.api.mvc.MultipartFormData[play.api.libs.Files.TemporaryFile]] =>
.....
.....
}
upload.scala.html
#helper.form(action = routes.TestController.upload(form.id, isTest), 'enctype -> "multipart/form-data") {
<div class="form-group">
<label for="file1">File 1:</label>
<input class='btn btn-outline btn-primary' name="file1" type="file" data-preview-file-type="text" >
</div>
<div class="form-group">
<label for="file1">File 2:</label>
<input class='btn btn-outline btn-primary' name="file2" type="file" data-preview-file-type="text" >
</div>
<button type="submit" class="btn btn-default btn-primary" id="uploadButton" onclick="$('#myPleaseWait').modal('show');">
Upload</button>
}
Any help on why the method is getting called twice will be helpful.
Related
I have a problem with a form that deletes a doctrine object when posting in Symfony 4.I searched the problem and i only found that i might have to define a service? I am a beginner in symfony so dont blame me please... I am using datatables and the form is in a modal. When i press the delete button in the modal it tries to post to the route but an error comes up:
Service "request" not found: did you mean "request_stack"? Anyway, the container inside "App\Controller\ItemManagement" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.csrf.token_manager", "session" and "twig" services. Try using dependency injection instead.
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteit">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete this item?
<form action="{{ path('delete_item')}}" method="POST" id="theform" >
<input type="hidden" value="" name="itemtodel" id="itemtodel"/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" form="theform" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
In Controller:
/**
* #Route("/delitem", name="delete_item", methods={"POST"});
*/
public function deletetheitem(Request $request)
{
if ($request->isMethod('POST')) {
$itemid = $this->get('request')->request->get('itemtodel');
... deleting item code...
}
}
return $this->redirectToRoute("item_management", [], 301);
}
The route item_management is in the same controller and works fine. Am i doing it with the wrong way? Please if you have something to suggest i will appreciate it, thanks!!!!
try replacing
$this->get('request')->request->get('itemtodel')
by
$request->request->get('itemtodel')
First of all, you do not need to define any services since everything should work out of the box in the Symfony 4 (if you have not customize a default config).
Second, do not create static html forms in the template. Use Symfony Forms instead (https://symfony.com/doc/current/forms.html).
Third, you do not need to use request object if there should not be extra checks for query that get the object to delete. If you have not switched off param converter option (see https://symfony.com/doc/current/best_practices/controllers.html#using-the-paramconverter) of default Symfony 4, delete action could be like that:
/**
* #Route("/delitem/{item}", name="delete_item", methods={"POST"});
*/
public function deleteItemAction(Item $item)
{
$doctrine = $this->getDoctrine();
$em = $doctrine->getManager();
$em->remove($item);
$em->flush();
//process your response
}
I have a working b3 play framework form with file upload. The only issue is I cannot see anywhere in the documentation to remove an already uploaded file. Ideally there would just be a "remove" type button by the file upload that could remove the already uploaded file before form submission.
My current form is below. Any help or suggestions on solving this are more than welcome.
Thank you
#b3.form(routes.SettingsController.submitEditProfile, 'enctype -> "multipart/form-data") {
#CSRF.formField
#b3.text(userForm("displayName"), '_label -> "Display name")
<div class="form-group">
<label class="control-label col-md-2" for="settingsImage">Settings Image</label>
<div class="col-md-6">
#user.settingsImage.fold {
<p>#Messages("has.no.image")</p>
}{ c =>
<p>#Messages("has.image")</p>
#c.artifacts.find(a => a.label == "small" && a.url.nonEmpty).map{ ca =>
<img src="#ca.url" #includes.artifactDimensions(ca) /></a>
}
}
<input type="file" name="settingsImage" />
</div>
</div>
#b3.free('_id -> "idFormGroup") {
<button type="submit" class="btn btn-secondary">#Messages("update")</button>
<a class="btn btn-default" href="#routes.SettingsController.settings()">#Messages("cancel")</a>
}
I have a couple of major gaps in my understanding of vapor/leaf/html. I am working from the "todo" example that is created using the beta branch of vapor.
First, I made my own fluent model (no problems that I know of):
import FluentSQLite
import Vapor
final class affordatmodel: SQLiteModel {
var id: Int?
var propertyCost: String
var targetEquity: String
var interestRate: String
var amortization: String
var sponsor1: String
var sponsor2: String
var rent: String
var rentInflation: String
var propertyTaxes: String
var propertyTaxesInflation: String
var strataFees: String
var strataFeesInflation: String
init(propertyCost: String, targetEquity: String, interestRate: String, amortization: String, sponsor1: String, sponsor2: String, rent: String, rentInflation: String, propertyTaxes: String, propertyTaxesInflation: String, strataFees: String, strataFeesInflation: String) {
self.propertyCost = propertyCost
self.targetEquity = targetEquity
self.interestRate = interestRate
self.amortization = amortization
self.sponsor1 = sponsor1
self.sponsor2 = sponsor2
self.rent = rent
self.rentInflation = rentInflation
self.propertyTaxes = propertyTaxes
self.propertyTaxesInflation = propertyTaxesInflation
self.strataFees = strataFees
self.strataFeesInflation = strataFeesInflation
}
}
/// Allows to be used as a dynamic migration.
extension affordatmodel: Migration { }
/// Allows to be encoded to and decoded from HTTP messages.
extension affordatmodel: Content { }
/// Allows to be used as a dynamic parameter in route definitions.
extension affordatmodel: Parameter { }
Then I make an instance and send it to a leaf template:
let defaultData = affordatmodel(propertyCost: "700000", targetEquity: "300000", interestRate: "1", amortization: "20", sponsor1: "500000", sponsor2: "200000", rent: "1200", rentInflation: "1", propertyTaxes: "8000", propertyTaxesInflation: "1", strataFees: "0", strataFeesInflation: "0")
return leaf.render("welcome", ["affordat": defaultData])
And my Leaf template successfully populates the html with the default data (body shown here):
<body class="container">
<h1>Payment and Principal Calculations</h1>
<form action="/affordat" method="POST">
<div class="form-group">
<label for="propertyCost">Property Cost</label>
<input type="number" class="form-control" name="propertyCost" placeholder="#(affordat.propertyCost)">
</div>
<div class="form-group">
<label for="targetEquity">Target Equity</label>
<input type="number" class="form-control" name="targetEquity" placeholder="#(affordat.targetEquity)">
</div>
<div class="form-group">
<label for="interestRate">Interest Rate</label>
<input type="number" class="form-control" name="interestRate" placeholder="#(affordat.interestRate)">
</div>
<div class="form-group">
<label for="amortization">Amortization (years)</label>
<input type="number" class="form-control" name="amortization" placeholder="#(affordat.amortization)">
</div>
<div class="form-group">
<label for="sponsor1">Sponsor 1 Funds</label>
<input type="number" class="form-control" name="sponsor1" placeholder="#(affordat.sponsor1)">
</div>
<div class="form-group">
<label for="sponsor2">Sponsor 2 Funds</label>
<input type="number" class="form-control" name="sponsor2" placeholder="#(affordat.sponsor2)">
</div>
<div class="form-group">
<label for="rent">Rent</label>
<input type="number" class="form-control" name="rent" placeholder="#(affordat.rent)">
</div>
<div class="form-group">
<label for="rentInflation">Rent Inflation (will be used exactly)</label>
<input type="number" class="form-control" name="rentInflation" placeholder="#(affordat.rentInflation)">
</div>
<div class="form-group">
<label for="propertyTaxes">Property Taxes (first year est.)</label>
<input type="number" class="form-control" name="propertyTaxes" placeholder="#(affordat.propertyTaxes)">
</div>
<div class="form-group">
<label for="propertyTaxesInflation">Property Taxes Inflation (est.)</label>
<input type="number" class="form-control" name="propertyTaxesInflation" placeholder="#(affordat.propertyTaxesInflation)">
</div>
<div class="form-group">
<label for="strataFees">Strata Fees (first year est.)</label>
<input type="number" class="form-control" name="strataFees" placeholder="#(affordat.strataFees)">
</div>
<div class="form-group">
<label for="strataFeesInflation">Strata Fees Inflation (est.)</label>
<input type="number" class="form-control" name="strataFeesInflation" placeholder="#(affordat.strataFeesInflation)">
</div>
<input type="hidden" name="_csrf" value="{{csrfToken}}">
<button type="submit" class="btn btn-primary">Refresh Calculations</button>
</form>
</body>
Great, so I know how to get fluent data to HTML. My problem is I don't know how to get it back. When the "Post" occurs, the data does not seem to get passed to the controller. My route is:
router.post("affordat", use: affordatController.create)
And the relevant part of my controller looks like this:
import Vapor
final class AffordatController {
func create(_ req: Request) throws -> Future<affordatmodel> {
return try req.content.decode(affordatmodel.self).flatMap(to: affordatmodel.self) { affordatmodel1 in
return affordatmodel1.save(on: req)
}
}
}
Which shows me one of my models, with an ID #, but no data. And I kind of understand why because I didn't really seem to send the post data to the controller. How I am supposed to send the POST data to the controller? Is the problem in my leaf template, my route, or my controller?
It looks like it should work. You can inspect the data being sent to your server in the network inspector in your browser. Make sure you preserve logs and you'll be able to see the POST request and the data sent to the server.
If you breakpoint at each point in the request you can see what the data is.
As an aside, it looks like you're submitting an empty form, so it's just filling everything in as blank strings. Do you mean to use value instead of placeholder in the form inputs? Value will pre-populate the data for the user, placeholder will show the value to the user as a suggestion but won't send the data in the form submission.
I have this template:
<Template name="nuevoEjercicio">
<div class="container-fluid">
<div class="form-group">
<input type="text" class="form-control input-lg" name="ejercicio" placeholder="Ejercicio?"/>
<input type="number" class="form-control" name="repeticiones" placeholder="Repeticiones?" />
<input type="number" class="form-control" name="peso" placeholder="Peso?" />
<button type="submit" class="btn btn-success" >
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
</div>
</Template>
that I use to capture and save to the database.
Then on my .js file I am trying to get the data and save it:
Template.nuevoEjercicio.events({
'click .btn btn-success': function (event) {
var ejercicio = event.target.ejercicio.value;
var repeticiones = event.target.repeticiones.value;
var peso = event.target.peso.value;
ListaRutina.insert({
rutina:"1",
ejercicio:ejercicio,
repeticiones:repeticiones,
peso:peso,
});
// Clear form
event.target.ejercicio.value = "";
event.target.repeticiones.value = "";
event.target.peso.value = "";
// Prevent default form submit
return false;
}
});
}
as I understand, when I click on any object that has the btn btn-success style....but is not the case. For some obscure reason -for me- is not working.
Can you check it and give me some advice?
Thanks!
First of all, there's an error in you selector. It's 'click .btn.btn-success', not 'click .btn btn-success'.
Also you can't do that event.target.ejercicio.value thing. event.target is the element that was clicked. You'll have to do something like this:
'click .btn.btn-success': function (event, template) {
var ejercicio = template.$('[name=ejercicio]').val()
...
OK
What after wasting hours and hours the solution is:
1- on the html file give your input an id:
<input type="number" class="form-control" **id="peso"** placeholder="Peso?" />
<button type="submit" class="btn .btn-success" id="**guardar**" />
so now you want to save data on the input when the button is clicked:
2- You link the button with the funcion via the id
Template.TEMPLATENAMEONHTMLFILE.events({
'click **#guardar**': function (event, template) {
var ejercicio = template.$("**#peso**").val();
and get the value linking using the input id.
I've added this to my Boot.scala
LiftRules.viewDispatch.append({
case List("admin", "categories") => Right(Admin)
})
Here is my Admin.scala in (admin/view package)
object Admin extends LiftView{
def dispatch = {
case "add" => editCategory
}
def editCategory(): NodeSeq = {
<lift:embed what="/admin/add_category"></lift:embed>
}
}
Here is my add_category template -
<lift:surround with="admin" at="content">
<div class="container-fluid" id="main_container">
<lift:Admin:addCategory form="POST" class="form-horizontal">
<fieldset>
<legend>Add Category</legend>
<div class="control-group">
<label class="control-label" for="cat_name">Name</label>
<div class="controls">
<cat:name></cat:name>
</div>
</div>
<div class="control-group">
<label class="control-label" for="cat_desc">Description</label>
<div class="controls">
<cat:desc></cat:desc>
<cat:submit></cat:submit>
</div>
</div>
</fieldset>
</lift:Admin:addCategory>
</div>
</lift:surround>
I'm trying to bind this with a snippet -Admin.scala in (admin/snippet package) with addCategory method.
object name extends RequestVar("")
object desc extends RequestVar("")
def addCategory(in: NodeSeq): NodeSeq = {
def doAdd() {
//todo: Save Category
}
bind("cat", in,
"name" -> SHtml.text(name.is, (n: String) => name(n), ("id", "cat_name"), ("class", "input-large")),
"desc" -> SHtml.textarea(desc.is, (d: String) => desc(d), ("id", "cat_desc"), ("class", "input-large")),
"submit" -> SHtml.submit("Save", doAdd, ("class", "btn btn-info"))
)
}
I'm getting this error -
Error processing snippet: admin:addcategory
Reason: Method Not Found
XML causing this error:
<lift:admin:addcategory class="form-horizontal" form="POST">
<fieldset>
<legend>Add Category</legend>
<div class="control-group">
<label for="cat_name" class="control-label">Name</label>
<div class="controls">
<cat:name></cat:name>
</div>
</div>
<div class="control-group">
<label for="cat_desc" class="control-label">Description</label>
<div class="controls">
<cat:desc></cat:desc>
<cat:submit></cat:submit>
</div>
</div>
</fieldset>
</lift:admin:addcategory>
And in the logs -
[net.liftweb.http.LiftRules] - Snippet Failure: SnippetFailure(/admin/categories/add -> ParsePath(List(admin, categories, add),,true,false),Full(admin:addcategory),Method Not Found)
Need help with this. I'm unable to figure out why lift is not able to find the method.
Have you added your admin package into LiftRules? Something like this should allow Lift to search admin.snippets for resolution:
LiftRules.addToPackages("admin")
Also, I believe you need to be calling the snippet as Admin.addCategory instead of with a :.
Edit:
I believe that the Lift HTML5 parser was case sensitive and had trouble with camel case method names. You may want to try renaming your method to all lowercase, or try calling your snippet as (instead of with the <lift: style):
<div class="lift:admin.addCategory"> ... </div>
or
<div data-lift="admin.addCategory"> ... </div>