Saugat Bhattarai
Saugat Bhattarai

Reputation: 2750

Neither the property "password" nor one of the methods exists in twig/symfony

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()
    ));
  }

Upvotes: 1

Views: 7210

Answers (1)

panche14
panche14

Reputation: 661

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 %}

Upvotes: 4

Related Questions