सबसे पहले, आपको यह जानना होगा कि सिम्फनी 2 वैधकर्ता आसानी से ऐसा करने के लिए तैयार नहीं हैं। मुझे कुछ समय और कुछ सिम्फनी 2 स्रोत पढ़ने के लिए आपके मामले के लिए एक समाधान समाधान प्राप्त हुआ, और मेरा समाधान प्राकृतिक नहीं है।
मैंने एक कक्षा बनाई है जो वैधकर्ता, आपकी सरणी और आपकी यम कॉन्फ़िगरेशन फ़ाइल लेती है ताकि आप जो भी उम्मीद कर सकें वह कर सकें। यह वर्ग YamlFileLoader
को सिम्फनी से सुरक्षित parseNodes
विधि तक पहुंचने के लिए विस्तारित करता है: यह सुंदर नहीं है, लेकिन यह एकमात्र तरीका है जिसे मैंने कस्टम Yaml कॉन्फ़िगरेशन फ़ाइल को Constraint
ऑब्जेक्ट की सरणी में बदलने के लिए पाया है।
तो हम यहां हैं। मैं आपको अपना कोड देता हूं, आपको अपने नाम के अनुसार कुछ नामस्थानों को प्रतिस्थापित करने की आवश्यकता होगी।
पहले, हमारे डेमो के लिए एक नियंत्रक बनाने के लिए:
public function indexAction()
{
// We create a sample validation file for the demo
$demo = <<< EOT
name:
- NotBlank: ~
- MinLength: { limit: 3 }
- MaxLength: { limit: 10 }
date:
- NotBlank: ~
- Regex: "/^[0-9]{4}\-[0-9]{2}$/"
price:
- Min: 0
EOT;
file_put_contents("/tmp/test.yml", $demo);
// We create your array to validate
$product = array();
$product['name'] = 'A book';
$product['date'] = '2012-09';
$product['price'] = '21.5';
$validator = $this->get('validator');
$service = new \Fuz\TestsBundle\Services\ArrayValidator($validator, $product, "/tmp/test.yml");
$errors = $service->validate();
echo '<pre>';
var_dump($errors);
die();
return $this->render('FuzTestsBundle:Default:index.html.twig');
}
फिर एक वर्ग ArrayValidator.php नामित पैदा करते हैं। फिर, नामस्थान का ख्याल रखना।
<?php
namespace Fuz\TestsBundle\Services;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
/**
* This class inherits from YamlFileLoader because we need to call the
* parseNodes() protected method.
*/
class ArrayValidator extends YamlFileLoader
{
/* the @validator service */
private $validator;
/* The array to check */
private $array;
/* The file that contains your validation rules */
private $validationFile;
public function __construct(ValidatorInterface $validator, array $array = array(), $validationFile)
{
$this->validator = $validator;
$this->array = $array;
$this->validationFile = $validationFile;
}
/* The method that does what you want */
public function validate()
{
$yaml = file_get_contents($this->validationFile);
// We parse the yaml validation file
$parser = new Parser();
$parsedYaml = $parser->parse($yaml);
// We transform this validation array to a Constraint array
$arrayConstraints = $this->parseNodes($parsedYaml);
// For each elements of the array, we execute the validation
$errors = array();
foreach ($this->array as $key => $value)
{
$errors[$key] = array();
// If the array key (eg: price) has validation rules, we check the value
if (isset($arrayConstraints[$key]))
{
foreach ($arrayConstraints[$key] as $constraint)
{
// If there is constraint violations, we list messages
$violationList = $this->validator->validateValue($value, $constraint);
if (count($violationList) > 0)
{
foreach ($violationList as $violation)
{
$errors[$key][] = $violation->getMessage();
}
}
}
}
}
return $errors;
}
}
अंत में, इसे अपने $ उत्पाद सरणी में विभिन्न मानों के साथ परीक्षण करें।
डिफ़ॉल्ट रूप से:
$product = array();
$product['name'] = 'A book';
$product['date'] = '2012-09';
$product['price'] = '21.5';
विल प्रदर्शन:
array(3) {
["name"]=>
array(0) {
}
["date"]=>
array(0) {
}
["price"]=>
array(0) {
}
}
हम करने के लिए मूल्यों को बदलते हैं:
$product = array();
$product['name'] = 'A very interesting book';
$product['date'] = '2012-09-03';
$product['price'] = '-21.5';
आप मिल जाएगा:
array(3) {
["name"]=>
array(1) {
[0]=>
string(61) "This value is too long. It should have 10 characters or less."
}
["date"]=>
array(1) {
[0]=>
string(24) "This value is not valid."
}
["price"]=>
array(1) {
[0]=>
string(31) "This value should be 0 or more."
}
}
उम्मीद है कि यह मदद करता है।
यह किसी इकाई ऑब्जेक्ट को सत्यापित करने के लिए है, जो मैं चाहता हूं –