epartment/nova-unique-ajax-field
A Laravel Nova field that validates uniqueness via AJAX as users type, giving instant feedback and preventing duplicate values without waiting for form submission. Easy to add to Nova resources and ideal for usernames, emails, slugs, and other unique identifiers.
This package provides a Laravel Nova field that validates a column's uniqueness via AJAX while the user types, instead of waiting for form submission. Version 1.0.2 is a breaking rename: the field class is now Unique (previously UniqueAjax).
Install and upgrade:
composer require <vendor>/<package>:"^1.0.2"
Look first at:
Unique) and its service provider.UniqueAjax references if upgrading from ≤1.0.1.First use case — replace a standard Nova text field on a resource:
use Vendor\Package\Fields\Unique;
public function fields(NovaRequest $request)
{
return [
Unique::make('Email')
->rules('required', 'email'),
];
}
Confirm the AJAX check works in the Nova UI by typing a value that already exists in the underlying table. You should see inline feedback before saving.
Basic uniqueness checking
Use the field anywhere you would use Text, Email, or another single-column field. The field handles the AJAX lookup and Nova form integration.
Unique::make('Username')
->rules('required', 'alpha_dash', 'min:3')
Ignore the current model on update Uniqueness checks on edit forms must exclude the record being edited. Most implementations do this automatically via Nova's resource/model resolution, but verify against your package version and configure the ignored primary key if necessary.
Custom table/column mapping When the field name does not match the database column, set the column explicitly:
Unique::make('Display Email', 'email')
->rules('required', 'email')
Scoped uniqueness (multi-tenant or composite constraints) If uniqueness is only required within a tenant, parent record, or another scope, use Nova's validation rules on submit and consider the package's field-level options for scope/where clauses. Do not assume the AJAX check knows about your application's tenancy scope unless configured.
Pair with server-side rules Treat the AJAX field as UX enhancement, not a replacement for backend validation:
Unique::make('Email')
->rules('required', 'email', Rule::unique('users', 'email')->ignore($this->user))
This protects against race conditions, direct API writes, and disabled JavaScript.
Reuse across resources Create a small helper or base resource method if many resources use the same field configuration:
protected function uniqueEmailField()
{
return Unique::make('Email')
->rules('required', 'email');
}
Testing Feature tests should assert the underlying validation rules, not the AJAX UI. For browser tests, assert that the field renders and that Nova's AJAX endpoint responds appropriately when authenticated.
UniqueAjax and replace it with Unique, including use statements, field instantiations, config files, cached files, and tests. A global search-and-replace is usually sufficient, but check for string references in config or dynamically constructed class names.php artisan optimize:clear and, if applicable, rebuild Nova assets. Stale compiled views or config caches can leave references to the old class.Rule::unique(...) or equivalent server-side rule.deleted_at, tenant IDs, or other global scopes unless configured. Verify the generated query or use a custom validation callback.Foo and foo as duplicates while PostgreSQL does not. Confirm behavior matches your product requirements.^1.0.2 (or the appropriate range) and run the upgrade in a branch before merging.How can I help you explore Laravel packages today?