$('input[type=radio]').on("change", function() {
$('input[type=radio]:checked').not(this).prop('checked', false);
});
var $collectionHolder;
var $addNewAnswer = $('<a href="#" class="btn btn-info">Add new answer</a>')
$(document).ready(function () {
$collectionHolder = $('#answer_list')
$collectionHolder.append($addNewAnswer);
$collectionHolder.data('index', $collectionHolder.find('#answer').length)
$collectionHolder.find('#answer').each(function () {
addRemoveButton($(this));
})
$addNewAnswer.click(function (e) {
e.preventDefault();
addNewForm();
})
$('input[type=radio]').change(function() {
$('input[type=radio]:checked').not(this).prop('checked', false);
});
});
function addRemoveButton ($answer) {
var $removeButton = $('<a href="#" class="btn btn-danger" style="margin-bottom: 40px">Remove</a>')
$removeButton.click(function (e) {
$(e.target).parents('#answer').slideUp(500, function () {
$(this).remove()
})
});
$answer.append($removeButton);
}
function addNewForm() {
var prototype = $collectionHolder.data('prototype');
var index = $collectionHolder.data('index');
var newFrom = prototype;
newFrom = newFrom.replace(/__name__/g, index);
$collectionHolder.data('index', index+1);
var $newAnswer = $('<div id="answer" ></div>');
$newAnswer.append(newFrom);
addRemoveButton($newAnswer);
$addNewAnswer.before($newAnswer);
}
<?php
namespace App\Entity;
use App\Repository\AnswerRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=AnswerRepository::class)
*/
class Answer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $is_true;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $text;
/**
* @ORM\ManyToOne(targetEntity=Question::class, inversedBy="answers")
*/
private $question;
public function getId(): ?int
{
return $this->id;
}
public function getIsTrue(): ?bool
{
return $this->is_true;
}
public function setIsTrue(?bool $is_true): self
{
$this->is_true = $is_true;
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
public function getQuestion(): ?Question
{
return $this->question;
}
public function setQuestion(?Question $question): self
{
$this->question = $question;
return $this;
}
}
<?php
namespace App\Entity;
use App\Repository\QuestionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=QuestionRepository::class)
*/
class Question
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $text;
/**
* @ORM\OneToMany(targetEntity=Answer::class, mappedBy="question", cascade={"persist"})
*/
private $answers;
public function __construct()
{
$this->answers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
/**
* @return Collection|Answer[]
*/
public function getAnswers(): Collection
{
return $this->answers;
}
public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setQuestion($this);
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
// set the owning side to null (unless already changed)
if ($answer->getQuestion() === $this) {
$answer->setQuestion(null);
}
}
return $this;
}
}
var $collectionHolder;
var $addNewAnswer = $('<a href="#" class="btn btn-info">Add new answer</a>')
$(document).ready(function () {
$collectionHolder = $('#answer_list')
$collectionHolder.append($addNewAnswer);
$collectionHolder.data('index', $collectionHolder.find('#answer').length)
$collectionHolder.find('#answer').each(function () {
addRemoveButton($(this));
})
$addNewAnswer.click(function (e) {
e.preventDefault();
addNewForm();
})
});
function addRemoveButton ($answer) {
var $removeButton = $('<a href="#" class="btn btn-danger">Remove</a>')
$removeButton.click(function (e) {
$(e.target).parents('#answer').slideUp(500, function () {
$(this).remove()
})
});
$answer.append($removeButton);
}
function addNewForm() {
var prototype = $collectionHolder.data('prototype');
var index = $collectionHolder.data('index');
var newFrom = prototype;
newFrom = newFrom.replace(/__name__/g, index);
$collectionHolder.data('index', index+1);
var $newAnswer = $('<div id="answer"></div>');
$newAnswer.append(newFrom);
addRemoveButton($newAnswer);
$addNewAnswer.before($newAnswer);
}
{% block body %}
{{ form_start(form) }}
{{ form_row(form.text) }}
<div class="col-sm-8 col-sm-offset-2" id="answer_list" data-prototype="{{ form_widget(form.answers.vars.prototype)|e('html_attr') }}">
{% for row in form.answers %}
<div id="answer">
{{ form_row(row) }}
</div>
{% endfor %}
</div>
{{ form_end(form) }}
{% endblock %}
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=******;dbname=******',
'username' => '*******',
'password' => '******',
'charset' => 'utf8',
];