Skip to content

Instantly share code, notes, and snippets.

View alexonozor's full-sized avatar
🏠
Working from home

Alex Onozor alexonozor

🏠
Working from home
View GitHub Profile
var Engine = (function(global) {
var doc = global.document,
win = global.window,
canvas = doc.createElement('canvas'),
ctx = canvas.getContext('2d'),
lastTime;
canvas.width = 505;
canvas.height = 606;
doc.body.appendChild(canvas);
@alexonozor
alexonozor / rubycode.rb
Created April 22, 2018 15:25
Code snippet from my ruby hangman gem.
def menu
option_choice = get_input('')
case option_choice
when "1"
menu_start_game
when "2"
load
when "3"
menu_instruction
when "4", "X", "x", "Q", "q", :quit
//rest the the entire form values and status.
this.userForm.reset();
//rest the user name and it status.
this.userForm.reset({
userName: ''
});
this.userForm.patchValue({
userName: 'alexonozor'
});
this.userForm.setValue({
userName: "AlexOnozor",
firstName: "Alex",
lastName: "Obogbare"
address: { ... } // reduce for brevity
});
const userForm = new FormGroup({
userName: new FormControl('', [
Validators.required,
Validators.minLength(2),
forbiddenNameValidator(/bob/i) // <-- Here's how you pass in the custom validator.
]),
firstName: new FormControl('', [ Validators.required, Validators.minLength(2)]),
lastName: new FormControl(''),
address: new FormGroup({
street: new FormControl('', Validators.required),
/** A user name can't match the given regular expression */
export function forbiddenUserNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
const forbidden = nameRe.test(control.value);
return forbidden ? {'forbiddenName': {value: control.value}} : null;
};
}
const userForm = new FormGroup({
userName: new FormControl('', [ Validators.required, Validators.minLength(4)]),
firstName: new FormControl('', [ Validators.required, Validators.minLength(2)]),
lastName: new FormControl(''),
address: new FormGroup({
street: new FormControl('', Validators.required),
city: new FormControl(''),
zipCode: new FormControl('')
})
});
<p>UserName value: {{ userForm.get('userName').value }}</p>
//Get the street of the nested address
<p>Street value: {{ userForm.get('address.street').value}}</p>
<div formGroupName="address" class="well well-lg">
<div class="form-group">
<label class="center-block">Street:
<input class="form-control" formControlName="street">
</label>
</div>
<div class="form-group">
<label class="center-block">City:
<input class="form-control" formControlName="city">
</label>