Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created July 2, 2017 11:18
Show Gist options
  • Save Armenvardanyan95/2683e4300326af1c1a87e5b6096c748b to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/2683e4300326af1c1a87e5b6096c748b to your computer and use it in GitHub Desktop.
interface Movie {
id: number;
title: string;
}
@Component({
selector: 'app-some-component-with-form',
template: `...` // our form is here
})
export class SomeComponentWithForm {
public form: FormGroup;
public movies: Array<Movie>
constructor(private formBuilder: FormBuilder){
this.form = formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
age: ['', Validators.max(120)],
favoriteMovies: [[]], /*
we'll have a multiselect dropdown
in our template to select favorite movies
*/
});
}
public onSubmit(values){
/*
'values' is actually a form value, which represents a user
but imagine our API does not expect as to send a list of movie
objects, just a list of id-s, so we have to map the values
*/
values.favouriteMovies = values.favouriteMovies.map(movie => movie.id);
// then we will send the user data to the server using some service
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment