Fixed helper_user_search_criteria looks for the wrong criteria value in custom user fields

Affected version
2.1.2

DragonByte Tech

Well-known member
Given this code:
HTML:
<xf:set var="$fieldName" value="criteria[user_field]{{ ($choices && $fieldDefinition.type_group != 'multiple') ? '[exact]' : '' }}[{$fieldId}]" />
this means that a multi-select will be saved like so:

Code:
  ["user_field"] => array(1) {
    ["test_multi_select"] => array(1) {
      [0] => string(11) "s:5:"test1""
    }
  }
(Notice the lack of exact)

However, when later looking for the value:
HTML:
<xf:checkbox name="{$fieldName}" value="{$criteria.user_field.exact.{$fieldId}}" listclass="listColumns" readonly="{$readOnly}">
It always expect exact to exist, which will never be true for multi-selects.

Therefore, it is impossible to save criteria with a multi-select user field and load these criteria back into the form with the correct selection.
 

Kier

XenForo developer
Staff member
Okay, what did I do wrong to not be able to reproduce this...

1562599080364.png

... because my user field criteria specifying 'Maybe' are preselected for me when I reload this notice's form

1562599157967.png
 

DragonByte Tech

Well-known member
PHP Code (digestAddEdit):
PHP:
        /** @var \XF\Searcher\User $searcher */
        $searcher = $this->searcher('XF:User', $digest->criteria);
        
        $viewParams = [
            'digest' => $digest,
            'criteria' => $searcher->getFormCriteria(),
            'handlers' => $this->getDigestRepo()->getHandlers()
        ];
        return $this->view('DBTech\Mail:Digest\Edit', 'dbtech_mail_digest_edit', $viewParams + $searcher->getFormData());
PHP Code (save):
PHP:
        $criteria = $this->filter('criteria', 'array');
        $searcher = $this->searcher('XF:User', $criteria);
        $input['criteria'] = $searcher->getRawCriteria();
        
        $form->basicEntitySave($digest, $input);
Template code:
HTML:
        <div class="block-body block-body--collapsible">
            <xf:include template="helper_user_search_criteria">
                <xf:set var="$criteria" value="{$criteria}" />
                <xf:set var="$noSpecificUser" value="{{ true }}" />
            </xf:include>
        </div>
Field Definition:
1562602994884.png


Field as shown in form:
1562602959005.png

Resulting array as saved in the DB (decoded from JSON, truncated to relevant portion):
Code:
array (
  'user_field' =>
  array (
    'country' => '',
    'test_multi_select' =>
    array (
      0 => 's:5:"test1"',
    ),
    'skype' => '',
    'facebook' => '',
    'twitter' => '',
  ),
)
Loading the form back does not work.
 

DragonByte Tech

Well-known member
Having read through this again I think the problem is that the Notices page uses user criteria, not user searcher criteria.

I need user searcher criteria because these criteria are meant to be used to limit who receives a digest, it's not meant to match against the current visiting user like a notice.

This bug likely only manifests when using user searcher criteria.
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue. It has now been resolved and we are aiming to include it in a future XF release (2.1.3).

Change log:
Template error using 'exact' for user fields regardless of context
Any changes made as a result of this issue being resolved may not be rolled out here until later.
 
Top