How to raise an error from a PostRevisor block and have it presented to the user?

Here’s a bit of code where I want to add some additional validation

(simplified code and use case)

# prevent non-staff from changing category on a topic
  PostRevisor.track_topic_field(:category_id) do |tc, category_id|
    if tc.guardian.is_staff?
      tc.record_change('category_id', tc.topic.category_id, category_id)
      tc.topic.category_id = category_id
    else
       tc.topic.errors[:base] << "you can't change category on a this kind of topic"
    end
  end

this will prevent the Category being changed by non-staff but does not bubble up an error.

This used to work:

That plugin has since been archived though.

This requirement should surely be supported by the API?

You’re better at this stuff than I am, but I’d think that Rails doesn’t need to bubble this back up because it’s expected that Ember won’t let the user select a category that’s not one that the user should be able to select. So maybe make sure that the front end keeps them from changing the category if they aren’t supposed to so PostRevisor is never confronted with this uncomfortable situation?

1 Like

yeah, sure, that’s a consideration.

It’s good practice to limit at the API end first, then front end if you can do it - ie you should never rely on only the front end to prevent an illegal action.

In addition, the front end should bubble up Active Record errors.

So performing this at the API is important.

Agreed. You want Rails to enforce it.

I thought I understood that it was enforcing the requirement and refusing to make the change, but just not providing a good-enough error to the front end.

1 Like

Yes, you are right.

I’m only trying to prevent a Category change, so the UI is kind of unhelpful in that it’s designed to allow you to change all the meta, or none at all.

Thus a bubbled up error would be really handy.

The rigidness of .gjs templates make these kind of changes very tricky now.

This template is still hbs but I don’t want to override it for obvious reasons.

I’ll try a CSS hack.

Again, you’re better at that than I am, but I’d think it’d be possible to hide the category selector that looks like it won’t work.

So maybe the thing to do is hack the category serializer so that it doesn’t send any categories. Seems tricky, though, as you’d need to figure out how to hack it only when you were editing a topic rather than creating one.

1 Like

“Hack” worked :tada: :

I’d still like a verbose and sensible response from the server and especially one that leveraged Active Record.