【问题标题】:how do i set a value for a hidden variable in symfony1.4如何在 symfony1.4 中为隐藏变量设置值
【发布时间】:2012-03-08 06:25:24
【问题描述】:

我需要将 login_id 存储到隐藏变量中,以便将其存储在数据库中。我如何存储它?登录后我像这样存储 login_id $this->getUser()->setAttribute('client_id',$client_id); 我在我的基类中设置了小部件

 $this->setWidgets(array(
     'id'      => new sfWidgetFormInputHidden(),
     'client_id'              => new sfWidgetFormInputHidden(array(),array('value'=>'3')),));

如何在我的基类中访问客户端的 login_id,或者有没有其他方法可以将 login_id 设置为隐藏变量?请帮我。提前致谢

【问题讨论】:

    标签: symfony1 symfony-1.4


    【解决方案1】:

    在您创建表单时采取的行动:

    $form = new MyForm();
    $form->setDefault('client_id', $this->getUser()->getAttribute('client_id'));
    

    【讨论】:

      【解决方案2】:

      你可以像 luliandro 说的那样做,或者你可以在表单中做

          $this->setWidgets(array(
               'id'      => new sfWidgetFormInputHidden(),
               'client_id'              => new sfWidgetFormInputHidden()
           )); 
          $this->getWidget('client_id')->setDefault(3);
      

      【讨论】:

      • 这实际上是比设置它更好的解决方案;特别是如果您在许多不同的操作中呈现相同的表单。
      【解决方案3】:

      在您的库/表单中,您可以这样做:

        $this->setWidgets(array(
              'example' => new sfWidgetFormInputHidden(array(), array()),
          ));
      

      祝你有美好的一天!

      古纳尔

      【讨论】:

        【解决方案4】:

        利米·杰林 我也有问题。 这是我的解决方案: 你可以试试:

          $this->setWidgets(array(
         'id'      => new sfWidgetFormInputHidden(),
         'client_id'              => new sfWidgetFormInputHidden());
        

        sfWidgetFormInputHidden 不支持默认值。但是我们可以使用 ValidatorChoice 设置值:

        $this->setValidators(array(
                'client_id' => new ValidatorChoice([
                    'choices' => [3],
                    'empty_value' => 3,
                    'required' => FALSE,
            ])));
        

        我有一个项目示例:

            $this->widgetSchema['portal_id'] = new sfWidgetFormInputHidden([
                'default' => [sfContext::getInstance()->getUser()->getAttribute('portal')],
                'is_hidden' => true,
            ]);
            $this->validatorSchema['portal_id'] = new sfValidatorChoice([
                'choices' => [sfContext::getInstance()->getUser()->getAttribute('portal')],
                'empty_value' => sfContext::getInstance()->getUser()->getAttribute('portal'),
                'required' => FALSE,
            ]);
        

        感谢阅读。

        【讨论】:

          猜你喜欢
          • 2011-09-25
          • 2018-09-11
          • 1970-01-01
          • 2019-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-09
          相关资源
          最近更新 更多