You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laminas框架+Doctrine ORM联动下拉列表报错:The input was not found in the haystack

Laminas框架+Doctrine ORM联动下拉列表报错:The input was not found in the haystack

嘿,我看了你的代码和问题描述,这个报错其实是两个核心问题共同导致的,咱们一步一步来解决:

第一个问题:Laminas Select元素的自动验证坑

Laminas的Select元素默认会自动添加一个InArray验证器,它会检查你提交的community_id是否存在于表单初始化时的value_options列表里。但你的社区选项是通过AJAX动态加载的,表单初始化时getCommunityOptions()大概率返回的是空数组或者只有空选项,服务器端根本不知道你后来通过AJAX新增的选项,自然就抛出了"The input was not found in the haystack"错误——哪怕你自己写了Callback验证,这个自动验证还是会优先触发。

修复方法:在社区下拉的表单配置里,禁用这个自动验证:

// Add community selection
$this->add([
    'name' => 'community_id',
    'type' => Element\Select::class,
    'options' => [
        'label' => 'Community',
        'empty_option' => 'Select Community',
        'value_options' => $this->getCommunityOptions(),
        'label_attributes' => [
            'class' => 'required-field'
        ],
        // 加上这一行,禁用自动的InArray验证
        'disable_inarray_validator' => true
    ],
    'attributes' => [
        'id' => 'community_id',
        'class' => 'form-control form-select',
        'required' => true,
        'disabled' => true,
        'data-dependent' => 'district_id',
        'value' => ''
    ],
]);

第二个问题:UUID验证逻辑的判断错误

你提交的community_id是UUID字符串(比如4acc72cb-fc61-11ef-8c90-00155d016f00),但你的validateCommunity方法里用了is_numeric($value)来判断——UUID显然不是数字,所以这部分验证逻辑根本不会执行,直接跳到最后返回false,导致验证失败。

修复方法:去掉这个is_numeric判断,直接验证UUID对应的社区是否属于选中的区域:

public function validateCommunity($value)
{
    // Log the submitted value
    error_log("Submitted community_id: " . $value);

    // Get the district ID
    $districtId = $this->get('district_id')->getValue();
    error_log("Selected district_id: " . $districtId);

    if (empty($districtId)) {
        error_log("District ID is empty.");
        return false;
    }

    // Handle 'other' option
    if ($value === 'other') {
        $otherCommunity = $this->get('other_community')->getValue();
        if (empty($otherCommunity)) {
            error_log("Other community name is empty.");
            return false;
        }
        return true;
    }

    // 直接验证UUID对应的社区是否存在于选中的区域,去掉is_numeric判断
    try {
        $community = $this->entityManager->getRepository(\Grm\Entity\Community::class)
            ->findOneBy([
                'id' => $value,
                'district' => $districtId
            ]);

        if ($community === null) {
            error_log("Community not found in the selected district.");
        } else {
            error_log("Community found: " . $community->getName());
        }

        return $community !== null;
    } catch (\Exception $e) {
        error_log("Error validating community: " . $e->getMessage());
        return false;
    }
}

最后补充

做完这两个修改后,你可以再测试一下:

  1. 禁用自动验证后,"haystack"错误应该就消失了;
  2. 修复UUID判断后,你的Callback验证会正确检查社区是否属于选中的区域,同时保留"Other"选项的逻辑。

另外,确认一下getCommunityOptions()方法在表单初始化时返回的是空数组或者只有空选项就好,因为这时候用户还没选区域,不需要加载任何社区选项,AJAX会在用户选择区域后动态填充。

备注:内容来源于stack exchange,提问作者Kwasi Ameyaw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:37:59