Abhoryo / APYJsFormValidationBundle

This bundle performs validations of a form in javascript. (i18n compatible and several javascript frameworks supported)
92 stars 23 forks source link

Hi #52

Closed marvoh closed 10 years ago

marvoh commented 10 years ago

This is not an issue but request for direction, I looked everywhere for a link to anywhere I could get help without any success.

I'm using FOSUser bundle and have extended the registration controller and also created a custom profile form and using APYJsFormValidationBundle because I need translated validation messages.

Following the example here to the letter, https://github.com/Abhoryo/APYJsFormValidationBundle/blob/master/Resources/doc/simpleform_example.md

I get an error Test\MyBundle\Controller\Collection' not found in my Controller file the offending line being $collectionConstraint = new Collection(array());

Any help will be appreciated

filips-alpe commented 10 years ago

you might try adding 'use Doctrine\Common\Collections\Collection;' at the top of your file :)

marvoh commented 10 years ago

Thanks for quick response, tried that and now get this error

FatalErrorException: Error: Cannot instantiate interface Doctrine\Common\Collections\Collection

Abhoryo commented 10 years ago

use Symfony\Component\Validator\Constraints\Collection;

marvoh commented 10 years ago

That seems to have worked, now one final one: FatalErrorException: Error: Class 'Test\MyBundle\Controller\NotBlank' not found

Thanks for your patience with this noob

marvoh commented 10 years ago

Got it use Symfony\Component\Validator\Constraints\NotBlank;

marvoh commented 10 years ago

Ok, not entirely out of the woods. Here's my form code, more or less copied from example

$defaultData = array(
        'nationality' => '',
        'city' => '',
        'country' => '',
        'tel' => '',
        'organization'    => '',
        'medium'    => '',
        'position'    => '',
        'orgemail'    => '',
        'languages'    => '',
        'topics'    => '',
    );
    $collectionConstraint = new Collection(array(
        'nationality' => new NotBlank(),
        'city' => new NotBlank(),
        'country' => new NotBlank(),
        'tel' => new NotBlank(),
        'organization'    => new NotBlank(),
        'medium'    => new NotBlank(),
        'position'    => new NotBlank(),
        'orgemail'    => new NotBlank(),
        'languages'    => new NotBlank(),
        'topics'    => new NotBlank(),
    ));
    $form = $this->container->get('form.factory')->createBuilder($defaultData,
            array('validation_constraint' => $collectionConstraint,)
        )
    ->add('nationality', 'choice', array('choices'=>$nationality,'label'=>'Nationality','attr'=>array('data-live-search'=>'true')))
    ->add('gender', 'choice', array('choices'   => array('Male'=>' Male ', 'Female'=>' Female '),'attr'=>array('class'=>'radio'),'expanded'=>true,'label'=>'Gender'))
    ->add('city', 'text',array('label'=>'City'))
    ->add('country', 'choice', array('choices'=>$country,'label'=>'Country','attr'=>array('data-live-search'=>'true')))
    ->add('tel', 'text', array('label'=>'Telephone'))
    ->add('mobile', 'text', array('label'=>'Mobile'))
    ->add('alternative_email', 'text', array('label'=>'Alternative email'))
    ->add('twitter_handle', 'text', array('label'=>'Twitter handle'))
    ->add('organization', 'text', array('label'=>'Media house'))
    ->add('medium', 'choice', array('choices'=>$media,'label'=>'Type of medium'))
    ->add('position', 'text', array('label'=>'Title'))
    ->add('orgemail', 'text', array('label'=>'Newsroom email'))
    ->add('languages', 'choice', array('label'=>'Working languages','choices'=>$languages,'attr'=>array('data-live-search'=>'true'),'multiple'=>true))
    ->add('topics', 'text', array('label'=>'Topics of interest'))
    ->getForm()->createView();

I get this error Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "array" given

issue seems to be around here

  $form = $this->container->get('form.factory')->createBuilder($defaultData,
            array('validation_constraint' => $collectionConstraint,)
        )
    ->add('nationality', 'choice', array('choices'=>$nationality,'label'=>'Nationality','attr'=>array('data-live-search'=>'true')))
    ->add('gender', 'choice', array('choices'   => array('Male'=>' Male ', 'Female'=>' Female '),'attr'=>array('class'=>'radio'),'expanded'=>true,'label'=>'Gender'))
Abhoryo commented 10 years ago

You forget one parameter:

$this->container->get('form.factory')->createBuilder('form', $defaultData,
            array('validation_constraint' => $collectionConstraint,)
marvoh commented 10 years ago

Sorry for that, I took it directly from the sample code

marvoh commented 10 years ago

This is driving me nuts now, thank you for your patience but now there's a new error: The option "validation_constraint" does not exist.

Hope you're not running out of patience with me, you're bundle will be very useful to me once i get it running

Abhoryo commented 10 years ago

Check your symfony version, this option isn't available on old symfony version.

marvoh commented 10 years ago

My symfony is version 2.3.7. What is the minimum required version?

Abhoryo commented 10 years ago

I don't know but you can simplify your code.

$form = $builder
    ->add('email', 'email', array(
        'label' => 'Email',
        'constraints' => array(
            new Assert\NotBlank(),
            new Assert\Email(),
        ),
    ))
    ->add('password', 'password', array(
        'label' => 'Password',
        'constraints' => new Assert\NotBlank(),
    ))
    ->getForm()
;
marvoh commented 10 years ago

Let me try that.