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 %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作