9

We have a particular field that can only allow input when adding the record for the first time, so I'm wondering if it is possible to add a class, or specify readonly at some point after the form has been loaded, but (of course), before it is rendered to the user.

When loading a form from models\forms\myform.xml, attributes like class(es) and readonly are being loaded as expected. Here's the way that the field is currently being rendered, which uses libraries\joomla\form\form.php:

echo $this->form->getInput('myReadOnlyCode')
3

Yes you can do this.

We have a component that has a concept of "Plans", it uses the same view for different access levels, but makes the fields accessible or not depending on the users groups.

So for those uses that can "Run" a plan but not edit it, we turn "off" a bunch of fields. Depending on the field type this may mean setting several field attributes, e.g.

$this->form->setFieldAttribute('name', 'class', 'readonly');
$this->form->setFieldAttribute('name', 'readonly', 'true');
$this->form->setFieldAttribute('description', 'class', 'readonly');
$this->form->setFieldAttribute('description', 'disabled', 'true');
$this->form->setFieldAttribute('description', 'type', 'text');
$this->form->setFieldAttribute('published', 'class', 'readonly');
$this->form->setFieldAttribute('published', 'readonly', 'true');
$this->form->setFieldAttribute('publish_up', 'class', 'readonly');
$this->form->setFieldAttribute('publish_up', 'readonly', 'true');
$this->form->setFieldAttribute('publish_up', 'format', '%Y-%m-%d %H:%M:%S');
$this->form->setFieldAttribute('publish_up', 'filter', 'user_utc');
$this->form->setFieldAttribute('publish_down', 'class', 'readonly');
$this->form->setFieldAttribute('publish_down', 'readonly', 'true');
$this->form->setFieldAttribute('publish_down', 'format', '%Y-%m-%d %H:%M:%S');
$this->form->setFieldAttribute('publish_down', 'filter', 'user_utc');

So, depending on what your myReadOnlyCode field is you can do it by setting one or more of the attributes as shown above, e.g. if it's just a standard text input:

$this->form->setFieldAttribute('myReadOnlyCode', 'class', 'readonly');
$this->form->setFieldAttribute('myReadOnlyCode', 'readonly', 'true');
2

Compare the Joomla core article edit. Administrator - article.php - method getForm.

Be aware of the filter to prevent "backdoor" update.

    $user = JFactory::getUser();

    // Check for existing article.
    // Modify the form based on Edit State access controls.
    if ($id != 0 && (!$user->authorise('core.edit.state', 'com_content.article.' . (int) $id))
        || ($id == 0 && !$user->authorise('core.edit.state', 'com_content'))
    )
    {
        // Disable fields for display.
        $form->setFieldAttribute('featured', 'disabled', 'true');
        $form->setFieldAttribute('ordering', 'disabled', 'true');
        $form->setFieldAttribute('publish_up', 'disabled', 'true');
        $form->setFieldAttribute('publish_down', 'disabled', 'true');
        $form->setFieldAttribute('state', 'disabled', 'true');

        // Disable fields while saving.
        // The controller has already verified this is an article you can edit.
         $form->setFieldAttribute('featured', 'filter', 'unset');
        $form->setFieldAttribute('ordering', 'filter', 'unset');
         $form->setFieldAttribute('publish_up', 'filter', 'unset');
         $form->setFieldAttribute('publish_down', 'filter', 'unset');
         $form->setFieldAttribute('state', 'filter', 'unset');
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.