Custom validation example

It would be very helpful to have an example validation in the docs for custom validations. I am getting hung up creating an error that implements the form error protocol. The following generates an error but is then stripped out before rending the form. error = Ash.Error.to_ash_error("Domain or company is required")
6 Replies
ZachDaniel
ZachDaniel•2y ago
So when given a string, we create an UnknownError which won't show in a form (for security reasons) If you do it like this:
Ash.Changeset.add_error(changeset, field: :field_name, message: "Domain or company is required")
Ash.Changeset.add_error(changeset, field: :field_name, message: "Domain or company is required")
then it will create an Ash.Error.Invalid and that will show up in the form you can use fields instead of field if you want to target multiple fields
TechnoMage
TechnoMageOP•2y ago
Thanks @Zach Daniel Given the above how do I return the modified changeset from the validate function? {:error, changeset} throws an error because it does not have a path. The docs indicate {:error, Ash.error()} which does not appear to accept a changeset?
ZachDaniel
ZachDaniel•2y ago
correct, you can't return a changeset if you want to return a new changeset, you need to use a change instead why do you want to return a new changeset, out of curiosity?
TechnoMage
TechnoMageOP•2y ago
The example you provided above produces a new changeset. I just want the validation to produce an error in the form.
ZachDaniel
ZachDaniel•2y ago
oh, sorry šŸ™‚ {:error, field: :field_name, message: "Message"}
TechnoMage
TechnoMageOP•2y ago
Thanks so much. That worked.

Did you find this page helpful?