dongxixiu9134 2016-12-12 10:05
浏览 40
已采纳

twig / symfony中既没有属性“password”也没有其中一种方法

I want to make reuse of the form field by passing show_password_field as true in the optional value in AddUserType.php. My edit form field should not contain password field. So I have used this

if ($options['show_password_field']) {
      $builder->add('password','password',array('required'=>true));
    }

in form type. Can anybody figure it out? What is the problem in line 37 of twig file.

Neither the property "password" nor one of the methods "password()", "getpassword()"/"ispassword()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in SokosimuEditorBundle:User:addUser.html.twig at line 37.

AddUserType.php

   namespace Sokosimu\EditorBundle\Form\Type;

   use Symfony\Component\Form\AbstractType;
   use Symfony\Component\Form\FormBuilderInterface;
   use Symfony\Component\Form\Extension\Core\Type\SubmitType;
   use Symfony\Component\OptionsResolver\OptionsResolverInterface;

  class AddUserType extends AbstractType
  {

 public function buildForm(FormBuilderInterface $builder, array $options)
 {
    $builder->add('alias','text',array('required'=>false));
    $builder->add('email', 'email',array('required'=>false));

    if ($options['show_password_field']) {
        $builder->add('password','password',array('required'=>true));
    }

    $builder->add('mobile','text',array('required'=>false));
    $builder->add('submit', 'submit');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Sokosimu\UserBundle\Entity\User',
        'show_password_field' => true
    ));
}

/**
 * Returns the name of this type.
 *
 * @return string The name of this type
 */
 public function getName()
 {
     return 'adduser';
  }
}

And line 37 in addUser.html.twig

<div class="form-group">
   <label for="password">Password:</label>
   {{ form_widget(form.password,{'attr':{'class':'form-row'}}) }}
   {{ form_errors(form.password) }}
  </div>

Controller that renders edit

public function editEditorUserAction(User $user,Request $request){

    $form = $this->createForm(new AddUserType(),$user,array(
        'show_password_field' => false));

    $form->handleRequest($request);
    if($form ->isValid() && $form->isSubmitted()){
        $em = $this->get('doctrine')->getManager();
        $editUser = $user ->getEditoruser();

        $em->persist($editUser);
        $em->flush();

        return $this->redirect($this->generateUrl('sokosimu_editor_userlist'));
    }

    return $this->render('SokosimuEditorBundle:User:addUser.html.twig', array(
        'form' => $form->createView(),
         'user' => $user->getId()
    ));
  }
  • 写回答

1条回答 默认 最新

  • douyanpeng0748 2016-12-12 10:09
    关注

    You must check if form.password exists.

    {% if form.password is defined %}
        <div class="form-group">
            <label for="password">Password:</label>
            {{ form_widget(form.password,{'attr':{'class':'form-row'}}) }}
            {{ form_errors(form.password) }}
        </div> 
    {% endif %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个批处理,在多个文件夹里面创建一个名称“0001”的文件夹,并将文件移动到刚才新建的文件里面
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 (求远程解决)深信服vpn-2050这台设备如何配置才能成功联网?