Validation
ValidContactValue is a standalone, reusable ValidationRule — apply it to any field. It normalizes the value first, then validates it against the kind’s rules and the 255-character cap:
use RoundlyConsulting\Contacts\Enums\ContactType;
use RoundlyConsulting\Contacts\Rules\ValidContactValue;
$request->validate([
'email' => ['required', new ValidContactValue(ContactType::Email)],
]);One-line FormRequest rules
ContactType::rules() returns a ready rule array — the kind’s field rules (required, string, …) plus ValidContactValue, which checks the normalized value — so a FormRequest stays a one-liner and accepts exactly what addEmail(), addPhone() and addUrl() accept (' [email protected] ', '+421 900 000 000', 'example.com'):
public function rules(): array
{
return [
'email' => ContactType::Email->rules(), // or ContactType::rulesFor(ContactType::Email)
'phone' => ContactType::Phone->rules(),
];
}For a registered custom kind use ContactRules::forValue(ContactType::Custom, 'whatsapp'). The validated input is still the raw string; the package normalizes it when you add it.
Repeatable contact arrays
For a repeatable list of {type, value} pairs, generate contacts.* rules: each type must be a built-in or registered kind, and each value is normalized and validated against the rules of the kind its entry declares:
use RoundlyConsulting\Contacts\Facades\Contacts;
use RoundlyConsulting\Contacts\Support\ContactRules;
$request->validate(Contacts::validationRules()); // keys: contacts, contacts.*.type, contacts.*.value
$request->validate(ContactRules::forArray('people')); // custom prefix
Contacts::validationRules();
// [
// 'contacts' => ['sometimes', 'array'],
// 'contacts.*.type' => ['required', 'string', 'in:email,phone,address,url,social,custom'],
// 'contacts.*.value' => ['required', 'string', new ValidContactEntryValue],
// ]
ContactRules::kinds(); // built-in kinds + every key in contacts.types
ContactRules::forValue(ContactType::Url); // same as ContactType::Url->rules()
ContactRules::forValue(ContactType::Custom, 'whatsapp'); // a registered custom kind's rulesThe type rule accepts the six built-ins plus every registered custom kind. The value rule, ValidContactEntryValue, reads the sibling type of the same entry — contacts.3.value against contacts.3.type — and applies that kind’s rules, a custom kind’s registered ones included. An entry whose type is missing or unknown is left to the type rule, which reports it.
Error messages
Failures use the contacts::errors.invalid_value translation (“The given value is not a valid :type contact.”), naming the custom kind rather than custom where one applies. Publish contacts-translations to reword or translate it.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.