excelwebzone / EWZRecaptchaBundle

This bundle provides easy reCAPTCHA form field for Symfony.
MIT License
396 stars 160 forks source link

Recaptcha is not loading in symfony 3.1 form #118

Open shahroznawaz opened 7 years ago

shahroznawaz commented 7 years ago

I have installed the bundle in symfony 3.1 and enabled it in AppKernel.php but it's not showing in a form instead it's giving an error

Neither the property "recaptcha" nor one of the methods "getRecaptcha()", "recaptcha()", "isRecaptcha()", "hasRecaptcha()", "__get()" exist and have public access in class "AppBundle\Entity\User"

Usertype.php

  namespace AppBundle\Form;

  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\EmailType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
  use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', EmailType::class)
            ->add('username', TextType::class)
            ->add('plainPassword', RepeatedType::class, array('type' => PasswordType::class,'first_options'               => array('label' => 'Password'),'second_options' => array('label' => 'Repeat Password'),))
            ->add('recaptcha', EWZRecaptchaType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\User',
        ));
    }
}

RegistrationController.php

namespace AppBundle\Controller;

use AppBundle\Form\UserType;
use AppBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class RegistrationController extends Controller
{
    /**
     * @Route("/register", name="user_registration")
     */
    public function registerAction(Request $request)
    {
        // 1) build the form
        $user = new User();
        $form = $this->createForm(UserType::class, $user);

        // 2) handle the submit (will only happen on POST)
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

            // 3) Encode the password (you could also do this via Doctrine listener)
            $password = $this->get('security.password_encoder')
                ->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);

            // 4) save the User!
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

            // ... do any other work - like sending them an email, etc
            // maybe set a "flash" success message for the user

            return $this->redirectToRoute('replace_with_some_route');
        }

        return $this->render(
            'default/register.html.twig',
            array('form' => $form->createView())
        );
    }

/**
 * @Recaptcha\IsTrue
 */
public $recaptcha;
}

what's the problem with it please guide.

mablae commented 7 years ago

Could you please fix the Markdown? Thanks in advance.

shahroznawaz commented 7 years ago

Hi @mablae I'm trying to update the markup but somehow i'm failed to do it. I have tried it step by step but the recaptcha is not loading in my form. I have added this line, "->add('recaptcha', EWZRecaptchaType::class);" after enabling recaptcha in AppKernel.php but it's not loading.

junowilderness commented 7 years ago

There are deprecations to fix #113. Actually, I am not seeing those form deprecation warnings on master, just Twig deprecations.

shahroznawaz commented 7 years ago

@cilefen i have used the form builder. where should i replace these new changes?

junowilderness commented 7 years ago

@shahroznawaz What version of twig is installed?

junowilderness commented 7 years ago

@shahroznawaz please can you fix the code display in your original message?

shahroznawaz commented 7 years ago

@cilefen I have updated the code block. i havn't install twig. is this generating an error?

junowilderness commented 7 years ago

@shahroznawaz Ignore what I wrote about twig. Version 1.26.0 introduces deprecation alerts. I do not think they are relevant.

This is wrong:

/* * @Recaptcha\IsTrue / public $recaptcha; }

It is not totally clear in the README, but that property and annotation belongs on your User entity, not the controller class.

shahroznawaz commented 7 years ago

@cilefen that means i should write this code in User entity which i have created right?

junowilderness commented 7 years ago

Yes.

Or, set 'mapped' => false on the form builder element and remove the property.

shahroznawaz commented 7 years ago

Thanx @cilefen, it works. but old recaptcha is added to the form. image

Any ways to update it to the new one?

junowilderness commented 7 years ago

I get the new one. I am not familiar with this codebase. What version of this bundle are you using?

shahroznawaz commented 7 years ago

@cilefen I am using version 1.4 of this bundle

ghost commented 7 years ago

@shahroznawaz I had the same pbm. The old captcha was showing. I found that it was the case if i activated the ajax version in the config file. I just deleted the "ajax: true", empty the cache, and the new one show up.

shahroznawaz commented 7 years ago

Thanks @StefanKali, It worked for me too :)

perk11 commented 5 years ago

This is still happening for me. The documentation doesn't mention the need for mapped => false

Nevermind, it's right there in Readme: image There are no reference to the fact that the field needs to be added before that though, which is what got me.