- Forms
- HTML
- Validation
HTML Form Validation with Hygular: Rules, Feedback, and Server Errors
Use required attributes, custom validation rules, and server field errors with Hygular forms while keeping validation responsibilities clear.
A form already describes much of its own behavior: field names, input types, required values, and where a submission goes. Hygular builds on that markup with custom rules, feedback generation, and an optional fetch-based submission flow.
The useful distinction is between a missing value and an invalid value. required makes a field mandatory. A data-rule checks a value when one is present. Mixing those responsibilities can turn an optional field into an unexpected obstacle.
Start with explicit field requirements
Load @hygular/core/css, then initialize the browser runtime after the form exists. The form marker opts it into Hygular's delegated behavior.
<form data-hygular="form" action="/subscribe" method="post">
<label for="subscriber-email">Email address</label>
<input
id="subscriber-email"
name="email"
type="email"
class="form-input"
required
data-rule="email"
data-required-msg="Enter your email address."
data-error-msg="Enter a valid email address."
/>
<label for="subscriber-site">Website (optional)</label>
<input
id="subscriber-site"
name="website"
type="url"
class="form-input"
data-rule="url"
/>
<button type="submit" class="button is-primary">Subscribe</button>
</form>
/subscribe is an application endpoint that you must implement. The framework does not supply a mailing list service or store submissions.
An empty website field remains optional. data-rule="required" is not the way to require a value; use the native required attribute. Labels remain part of the markup even when validation feedback is generated for you.
Add a custom rule for domain-specific input
Named rules are useful when the same constraint appears in several forms. Register the rule before the user interacts with those forms:
import { Form } from '@hygular/core';
Form.addRule('project-code', (value) =>
/^[A-Z]{3}-\d{4}$/.test(value)
? true
: 'Use three capital letters followed by a hyphen and four digits.'
);
<label for="project-code">Project code</label>
<input
id="project-code"
name="projectCode"
class="form-input"
required
data-rule="project-code"
placeholder="ABC-1234"
/>
Place the field inside a form marked with data-hygular="form". The placeholder illustrates the format; it does not replace the label or the error message.
For a one-off pattern, data-regex is another option. Keep complex business rules in application code when a small attribute would hide more logic than it explains.
Enable asynchronous submission deliberately
Add data-ajax="true" to opt into Hygular's fetch-based submission. The form engine manages submission state and exposes hygular.form.start, hygular.form.success, and hygular.form.error events.
<form
data-hygular="form"
data-ajax="true"
action="/api/subscribe"
method="post"
>
<label for="signup-email">Email address</label>
<input
id="signup-email"
name="email"
type="email"
class="form-input"
required
data-rule="email"
/>
<button type="submit" class="button is-primary">Subscribe</button>
</form>
Your endpoint must accept the request format produced by the form and return a response your application can handle. Authentication, authorization, anti-abuse measures, and persistence remain server responsibilities.
Match server errors to field names
For a failed submission with field-level validation errors, the engine recognizes an errors object keyed by the field's name:
{
"errors": {
"email": "This address is already subscribed."
}
}
The server should return an appropriate failure status with that body. Hygular can associate the message with name="email", set custom validity, and mark feedback with form-feedback is-server with-error. Returning an array of messages uses its first entry for the field.
This is why stable field names matter. A visible label can change without changing the API contract. A server response keyed by a different name cannot be attached to the intended input automatically.
Decide who owns the form
In React, useForm() provides delegated setup and cleanup. Avoid attaching both that hook and another form system that independently owns the same submission and feedback nodes without an explicit integration plan.
Hygular's approach fits forms where the DOM is the main representation of field state. A schema-driven application may prefer its existing form library. The goal is a clear owner for validation and submission, not fewer dependencies at any cost.
Client-side validation provides feedback before a request. Repeat the required checks on the server, and test keyboard focus, optional fields, corrected values, and rejected submissions. See the validation reference for the rule API.