Skip to content

Instantly share code, notes, and snippets.

View CharlieGreenman's full-sized avatar
🔎
Have you found my lost AI?

Charlie Greenman CharlieGreenman

🔎
Have you found my lost AI?
View GitHub Profile
<trans-unit id="f99f34ac9bd4606345071bd813858dec29f3b7d1" datatype="html">
<source>The author is <x id="ICU" equiv-text="{gender, select, male {...} female {...} other {...}}"/></source>
<target>L'auteur est <x id="ICU" equiv-text="{gender, select, male {...} female {...} other {...}}"/></target>
</trans-unit>
<trans-unit id="eff74b75ab7364b6fa888f1cbfae901aaaf02295" datatype="html">
<source>{VAR_SELECT, select, male {male} female {female} other {other} }</source>
<target>{VAR_SELECT, select, male {un homme} female {une femme} other {autre} }</target>
</trans-unit>
ngOnInit() {
this.myForm = this.fb.group({
name: ['', Validators.required],
email: [
'',
[Validators.required, Validators.email],
ValidateEmailNotTaken.createValidator(this.signupService),
{updateOn: 'blur'}
]
});
<form [formGroup]="myForm">
<input type="text" formControlName="name">
<input type="email" formControlName="email">
<!-- Other related errors go here-->
<div *ngIf="myForm.get('email').errors && myForm.get('email').errors.emailUnavailable">
This email is already taken.
</div>
</form>
ngOnInit() {
this.myForm = this.fb.group({
name: ['', Validators.required],
email: [
'',
[Validators.required, Validators.email],
ValidateEmailNotTaken.createValidator(this.signupService)
]
});
}
constructor(private fb: FormBuilder,
private loanAmountValidatorService: LoanAmountValidatorService) { }
ngOnInit() {
this.newsletterForm = this.fb.group({
email: ['', [Validators.required, Validators.email]],
}, {validators: this.loanAmountValidatorService.loanAmountValidator});
}
import { Directive } from '@angular/core';
import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator } from '@angular/forms';
import { LoanAmountValidatorService } from './loan-amount-validator.service';
@Directive({
selector: '[razrooLoanAmount]',
providers: [{ provide: NG_VALIDATORS, useExisting: LoanAmountDirective, multi: true }]
})
export class LoanAmountDirective implements Validator {
constructor(private loanAmountValidatorService: LoanAmountValidatorService) {}
import { Injectable } from '@angular/core';
import { FormGroup, ValidationErrors, ValidatorFn } from '@angular/forms';
@Injectable({
providedIn: 'root'
})
export class LoanAmountValidatorService {
constructor() { }
<mat-form-field [formGroup]="newsletterForm" class="email-field" >
<input matInput razrooNumber formControlName="email" placeholder="Your E-mail" required>
</mat-form-field>
<ng-container *ngIf="email.invalid && (email.dirty || email.touched)">
<mat-error *ngIf="email.errors.number">Not a number</mat-error>
</ng-container>
@CharlieGreenman
CharlieGreenman / number-directive.ts
Created September 20, 2019 12:36
number-directive.ts
import { Directive, forwardRef } from '@angular/core';
import { AbstractControl, NG_VALIDATORS, Validator } from '@angular/forms';
import { number } from './validator';
const NUMBER_VALIDATOR: any = {
provide: NG_VALIDATORS,
useExisting: forwardRef(() => NumberDirective),
multi: true
};
@CharlieGreenman
CharlieGreenman / number-validator.ts
Created September 20, 2019 12:34
Number validator file
import { AbstractControl, Validators, ValidatorFn } from '@angular/forms';
function isPresent(obj: any): boolean {
return obj !== undefined && obj !== null;
}
export const number: ValidatorFn = (control: AbstractControl): {[key: string]: boolean} => {
if (isPresent(Validators.required(control))) return null;
let v: string = control.value;