微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 EasyAdmin 3 中的预填充值重定向到 NEW Action

如何解决使用 EasyAdmin 3 中的预填充值重定向到 NEW Action

我目前正在尝试向我的 EmployeeCrudController 添加克隆操作。
该操作应重定向到 Action::NEW 视图并具有一些预填充值。
但是我不知道如何预填这个表格。

这里是我如何在 EmployeeCrudController 中定义我的操作:

  public function configureActions(Actions $actions): Actions
  {
    $cloneAction = Action::new('Clone','')
        ->setIcon('fas fa-clone')
        ->linkToCrudAction('cloneAction');

    return $actions->add(Crud::PAGE_INDEX,$cloneAction);
  }

这就是我的 cloneAction 的样子,它目前按预期重定向到 Action::NEW,但没有预填充值:

 public function cloneAction(AdminContext  $context): RedirectResponse
 {
    $id     = $context->getRequest()->query->get('entityId');
    $entity = $this->getDoctrine()->getRepository(Employee::class)->find($id);

    $clone = new Employee();
    $entity->copyProperties($clone);
    $clone->setFirstname('');
    $clone->setLastname('');
    $clone->setEmail('');

    $routeBuilder = $this->get(CrudUrlGenerator::class);
    $url = $routeBuilder->build([
            'Employee_lastname' => 'test','Employee[teamMembershipts][]' => $clone->getTeamMemberships(),])
        ->setController(EmployeeCrudController::class)
        ->setAction(Action::NEW)
        ->generateUrl()
    ;

    return $this->redirect($url);
 }

解决方法

您可以使用选项 data 在 easyAdmin 中设置字段的值。

$builder->add('Employee_lastname',null,['data' => $clone->getTeamMemberships()]);

如果您的字段有多个选项,您可以使用 choiceschoices_value

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。