P
P
Peter2013-11-15 14:52:02
PHP
Peter, 2013-11-15 14:52:02

Silex: where are the isValid() rules for the form set?

I write on Silex the loading of the form.
Locally - everything worked.
After installation on the server, it stopped writing to the database.
Through trial and error, I found out that the problem is:
$form->isValid()
Attention, the question is - where are the validity rules set? In the Silex documentation, a separate chapter "ValidatorServiceProvider" is devoted to this, but I didn’t get it :(
I can assume that validation does not work due to the presence of empty values ​​in the form. Although, when creating the form, all potentially empty fields received the corresponding parameter required=>false

$form = $app['form.factory']->createBuilder('form', $data)
        ->add('foo', 'hidden', array('required' => false))
        ->getForm();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Peter, 2013-11-15
@Alcospb

In form rendering it was necessary to add
{{ form_rest(form) }}

V
Vladimir Chernyshev, 2013-11-15
@VolCh

Somehow, the validation rules are created:

$form = $app['form.factory']->createBuilder('form')
    ->add('name', 'text', array(
        'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5)))
    ))

N
Nikita Gusakov, 2013-11-15
@hell0w0rd

And what is not so validated? In general, all validations in the default FormType like

P
Peter, 2013-11-15
@Alcospb

Here is the stripped down code. Can you see something?

$app->match('/add/{flat_id}', function (Request $request, $flat_id) use ($app) {

    $form = $app['form.factory']->createBuilder('form', $data)
        ->add('attachment', 'file')
        ->add('type', 'choice', array(
            'choices' => array(
                '1' => 'Студия',
                '2' => 'Однушка',
                '3' => 'Евродвушка',
                '4' => 'Двушка',
                '5' => 'Трешка',
                '6' => 'Четвирешка :)'),
            'expanded' => false
            ))
        ->add('square_total')
        ...
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();

                $app['db']->insert('vn_db_flats', array(
            	'attachment' => rand(1,999) . $data['attachment']->getClientOriginalName(),
                'type' => $data['type'],
                'square_total' => $data['square_total'],
                 ...
            ));

            $data['attachment']->move(__DIR__ . '/attach/', $data['attachment']->getClientOriginalName());

            return $app->redirect($app['url_generator']->generate('index'));
        }
    }

    return $app['twig']->render('add.twig', array('form' => $form->createView()));

})
->bind('add')
->value('flat_id', '0');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question