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条回答 默认 最新

相关推荐 更多相似问题