限制symfony管理生成器编辑多选中显示的选项 [英] Restrict choices shown in symfony admin generator edit multi-select

查看:9
本文介绍了限制symfony管理生成器编辑多选中显示的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sfGuardUser模块的管理生成器。generator.yml文件的编辑部分如下:

edit:
  title: Editing User "%%username%%"
  display:
    "User":  [first_name, last_name, email_address, username, password, password_again]
    "Permissions and groups": [is_active, groups_list, sites_list]
现在,并非每个用户都有权访问此表单,只有站点管理员才能允许站点管理员创建和更新他们自己的用户。UserSite之间存在多对多关系。每个站点管理员也是用户,因此具有一组关联的站点。

我希望sites_list不显示所有站点,而只显示与站点管理员关联的站点,从而确保站点管理员无法将自己的用户之一放入与管理员不关联的站点。

在我看来,我需要将sites_list替换为其他内容来执行此操作,但我不知道在哪里以及如何进行此更改。

推荐答案

我认为解决此问题的唯一方法是从自动生成的表单中更改Sites_List小部件。例如,在您的情况下,您可以这样做:

<!-- SitesTable -->
public function getByUser($userId){
     //create your query to find all sites from that user
      $userSites = $this->createQuery()->...
                        ->where('user_id = ?', $userId);

      //create the array
      $choices = array();
      foreach ( $userSites as $site ) {
          $choices[$site->getId()] = $site->getName();
      }

      return $choices;
}

<!-- sfGuardUserForm -->
class sfGuardUserForm extends BaseSfGuardUserForm{
    public function configure() {
      //unset the old sites_list
      unset($this['sites_list']);

      //obtain the user id (depends on how it's implemented, i'm not using sfGuard)
      $userId = sfContext::getInstance()->getUser()->getId(); 

      $choices = Doctrine::getTable('Sites')->getByUser($userId);

      //set the new widget filtered
      $this->setWidget('sites_list', new sfWidgetFormChoice(array('choices' => $choices)));
      $this->setValidator('sites_list', new sfValidatorChoice(array('choices' => array_keys($choices))));

}

这篇关于限制symfony管理生成器编辑多选中显示的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆