Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Can't use old variable with dynamic key #3514

@duncanmcclean

Description

@duncanmcclean

Bug Description

I've got a site where we loop over form fields. When looping through, we need to be able to access old variables by their keys. However, we don't seem to be able to use a 'dynamic' key (where we get the field handle for example).

Here's a list of Antlers syntax I've tried:

  • {{ old[handle] }}
  • {{ old:[handle] }}
  • {{ old{handle} }}
  • {{ old:{handle} }}

How to Reproduce

  • Create a form with fields with required fields
  • Add form to template & loop through the fields
  • When looping through, try to do value="{{ old:{handle} }}" on one of the form inputs.
  • Submit the form (and know there's gonna be validation errors).
  • Once page has reloaded, the output won't be as expected (the old value)

For the record, this is something that was broken on v3.0 as well, I just never got round to reporting it then.

Extra Detail

I can't quite remember which ones which but one of them returned the actual Antlers code {{ old:{handle} }} for example, whereas the other just returned an empty string. I imagine this is a parser/regex issue.

Workaround

I've developed a workaround tag for the meantime until this is properly fixed, in case it's useful for anyone else.

<?php

namespace App\Tags;

use Statamic\Tags\Tags;

class OldValueTag extends Tags
{
    protected static $handle = 'old_value';

    // {{ old_value :key="handle" }}

    // this tag is used to get old values of fields. Statamic's {{ old }}
    // doesnt work if you need to pass in a handle from a form. Hence the need
    // for this custom tag

    public function index()
    {
        return old($this->params->get('key'));
    }
}

Environment

Statamic 3.1.6 Solo
Laravel 8.36.2
PHP 8.0.3
doublethreedigital/duplicator 1.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions