@@ -297,81 +297,6 @@ Please note: The `CakeValidationFactory` should be injected via constructor.
297297
298298** Read more:** < https://odan.github.io/2020/10/18/slim4-cakephp-validation.html >
299299
300- ### Symfony Validator
301-
302- ** Installation**
303-
304- ```
305- composer require symfony/validator
306- ```
307-
308- ** Usage**
309-
310- ``` php
311- <?php
312-
313- use Selective\Validation\Converter\SymfonyValidationConverter;
314- use Selective\Validation\Exception\ValidationException;
315- use Selective\Validation\Regex\ValidationRegex;
316- use Symfony\Component\Validator\Constraints as Assert;
317- use Symfony\Component\Validator\Validation;
318-
319- // Note: This is just an example. Don't use the $request object within the domain layer.
320- $formData = (array)$request->getParsedBody();
321-
322- // ...
323-
324- // Create a Symfony validator instance
325- $validator = Validation::createValidator();
326-
327- // Add rules
328- $constraint = new Assert\Collection(
329- [
330- 'first_name' => new Assert\NotBlank(['message' => 'Input required']),
331- 'last_name' => new Assert\NotBlank(['message' => 'Input required']),
332- 'mobile' => new Assert\Optional(
333- [
334- new Assert\Regex(
335- [
336- 'pattern' => ValidationRegex::PHONE_NUMBER,
337- 'message' => 'Invalid',
338- ]
339- ),
340- ]
341- ),
342- 'comment' => new Assert\Optional(
343- [
344- new Assert\Length(['max' => 255, 'maxMessage' => 'Too long']),
345- ]
346- ),
347- 'email' => new Assert\Optional(
348- [
349- new Assert\Email(
350- [
351- 'message' => 'Invalid',
352- ]
353- ),
354- ]
355- )
356- ]
357- );
358-
359- $constraint->missingFieldsMessage = 'Input required';
360- $violations = $validator->validate($formData, $constraint);
361-
362- // Convert symfony violations to ValidationResult
363- $validationResult = SymfonyValidationConverter::createValidationResult($violations);
364-
365- // Optional: Do more complex validation and append it to the validation result
366- if ($this->existsUsername($formData['username'])) {
367- $validationResult->addError('username', 'Username is already taken');
368- }
369-
370- if ($validationResult->fails()) {
371- throw new ValidationException('Please check your input', $validationResult);
372- }
373- ```
374-
375300## Transformer
376301
377302If you want to implement a custom response data structure,
0 commit comments