Issue: Readonly fields editable in Report View

Im having an issue with a field that is set to be readonly based on a checkbox in the doc.

This works fine when editing the form but when I open the report view, im able to edit the fields although they should be Readonly.

Is there a way to add custom validation whenever a field is changed? Im not talking about client scripts, I need something that will work in the Report view and when importing items.

Use case:
I have set the item_name field in the item doctype to be Readonly when it’s a variant. This is necessary because I’m generating the variants item_name from the template’s item_name and the variant’s attributes.

People will use the import and report functionality though, and I don’t want anyone to accidentally mess up the naming scheme.

Any advice?

For anyone looking for a solution, this works perfectly:

if doc.variant_of != None:
    old_item_name = frappe.db.get_value("Item",, "item_name");
    if doc.item_name != old_item_name:
        frappe.throw("Item Names of Variants are derived from their base name and attributes and cannot be changed directly.")

I’m executing this in a server script on the “Before Save” Hook, so this will be executed last after the regular validation.

Thanks to Hans from the German Telegram Channel for getting me on the right track :slight_smile: