I have a form and I want to display data that comes from an API (JSon file) in mode readonly. Do you have tracks?
I take an example of a form:
Thank you
<div class="col-sm-8 col-sm-offset-2">
<form [formGroup]="userForm" (ngSubmit)="onSubmitForm()">
<div class="form-group">
<label for="firstName">Prénom</label>
<input type="text" id="firstName" class="form-control" formControlName="firstName">
</div>
<div class="form-group">
<label for="lastName">Nom</label>
<input type="text" id="lastName" class="form-control" formControlName="lastName">
</div>
<div class="form-group">
<label for="email">Adresse e-mail</label>
<input type="text" id="email" class="form-control" formControlName="email">
</div>
<div class="form-group">
<label for="email">Adresse address</label>
<input type="text" id="address" class="form-control" formControlName="address">
</div>
<button type="submit" class="btn btn-primary">Soumettre</button>
</form>
</div>
File JSON
User:
- User1:
firstName: toto
lastName: titi
email: toto#toto.com
address: 2 rue titi
In your component (.ts file), as soon as the data comes back (typically inside of your ngOnInit), you should be able to do this.userForm.setValue(data).
As for making them readonly, an easy way I know is to set the FormGroup as disabled when you create it. So userForm = new FormGroup({_property_names_}, { disabled: true }).
Related
<div class="card-body">
<form action="" >
<div class="row">
<div class="col-5">
<label for="">From Date</label>
<input type="date" class="form-control" name="from" id="from" value="{{old('from')}}" required>
</div>
<div class="col-5">
<label for="">To Date</label>
<input type="date" class="form-control" name="to" id="to" value="{{old('to')}}" required>
</div>
<div class="col2 pt-4 mt-2">
<input type="submit" class="btn btn-success" value="search">
</div>
</div>
</form>
</div>
I want my from data after submission. i am using get request to search data and refresh the page with new data with from date fields with old search data
As I understood you want to show new data on your page while the from and to are from your previous request ?
Then why are you not returning the to and from with new data.
<input type="date"
class="form-control"
name="from"
id="from"
value= #if($from) "{{$from}}" #endif required>
<input type="date"
class="form-control"
name="to"
id="to"
value= #if($to) "{{$to}}" #endif required>
from controller return data that you want to return after applying the filters with the to and from
So i'm trying to store an input from my form into a property but when I submit my console.log returns undefined in the console itself.
These are the variations I tried but didn't work
<form>
<input #in1 type="text" id="username" placeholder="username" value="5">
<input type="password" id="password" placeholder="password">
<input type="submit" (click)="onSubmit(in1.value)" id="submit" placeholder="submit">
</form>
<form #myForm="ngForm">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" id="Log-email" ngControl="Email" ([ngModel])="SUsername.Email" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1" >Password</label>
<input type="password" id="Log-pass" ngControl="pass" [(ngModel)]="SPassword.pass">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="button" (click)="test()" value="test">
</form>
And heres the typescript part of it
SUsername:string;
SPassword:string;
onSubmit(){
console.log(this.SUsername,this.SPassword);
}
I will do with this:
private model : any = { Email: "", pass : "" };
EDIT:
HTML Code with (ngSubmit)="onSubmit()" and [(ngModel)] (which was
already ([ngModel]))
Removed <input type="button" (click)="test()" value="test">
HTML Code:
<form #myForm="ngForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" id="Log-email" ngControl="Email" [(ngModel)]="model.Email" name="Email" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1" >Password</label>
<input type="password" id="Log-pass" name="Email" ngControl="pass" [(ngModel)]="model.pass">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Make Sure:
you have written [(ngModel)] not ([ngModel])
A working demo with your code example:)
SUsername and SPassword should be initialized as objects
SUsername = {Email: ''}
SPassword= {pass: ''}
you need to have a name attribute for the Html tag element that is same as ([ngModel])="SUsername.Email"
I want to display the data of the selected user in angular 2. I make a service to get the data from API
getUserById(id: string): Promise<User> {
const url ='api/user/${id}';
return this.httpClient.get(url)
.toPromise()
.then(response => response.json() as User)
.catch(this.handleError); }
and this is my controller
user: User;
constructor(private userService: UserService) {
userService.getUserById('45835708-f55a-40fb-878f-33b9c920e196')
.then(hasil => this.user = hasil)
.catch(this.handleError);
}
and here is my template
<div class="form-group">
<label class="col-sm-2 control-label">FULL NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">PASSWORD</label>
<div class="col-sm-6">
<input class="form-control" type="password" value="" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DATE OF BIRTH</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" disabled>
</div>
</div>
How to display the json to my template? Do I make correct service and controller?
You can choose to use template driven forms or reactive driven forms.
this.user object can be a model to that form, and when you input something into html input elements, object will update automatically:
<div class="form-group">
<label class="col-sm-2 control-label">FULL NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" [(ngModel)]="user.fullName" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">USER NAME</label>
<div class="col-sm-6">
<input class="form-control" type="text" value="" [(ngModel)]="user.username" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">PASSWORD</label>
<div class="col-sm-6">
<input class="form-control" type="password" value="" [(ngModel)]="user.password" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DATE OF BIRTH</label>
<div class="col-sm-6">
<input class="form-control" type="date" value="" [(ngModel)]="user.dateOfBirth" disabled>
</div>
</div>
If you want to add some validator, you should go with Reactive forms:
https://angular.io/docs/ts/latest/guide/reactive-forms.html
The principle here is to create a form group and define individual properties with their initial values and validator.
First make sure you have FormsModule imported in your module. Then... remember to use ngModel to bind the user to the form.
But, forms do not care about ngModel unless there is also a name attribute on each field:
<input class="form-control" name="fullName" [ngModel]="user.fullName">
OR
ngModelOptions in each field:
<input class="form-control" [ngModelOptions]="{standalone: true}" [ngModel]="user.fullName">
Here's a demo for you with usage of the name attribute:
Plunker
I have a contact view in my app, and it contains a form, which is defined as below:
<form role="form" style="width:100%; float:left;">
<div class="form-group">
<img src="~/Content/img/zarf.png" class="zarf-pos">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Name and Surname</label>
<input type="text" name="fullName" class="form-control" id="exampleInputEmail1" placeholder="Enter your full name" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">E-Mail</label>
<input type="email" name="emailAddress" class="form-control" id="exampleInputPassword1" placeholder="Enter your e-mail address" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Write to us</label><br>
<textarea name="message" class="form-control" rows="5" placeholder="How can we help you?" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default reg-position">Send</button>
</div>
</form>
Then, in my controller, I have two ActionResults, one HttpGet, and one HttpPost, defined like this:
[HttpGet]
public ActionResult Contact()
{
return View();
}
[HttpPost]
public ActionResult Contact(string fullName, string emailAddress, string message)
{ // some functionality }
But, when I fill out the form and click the Send button, it redirect to the empty Contact page. The point is that I need to have two Contact methods, while in my Post method I have some code that assumes that the strings are not empty. And if I have just one contact method, I get an error message saying the string is null, and the view is not displayed. So, I decided to have two methods, but don't know how to call the post method when the button is pressed.
Assuming you are using Razor to create your views, define the form like so:
#using( Html.BeginForm() )
{
<div class="form-group">
<img src="~/Content/img/zarf.png" class="zarf-pos">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Name and Surname</label>
<input type="text" name="fullName" class="form-control" id="exampleInputEmail1" placeholder="Enter your full name" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">E-Mail</label>
<input type="email" name="emailAddress" class="form-control" id="exampleInputPassword1" placeholder="Enter your e-mail address" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Write to us</label><br>
<textarea name="message" class="form-control" rows="5" placeholder="How can we help you?" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default reg-position">Send</button>
</div>
}
This will ensure the correct <form> element is generated to post back to your action.
P.S. Just some nomenclature housekeeping... You don't call an ActionResult, you call Actions and they return ActionResults. It'll help when you're talking to other developers if you're using the correct names for things.
Here is my HTML
<form class="form-horizontal" action="/" method="POST">
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input type="text" id="email" placeholder="Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Sign In</button>
</div>
</div>
</form>
When I submit form, on server I try to print it, I do
def post(self):
print 'data :', request.form
I get
data : ImmutableMultiDict([])
I also try
def post(self):
print 'data :', request.json
I get
data : None
What exactly I am missing here
None of your inputs have name attribute
Therefore, nothing sent.
<input type="password" id="password" placeholder="Password">
should be:
<input type="password" id="password" name="password" placeholder="Password">
ID is just for DOM