Я хочу получить от элементов ввода сервера их требования проверки, которые я храню в файле validation.yaml.
о, и, как видно из тегов, я делаю это с Symfony 4.
Когда пользователь захочет загрузить новый пост, у него будет представление поста по умолчанию, но с элементами ввода - это то, чего я хочу добиться.
На стороне сервера: У меня есть 2 идеи, ни одну из них я не знаю, что реализовать
Получите проверку и создайте элементы как-нибудь:
/**
* Controller function
*/
public function getPostFields(): JsonResponse
{
$topicRequirements = getThemFromSomewhere('topic');
$categoryRequirements = getThemFromSomewhere('category');
# How do I get those?
$topicHTMLInput = buildItSomehow('input', $topicRequirements);
$categoryHTMLSelection = buildItSomehow('selection', $categoryRequirements);
# Or build those??
или создайте его через конструктор форм:
/**
* Builder function
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', EntityType::class, [
'class' => Category::class
])
->add('topic', TextType::class);
}
и сделать это так:
/**
* Controller function
*/
public function getPostFields(): JsonResponse
{
$post = new Post();
$form = $this->createForm(Builder::class, $post);
$HTMLInput = $form->renderHTMLInput();
$topicHTMLInput = $HTMLInput['topic'];
$categoryHTMLSelection = $HTMLInput['category'];
Клиент:
var post = {
topic: someHTMLElement,
category: someOtherHTMLElement,
insert: function(data) {
for (let key in this)
if (this[key] instanceof Element && data.hasOwnProperty(key))
this[key].innerHTML = data[key];
}
}
response = someXMLHttpRequestResponse;
post.insert(response.data);
Я хочу, чтобы response.data, который я передаю post.insert, соответствовал требованиям проверки с сервера, как:
{topic: '<input attr>', category: '<input attr>'}
так что на стороне сервера я ожидаю
return new JsonResponse(['data' => [
'topic': $topicHTMLInput,
'category': $categoryHTMLSelection
]]);
}
Рад получить помощь ;)



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Я пошел с конструктором и оказалось, что вы можете отображать в Twig form_widget без формы, что я и сделал. Это не самый оптимизированный ответ, но он работает так, как я хотел:
/**
* Controller function
*/
public function getPostFields(): JsonResponse
{
$post = new Post();
$form = $this->createForm(PostFormBuilder::class, $post);
$form = $form->createView();
return new JsonResponse([
'data' => [
'category' => $this->renderView('elements/form/category.twig', ['post' => $form]),
'topic' => $this->renderView('elements/form/topic.twig', ['post' => $form]),
]
]);
}
/**
* templates/elements/form/category.twig
*/
{{ form_widget(post.category) }}
/**
* templates/elements/form/topic.twig
*/
{{ form_widget(post.topic) }}