sulu

Disable form fields in Sulu's admin frontend after the entity has been persisted


I'd like to connect the key of a category in the Sulu CMS with specific business logic. But therefore the key must not be changeable in the admin area. Is there a way to disable a form field once the entity is persisted?


Solution

  • It's possible to override any form configuration by creating a file with the same name and the same <key> in the config/forms/ directory of your project. In case of the category details form, create a config/forms/category_details.xml file with <key>category_details</key> containing only the properties you want to override, in your case the <property name="key">. You can omit the other properties, because all the form configurations with the same key will be merged together. Then you can use a disabledCondition to configure when this property should be disabled and when it should be possible to edit, in your case something like disaledCondition="!!id", because then the property is only editable, as long as it doesn't have an id, which is only the case when you create a new category. You probably also want to add the mandatory="true" attribute, if your application depends on the key of a category. So the whole file should look like this:

    <?xml version="1.0" ?>
    <form xmlns="http://schemas.sulu.io/template/template"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/form-1.0.xsd"
    >
        <key>category_details</key>
    
        <properties>
            <property name="key" type="text_line" mandatory="true" disabledCondition="!!id">
                <meta>
                    <title>sulu_admin.key</title>
                </meta>
            </property>
        </properties>
    </form>