When I submitted my form I have this error:
An exception occurred while executing 'INSERT INTO skills (name) VALUES (?)' with params [null]:
I use modal i load page x.html.twig in modal ..page x.html.twig content name(SkillType) and level(select option).
I have this form Type:
<?php
namespace AppBundle\Form\Type;
use AppBundle\Entity\CandidateSkill;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use AppBundle\Form\Type\SkillType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
/**
* Class CandidateSkillType.
*/
class CandidateSkillType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('level', ChoiceType::class, array('choices' => array('Beginner' => 'Beginner', 'Confirmed' => 'Confirmed', 'Senior' => 'Senior', 'Expert' => 'Expert')))
->add('skill', SkillType::class)->add('Create', SubmitType::class, array('attr' => array('class'=>'skill btn btn-fill btn-rose')));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => CandidateSkill::class,
'csrf_protection' => 'false',
'allow_extra_fields' => 'false',
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_candidateskill';
}
}
and SkillType is:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
/**
* SkillType.
*/
class SkillType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',null, array('attr' => array('class' => 'form-control','data-id'=>"test")));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Skill',
'csrf_protection' => 'false',
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_skill';
}
}
code twig:
<style>
#ui-id-1 {
width:150px;
height:50px;
top:169px;
left:250px;
z-index: 9999999999999999999999999999999 !important;
}
.ui-menu .ui-menu-item a {
font-size: 12px;
}
.ui-menu {
z-index: 9999999999999999999999999999999 !important;
}
.ui-font {
z-index: 9999999999999999999999999999999 !important;
}
</style>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header card-header-icon" data-background-color="rose">
<i class="material-icons">title</i>
</div>
<div class="card-content">
<h4 class="card-title">Add Skill</h4>
{{ form_start(form, { 'attr' : { 'class': 'theFormskill' } }) }}
{{ form_end(form) }}
</div>
</div>
</div>
</div>
<script>
$( function() {
var availableTags= [];
{% for skill in all%}
availableTags.push('{{skill.skill.name}}');
{% endfor%}
console.log(availableTags);
$( "#appbundle_candidateskill_skill_name" ).autocomplete({
source: availableTags
});
} );
$( ".skill" ).click(function() {
$(".theFormskill").ajaxForm({url: "{{path('recurit_canddiate_skill_new')}}", type: 'post'});
});
</script>
code controller
/**
* Creates new CandidateSkill Object.
*
* @Template()
* @Route("/add-candidateskill", name="recurit_canddiate_skill_new")
*
* @param Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function newAction(Request $request)
{
$candiateSkill = $this->getHandler()->post($request->request->all(), $request->isMethod(Request::METHOD_POST));
if ($candiateSkill instanceof CandidateSkill) {
return $this->redirectToRoute('fos_user_profile_show');
}
return ['form' => $candiateSkill->createView(),'all'=>$this->getHandler()->all()];
}
function handle for create form and getData:
/**
* {@inheritdoc}
*/
public function handle($object, array $parameters, string $method, bool $submited = false, array $options = [])
{
$options = array_replace_recursive([
'method' => $method,
'csrf_protection' => false,
], $options);
$form = $this->formFactory->create(get_class($this->formType), $object, $options);
if (!$submited) {
return $form;
}
$form->submit($parameters, 'PATCH' !== $method);
return $form->getData();
}
when i dump for $form->getData() ..data is null
function save :
/**
* @param mixed $object
* @param array $arguments
*/
public function save($object, array $arguments = ['flush' => true])
{
$this->em->persist($object);
if (true === $arguments['flush']) {
$this->em->flush();
}
}
when i ump in function post() in CandidateSkillHandler .. error has in
$this->repository->save($candidateSkill);
We have found with devit2017 what this form didn't work because the request wasn't handled.
These lines
$request = $this->requestStack->getCurrentRequest();
$form->handleRequest($request);
instead of
$form->submit($parameters, 'PATCH' !== $method);
solve this problem.