I'm trying to validate that a string-input is a valid CSS color.
So, I've got a custom validator in my Angular App (based on some tutorials and some validation suggestions found on SO):
import { AbstractControl, ValidationErrors } from '@angular/forms';
export class GamePlatformValidator {
static mustBeValidColor(control: AbstractControl): ValidationErrors | null {
const ele = document.createElement('div');
ele.style.color = control.value as string;
const color = ele.style.color.split(/\s+/).join('').toLowerCase();
if (color === '') {
ele.remove();
return { mustBeValidColor: true };
}
return null;
}
}
I understand what's happening in the code, and the above code validates the string just fine, but it clearly can't be used as is since a hard-refresh of the page would require the document to be ready for this code to work.
Unfortunately, I have no idea how to fix this, as I'm fairly new to angular and practical javascript in general.
Any suggestions would be most appreciated.
Updated with Implementation
'platform.component.ts' (partial)
import { PlatformValidator } from './platform.validator';
export class GamesPlatformsComponent implements OnInit {
public form = new FormGroup({
platform: new FormGroup({
id: new FormControl(),
name: new FormControl('', [Validators.required, Validators.minLength(2)]),
icon: new FormControl('', [Validators.required, GamePlatformValidator.startsWithDot]),
color: new FormControl('' GamePlatformValidator.mustBeValidColor),
hidden: new FormControl(false)
})
});
}
'platform.component.html' (partial)
<div class="input-group">
<span class="input-group-addon">Color</span>
<input formControlName="color"
id="color" type="text"
class="form-control"
placeholder="Enter #color-code..." />
</div>
<div *ngIf="color.invalid && color.touched" class="alert alert-danger top-margin">
<div *ngIf="color.errors.mustBeValidColor">
<p>Must be a valid css color.</p>
<hr />
<h6>Examples:</h6>
<ul>
<li>red</li>
<li>#f00</li>
<li>#ff0000</li>
<li>rgb(255,0,0)</li>
<li>rgba(255,0,0,1)</li>
</ul>
</div>
</div>
To clarify: The validator works fine when I don't browse to the form page directly. So, if the apps is able to load by navigating to any other url, the validation will work fine. However, if I browse directly to the page with the form, 'document will not be defined' yet since it's still being loaded from the server.