Symfony問題,我剛剛開始學習它。用戶上傳一個檔案,其中包含我想以陣列(鍵值)的形式提交到表單以進行驗證的資料。我呼叫該方法來構建表單,在那里傳遞一個陣列并嘗試檢查,例如,標題('title')的長度是否不超過 255 個字符。拋出錯誤“選項“約束”不存在”。
匯入服務.php
<?php
namespace App\Service;
use App\Entity\Import;
use App\Service\ServiceInterface;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Symfony\Component\Form\Forms;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Form\Form;
class ImportService implements ServiceInterface {
public function __construct(protected ManagerRegistry $doctrine)
{
}
public function parse(Import $import, array $scheme, array $formOptions = [])
{
$rowArray = [];
$reader = ReaderEntityFactory::createXLSXReader();
$reader->open($import->getPath());
foreach ($reader->getSheetIterator() as $index => $sheet) {
foreach ($sheet->getRowIterator() as $rowIndex => $row) {
if ($rowIndex == 1) {
continue;
} else {
$cells = $row->getCells();
foreach ($cells as $cell) {
$rowArray[] = $cell->getValue();
};
$row = array_combine($scheme, $rowArray);
$importRow = new ImportRow($import, $sheet, $rowIndex, $row);
$form = $this->buildForm($import->getFormType());
$form->submit($row);
if ($form->isValid()) {
$import->setSuccess('true');
$this->saveImport($import);
return $row;
}
}
};
}
}
protected function buildForm(string $formType, array $formOptions = []): Form
{
$formFactory = Forms::createFormFactory();
return $formFactory
->create(
$formType,
null,
);
}
NewsImportType.php
use App\Entity\News; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Url;
class NewsImportType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'constraints' =>
[new Length(['max' => 256])],
])
->add('text', TextareaType::class, [
'constraints' =>
[new Length(['max' => 1000])],
])
->add('image', TextType::class, [
'constraints' =>
[
new Length(['max' => 256]),
new Url()
],
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'allow_extra_fields' => true,
'data_class' => News::class,
]);
} }
錯誤文本
決議“Symfony\Component\Form\Extension\Core\Type\TextType”形式的選項時發生錯誤:選項“約束”不存在。定義的選項有:“action”、“allow_file_upload”、“attr”、“attr_translation_parameters”、“auto_initialize”、“block_name”、“block_prefix”、“by_reference”、“compound”、“data”、“data_class”、“disabled” ”、“empty_data”、“error_bubbling”、“form_attr”、“getter”、“help”、“help_attr”、“help_html”、“help_translation_parameters”、“inherit_data”、“invalid_message”、“invalid_message_parameters”、“is_empty_callback”、 “標簽”, ”
uj5u.com熱心網友回復:
如檔案中所述:
此選項添加在FormTypeValidatorExtension表單擴展中。
您可能忘記安裝Validator 組件:
composer require symfony/validator
uj5u.com熱心網友回復:
該constraints選項是核心表單擴展的一部分,ValidatorExtension而不是核心表單擴展的一部分。你可以像下面這樣使用它
$validator = Validation::createValidator();
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
還添加這個
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
詳情參考鏈接
uj5u.com熱心網友回復:
根據檔案,你必須有類似的東西:
->add('title', TextType::class, [
'constraints' =>
new Length(['max' => 256]),
])
->add('text', TextareaType::class, [
'constraints' =>
new Length(['max' => 1000]),
])
->add('image', TextType::class, [
'constraints' =>
[
new Length(['max' => 256]),
new Url()
],
]);
你把這個:
'constraints' =>
[new Length(['max' => 256])],
])
醫生這樣說:
'constraints' => new Length(['max' => 256]),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462123.html
上一篇:如何在PHP中識別要處理的表單
