You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Application-Web/src/Validation/FieldValidationFail.php

43 lines
1.2 KiB

<?php
namespace App\Validation;
/**
* An error that concerns a field, with a bound message name
*/
class FieldValidationFail extends ValidationFail {
private string $fieldName;
/**
* @param string $fieldName
* @param string $message
*/
public function __construct(string $fieldName, string $message) {
parent::__construct("Champ invalide", $message);
$this->fieldName = $fieldName;
}
public function getFieldName(): string {
return $this->fieldName;
}
public static function invalidChars(string $fieldName): FieldValidationFail {
return new FieldValidationFail($fieldName, "field contains illegal chars");
}
public static function empty(string $fieldName): FieldValidationFail {
return new FieldValidationFail($fieldName, "field is empty");
}
public static function missing(string $fieldName): FieldValidationFail {
return new FieldValidationFail($fieldName, "field is missing");
}
/**
* @return array<string, string>
*/
public function jsonSerialize(): array {
return ["field" => $this->fieldName, "message" => $this->getMessage()];
}
}