Insert data in H2 using HTML form - forms

I'm using Play! Framework 2.4. I can make a table and insert data via the evolution .sql scripts but how do I setup my Appication.scala, routes etc to make a form insert data?
PS I'm quite new to Play

The best way play fromework recommends is using helper and template system.
https://www.playframework.com/documentation/2.4.x/ScalaForms
But if you want to try with plain html and handler request in the controller by your own so you can try this:
Application.scala
def save = Action { implicit request =>
//here handle params from html form,
//this will return Option[Map[String,ArrayBuffer]]
val optionMap = request.body.asFormUrlEncoded
println(optionMap)
//saving things here
Ok("saved")
}
in plain html form
<form action="/savedata" method="post" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded">
<input type="text" name="name" />
<input type="text" name="lastName" />
<input type="submit" />
</form>
conf/routes
POST /savedata controllers.Application.save
hope this helps.
regards

Related

How can I stop default submission of form using Vanilla JavaScript

I would like to stop default submission using Vanilla JavaScript. I created a sample of little form. But it gets refreshed when I submit the form even though I call the preventDefault() method. When I use input type="button" it works. But not works with input type="submit".
What will be the reason? Can anyone explain me what is the right method and what's wrong with the code?
My code is as follows:
let validation = () => {
let form = document.querySelector(".form");
form.addEventListener("submit", (event) => {
event.preventDefault();
});
}
<form action="" class="form" method="post" onsubmit="return validation()">
<input type="text">
<input type="submit" value="submit">
</form>
Optional Help: Can anyone give me a proper method or any good references on creating forms. Because when I search, I got lots of tutorials where all are says different methods and I really struggle to find out which method is the standard one.
Try the following changes to your code:
HTML
<form action="" class="form" method="post" onsubmit="validation(event)">
<input type="text">
<input type="submit" value="submit">
</form>
Try removing the return keyword and add event parameter.
JavaScript
const validation = event => {
event.preventDefault();
}
Using the preventDefault() method of event, the form is hopefully not submitted! Hopefully it works for you.

Play framework: empty body in post request

Helo! I have a following code:
def foo = Action { request =>
Ok(request.body.asText.getOrElse("no body"))
}
In frontend have a form like this:
<form action="#controllers.routes.Application.foo()" method="POST">
<input name="name" type="text">
<input name="surname" type="text">
<button type="submit">
</form>
If I fill the form and click submit, they gives me result: no body.
But if I add brakepoint in debugger to Ok(..), they shows me, that body is not emty.
AnyContentAsFormUrlEncoded(Map(name -> ArrayBuffer(123), surname -> ArrayBuffer(123)))
Why, that doesn't give me body as text, or as anything else, and how I can get them?
Given your form and your debugging output, you should be using asFormUrlEncoded.
You'll need the following as well:
<form ... enctype="text/plain">
to specify that the payload is plain text. Then the .asText will work.
Your content type probably is something other than text/plain. The BodyParser will only return a result, when the content type is the expected.
edit: Instead of asText try asRaw.

MVC how can i post a form without using a model

I would like to know how i could sumbit a form in mvc 4 without using a model binding to the view.
can some one please demontrate how? and how can i use that form in the controller?
It is very easy to use a form without any sort of Model binding.
Simple set up your for using either #using(Html.BeginForm()){ ... } or plain html.
<form id="myForm" action="/ControllerName/ActionName" method="Post">
<input type="text" name="foo" value="FOOOO" />
<input type="text" name="bar" value="Baaaar" />
<button type="submit">Submit</button>
</form>
When you post this to the ActionResult, your action result just needs to be set up to accept a string value named foo and one named bar
[HttpPost]
public ActionResult ActionName(string foo, string bar){
// your code here
return View();
}

Getting problems in saving data in mongodb with the enctype set

Hi I have a form like the one given below
<form action='/posts', method ='post',enctype='form-data'>
<div>
<span>FileName :</span>
<input type="text" name="filename" id="filename" />
</div>
<div>
<span>File :</span>
<input type="file" name="file" id="file" />
</div>
<div>
<span>Submit :</span>
<input type="submit" name="Submit" value ='Submit' id="editSubmit" />
</div>
</form>
and the corresponding method to save in mongodb is:
app.post('/posts', function(req, res) {
RegProvider.save({
filename: req.param('filename'),
file: req.param('file')
}, function(error, docs) {
res.redirect('/');
});
});
when i remove enctype in ejs file everything is working fine and the data is getting stored in mongodb.However when i put enctype in ejs file,nothing gets stored in monogdb.Infact when try to retrieve the data i get undefined values for filename and file from fiels.Is it possible to have enctype and store data in database.If yes how can this be achieved.
You probably mean multipart/form-data encoding?
As far as I know you will have to parse the request as multipart message. There is Node.js plugin for that: Multipart-js.

ASP .Net MVC 2 custom Helper Grid not posting Model to controller action

I've created a custom helper that renders a grid and receives the strongly typed view's model as a parameter.
Basically my view looks like this:
<% using (Html.BeginForm("UpdateValues", "Home", FormMethod.Post)) { %>
<%= Html.MyGrid(Model)%>
<input type="submit" value="Update Values" />
<%} %>
But when I click on the submit button, all the values on the model are null.
This is what the controller looks like:
[HttpPost]
public string UpdateValues(AssignmentResultsVm assignmentResults)
{
//..... do something
}
How can I make this work?
Thanks in advance.
You need to make sure that the items are arranged in your grid so that the default model binder can map the data map to your view model class.
This is done by index the name property of the data your are binding like this:
<form method="post" action="/Home/Create">
<input type="text" name="[0].Title" value="Curious George" />
<input type="text" name="[0].Author" value="H.A. Rey" />
<input type="text" name="[1].Title" value="Code Complete" />
<input type="text" name="[1].Author" value="Steve McConnell" />
<input type="submit" />
</form>
You can have this done for you by the EditorTemplates feature of asp.net mvc which is exemplified in this article.