XF 2.1 Default value for radiorow

Matt C.

Well-known member
I need to set a default value for the radiorow when adding an entity. I know how to set a default value, but there's just one problem. I'm using the same edit macros for both the add and edit template.

I'm using value={$match.platform} because on the edit page, I need the value to reflect the entity's saved value.
Code:
<xf:radiorow name="platform" value="{$match.platform}" label="{{ phrase('ah_mm_platform') }}">
    <xf:option value="PC" label="{{ phrase('ah_mm_pc') }}"></xf:option>
    <xf:option value="PlayStation 4" label="{{ phrase('ah_mm_playstation_4') }}"></xf:option>
    <xf:option value="Xbox One" label="{{ phrase('ah_mm_xbox_one') }}"></xf:option>
</xf:radiorow>

Is there a way also set a default fallback value when the user is not on the edit page without creating entirely new macros for the add page?
 
Two ways:

Match entity:
PHP:
protected function _setupDefaults()
{
    $this->platform = 'PC';
}

Macro:
HTML:
value="{{ $match.isUpdate() ? $match.platform : 'PC' }}"


Fillip

Okay using isUpdate only effects editing the entity, so I have to use {{ $match.isUpdate() ? $match.platform : '{$match.platform}' }}. I added the _setupDefaults() function to the entity file, but that didn't work.

Thank you Fillip.
 
If you are using _setupDefaults then you should not need to modify the template at all. That template code doesn't look right. If it's not setting the default after adding the entity default function, that would indicate that your actionAdd isn't creating a new empty entity.

PHP:
    /**
     * @return \XF\Mvc\Reply\View
     */
    public function actionAdd()
    {
        /** @var \AH\Matchmaking\Entity\Match $match */
        $match = $this->em()->create('AH\Matchmaking:Match');
       
        return $this->matchAddEdit($match);
    }


Fillip
 
If you are using _setupDefaults then you should not need to modify the template at all. That template code doesn't look right. If it's not setting the default after adding the entity default function, that would indicate that your actionAdd isn't creating a new empty entity.

PHP:
    /**
     * @return \XF\Mvc\Reply\View
     */
    public function actionAdd()
    {
        /** @var \AH\Matchmaking\Entity\Match $match */
        $match = $this->em()->create('AH\Matchmaking:Match');
     
        return $this->matchAddEdit($match);
    }


Fillip

Maybe it's because I'm I have two different functions for Add and Edit?

Code:
protected function matchAdd(\AH\Matchmaking\Entity\Match $match)
{
    $viewParams = [
        'match'       => $match,
    ];

    return $this->view('AH\Matchmaking:Match\Add', 'ah_mm_match_add', $viewParams);
}

protected function matchEdit(\AH\Matchmaking\Entity\Match $match)
{
    $viewParams = [
        'match'       => $match,
    ];

    return $this->view('AH\Matchmaking:Match\Edit', 'ah_mm_match_edit', $viewParams);
}

/*
* @return \XF\Mvc\Reply\View
*/
public function actionAdd()
{
    /** @var \AH\Matchmaking\XF\Entity\User $visitor */
    $visitor = \XF::visitor();
    if (!$visitor->canAddMatch()) {
        return $this->noPermission();
    }

    $match = $this->em()->create('AH\Matchmaking:Match');

    return $this->matchAdd($match);
}

/**
* @param ParameterBag $parameterBag
* @return \XF\Mvc\Reply\View
* @throws \XF\Mvc\Reply\Exception
*/
public function actionEdit(ParameterBag $params)
{
    $match = $this->assertViewableMatch($params->match_id);
    if (!$match->canEdit($error))
    {
        return $this->noPermission($error);
    }

    return $this->matchEdit($match);
}

Okay, I combined the Add and Edit functions and it works now. Thanks!
 
Last edited:
Top Bottom