2
我是symfony的新手,我目前正在嘗試理解Symfony3。 雖然通過註釋驗證工作正常,但我無法通過validation.yml文件激活驗證。validation.yml文件被忽略
在我的應用程序/配置/ config.yml,我改變了這一行:
framework:
validation: { enabled: true, enable_annotations: false }
我的src /的appbundle /資源/配置/ validation.yml是:
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\TaskSetClass:
properties:
taskName:
- NotBlank: ~
dueDate:
- NotBlank: ~
- Type: \DateTime
我加了一個擴展文件:
<?php
// src/AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Finder\Finder;
/* */
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container=new ContainerBuilder();
// Services
$loader=new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../../app/config'));
$loader->load('services.yml');
// Validation
// Commented : No "validator.mapping.loader.yaml_files_loader.mapping_files" parameter.
//$yamlMappingFiles=$container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');
$finder=new Finder();
foreach($finder->files()->in(__DIR__.'/../../../app/Resources/config') as $file)
{
$filePath=$file->getRealPath();
if(preg_match('@\[email protected]',$filePath)===1)
{
$yamlMappingFiles[]=$filePath;
}
}
$container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $yamlMappingFiles);
}
}
在appExtension.php所述的setParameter()之後的容器的傾倒表示命令已成功執行:
[parameterBag:protected] => Symfony\Component\DependencyInjection\ParameterBag\ParameterBag Object
(
[parameters:protected] => Array
(
[validator.mapping.loader.yaml_files_loader.mapping_files] => Array
(
[0] => /mnt/400Go/www/sy1/app/Resources/config/validation.yml
)
)
[resolved:protected] =>
)
在我的表單控制器,如果我列出的約束:
[...]
if($form->isSubmitted())
{
if($form->isValid())
{
$metadata=$this->container
->get('validator')
->getMetadataFor('AppBundle\Entity\TaskSetClass');
$propertiesMetadata=$metadata->properties;
$constraints=array();
foreach ($propertiesMetadata as $propertyMetadata)
{
$constraints[$propertyMetadata->name]=$property->constraints;
}
echo'<pre>Constraints : ';
print_r($constraints);
echo'</pre>';
die();
[...]
...數組爲空。所以文件似乎被加載,但約束被忽略。
有沒有人遇到此錯誤?它可能來自symfony 2和3之間的差異。
這很讓人傷心,因爲這是目前唯一需要放在捆綁包中的配置文件。幾乎不一致 –