【问题标题】:Redirect to NEW Action with prefilled values in EasyAdmin 3在 EasyAdmin 3 中使用预填充值重定向到新操作
【发布时间】:2021-05-09 14:39:02
【问题描述】:

我目前正在尝试将克隆操作添加到我的 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);
 }

【问题讨论】:

  • 您好,您找到解决方案了吗?我也有同样的问题
  • @eronn 不,我刚回到easyadmin 1

标签: php symfony easyadmin3


【解决方案1】:

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

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

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

【讨论】:

  • 这实际上在这种情况下不起作用。您在此处无权访问 $builder。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多