www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibed how do I convert form values into a struct automatically?

reply Chris Bare <chris bareflix.com> writes:
I know I can do this manually by looking at HTTPServerRequest 
req, but the documentation for registerWebInterface sounds like 
there's an automatic way to do this.
```
<form id="employeeform" action="employeeform" method="POST">
     <input class="textfield" name="first_name" value="Homer" 
required/>
     <input class="textfield" name="last_name" value="Simpson" 
required/>
</form>
```
```d
struct Employee
{
	long id;
	string first_name;
	 string last_name;
}
```
in my web interface service I tried:
```d
void postEmployeeform (Employee employeeform)
```
but that won't compile. The following compiles and works:
```d
void postEmployeeform (string first_name, string last_name)

```
What am I missing?
Nov 10 2021
parent reply Chris Bare <chris bareflix.com> writes:
On Wednesday, 10 November 2021 at 19:23:44 UTC, Chris Bare wrote:
 What am I missing?
I figured out part of it. In the html input element the name has to be employeeform.first_name. but now I get a different error: Error handling field 'employeeform.id': Missing form field. I tried declaring the id as optional, but that did not make a difference. ```d struct Employee { optional long id; string first_name; string last_name; } ``` Is there another way to indicate that a field does not have to be in the form?
Nov 10 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Wednesday, 10 November 2021 at 20:23:07 UTC, Chris Bare wrote:
 On Wednesday, 10 November 2021 at 19:23:44 UTC, Chris Bare 
 wrote:
 What am I missing?
I figured out part of it. In the html input element the name has to be employeeform.first_name. but now I get a different error: Error handling field 'employeeform.id': Missing form field. I tried declaring the id as optional, but that did not make a difference. ```d struct Employee { optional long id; string first_name; string last_name; } ``` Is there another way to indicate that a field does not have to be in the form?
Make it a `Nullable!int` -Steve
Nov 10 2021