Skip to content

Error States

Show validation errors using the if-error directive system. When a field fails validation, SnipForm can show elements, add classes, inject text, and apply styles.

The error system uses two parts:

  1. if-error="fieldname" - Targets a specific field
  2. then-* / else-* - Actions to take when error exists or doesn’t

Show an element when a field has an error:

<input name="email" sf-validate:required sf-validate:email />
<span if-error="email" then-show style="display:none">
Please enter a valid email
</span>

Use then-text without a value to inject the validation message from the API:

<input name="email" sf-validate:email="Invalid email format" />
<span if-error="email" then-show then-text style="display:none"></span>

When validation fails, shows “Invalid email format”.

Use then-show-text as a shorthand:

<span if-error="email" then-show-text style="display:none"></span>

Provide a value to show custom content instead of the API message:

<span if-error="email" then-show then-text="Check your email address" style="display:none"></span>

Apply classes when an error exists:

<div if-error="email" then-class="text-red-500 shake-animation">
<label>Email</label>
<input name="email" sf-validate:email />
</div>

For inputs, use error-class directly (no if-error needed):

<input name="email"
sf-validate:email
error-class="border-red-500 bg-red-50" />

Apply inline styles when an error exists:

<div if-error="email" then-style="border: 2px solid red">
...
</div>
<input name="email"
sf-validate:email
error-style="border: 2px solid red" />

Hide an element when an error exists:

<span if-error="email" then-hide>
Email looks good!
</span>

Show content when there is NO error:

<div if-error="email">
<span then-show class="text-red-500">Invalid email</span>
<span else-text class="text-green-500">Email is valid</span>
</div>

Available else-* attributes:

  • else-class - Classes when valid
  • else-text - Text when valid
  • else-style - Styles when valid

<snip-form key="YOUR_KEY">
<form>
<div>
<label>Email</label>
<input name="email"
sf-validate:required="Email is required"
sf-validate:email="Invalid email"
error-class="border-red-500"
valid-class="border-green-500" />
<div if-error="email">
<span then-show then-text class="text-red-500 text-sm" style="display:none"></span>
<span else-text class="text-green-500 text-sm">Looks good!</span>
</div>
</div>
<button type="submit">Submit</button>
</form>
</snip-form>

AttributeValueDescription
if-errorfield nameTarget field to watch for errors
then-show-Show element on error
then-hide-Hide element on error
then-classCSS classesAdd classes on error
then-texttext (optional)Inject text on error
then-show-texttext (optional)Show + inject text
then-styleCSSAdd inline styles on error
else-classCSS classesAdd classes when valid
else-texttext (optional)Inject text when valid
else-styleCSSAdd inline styles when valid
AttributeValueDescription
error-classCSS classesAdd classes on field error
error-styleCSSAdd inline styles on field error
valid-classCSS classesAdd classes when field valid
valid-styleCSSAdd inline styles when field valid