Answer the question
In order to leave comments, you need to log in
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
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)))
))
And what is not so validated? In general, all validations in the default FormType like
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 questionAsk a Question
731 491 924 answers to any question