Validation Request $request?

Need to validate different methods for different, and do not specify in the annotations, they already overmanage.

To do something in the style of Laravel
https://laravel.com/docs/5.5/validation#creating-f...
Will have to add a bunch of rules, because apparently Symfony don't even care how it validation and authorization how it works, let others mold everything yourself from scratch, or bundles and packages.

That is, to accept not Request and already checked DoSmthRequest.
public function registerNewUser(CreqteNewUserRequest $request)


Without any additional calls inside the method to data validation. The type of the array, and we're making new NotNull (), and a dozen rules
'some_field_goes_here' => new Rulle(), new Rulle(), new Rulle(), new Rulle(), new Rulle(), new Rulle(), new Rulle(), new Rulle(), new Rulle(), new Rulle(),
neoch looks, right?
But if you have custom add...
June 10th 19 at 16:22
2 answers
June 10th 19 at 16:24
Solution
Request is a wrapper of the HTTP request. The HTTP request may not be valid if it is made incorrectly: incorrect header value, etc., it has nothing to do with the business logic (not the correct email in the form, etc.). In my opinion, what has been done in Laravel (if it's true, let me emphasize that I don't know it!) madness.

I recommend to look for the validation group, perhaps that's what you need: How to Apply only a Subset of all Your Validation ....
Works there from the Request, the same package that Symphony. Only, when passing him, he falls into the custom validator passes the test and moves on to the method. So everything is OK, request comes the same, just tested. - jaycee commented on June 10th 19 at 16:27
That query does not change, I understood it. I mean, in my opinion, we should not mix HTTP and business logic. What is done in larval - convenient, but not true. - Darien46 commented on June 10th 19 at 16:30
It is comfortable and correctly and to have the entire container in any console command. - Justice.Nienow commented on June 10th 19 at 16:33
I have a hair on his head moved when I saw the line: interface Application extends Container.... :)

The book was PHP and MySQL. Creating an online store. There is the same, only crazier. There is the application class is inherited from the Smarty template engine (such as whether the render templates, etc.). - Darien46 commented on June 10th 19 at 16:36
Yes, there is in Lara has his moments) I sometimes get the instance of the current project, at any point where I need it. There are more helpers there are the facades that give the wrapper to control what you said. Example app()->request->get('name_field') and we get the value from the GET request to view blade. Or Beck, $this->app->request()...

Please tell me the easy validation example, if let's say I receive 20 fields on the form and each field 5-8 of the rules. I would like to see such a sight from the controller, and of course from the Entity, in some validator or something. On git hub I have not found examples of what I mean. - jaycee commented on June 10th 19 at 16:39
Try to make the validation rules in yaml files and use the validation group. Here is an example, but with xml: https://github.com/Sylius/Sylius/blob/master/src/S...

If you have some special logic validation - get it in OTDELENIE services. - Darien46 commented on June 10th 19 at 16:42
No , well, the inheritance of the Application class from the container it is currently used in... microframework, where the main thing is to make a small and simple code. - Justice.Nienow commented on June 10th 19 at 16:45
About microframework agree. But Laravale the Application interface extends the Container interface. Why in the end one big interface? - Darien46 commented on June 10th 19 at 16:48
Thanks for the tips) - jaycee commented on June 10th 19 at 16:51
So RAD to do. - Justice.Nienow commented on June 10th 19 at 16:54
, RAD? - jaycee commented on June 10th 19 at 16:57
, RAD. - Justice.Nienow commented on June 10th 19 at 17:00
, ) - jaycee commented on June 10th 19 at 17:03
just did the Lara approach is very correct. This increased level of abstraction, where the action of the controller transforms from a handler any handler query a valid query. The handler is not valid will be somewhere else. And the rules and restrictions for each query will be described directly in the class of this query. In the end, each handler will use the request object as a DTO with the necessary data. - Cassandre_Boy commented on June 10th 19 at 17:06
I wrote above my understanding of the validity of the HTTP request: it needs to meet the specifications of RFC2616. The validity of the user data is the problem of the subject area. In this sense we disagree with you, therefore, to argue about.

You maravelia will give you and ActiveRecord to a HTTP request attach to database save :) - Darien46 commented on June 10th 19 at 17:09
between specai http Protocol and data subject area there is another layer - the application interface. On the web this interface represents a set of rules used to build the query. Here for compliance with these rules, the request and worth checking out, but data validation of the subject field is irrelevant.

You lepselter will give you all the code in one controller write :) - Cassandre_Boy commented on June 10th 19 at 17:12
About the presence of a layer between the HTTP and domain absolutely I agree with you - but in my opinion, it should be the controller.

Can you give an example of the data used to build the query and the rules for them? I may have misunderstood the question, thinking that the author has in mind the data validation of the subject area. - Darien46 commented on June 10th 19 at 17:15
for example, the authorization token. Limitations the http interface:
1) Must be passed in the request headers
2) the Key should be the word "Auth-Token"
3) the Value must not be empty
4) the Minimum value of 30 characters
5) Maximum 50 characters
6) Can contain only numbers and Latin characters in both registers

But even granting all these conditions, the token may not be valid from the point of view of the application (there is no such validity period has expired, etc). But until we run the application logic, we'll never know. And unlike the above rules, which are described declaratively, and even on a piece of paper to print and distribute as a documentation to the api.

Or another example - the Bank's BIC. Interface restrictions:
1) Must peredovat in the request body
2) the Key should be the word "bik"
3) the Value must not be empty
4) Must contain 9 characters
5) the First character must always be "0"
6) Can contain only numbers

But again, even granting all these conditions, it may not be valid from the point of view of the subject area, just because there is the Bank with the BIC. - Cassandre_Boy commented on June 10th 19 at 17:18
Ok, thanks for the examples.

I partially agree with the first example, because the token is not really associated with the subject area, but to check the tokens I would use either the middleware or the subscriber of the event (but not as much as it makes Laravel).

In the second case, I would do validation in the controller using the component Form (again, not as it offers to make Laravel).

I'm ending this discussion, it's useless :) - Darien46 commented on June 10th 19 at 17:21
June 10th 19 at 16:26
Solution
Complement

https://symfony.com/doc/current/controller/argumen...

class NewUserRequestResolver implements \Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface
{
/**
 * @var \Symfony\Component\Serializer\SerializerInterface
*/
 private $serializer;

/**
 * @var \Symfony\Component\Validator\Validator\ValidatorInterface
*/
 private $validator;

 public function __construct(
 \Symfony\Component\Serializer\SerializerInterface $serializer,
 \Symfony\Component\Validator\Validator\ValidatorInterface $validator
 ) {
 $this->serializer = $serializer;
 $this->validator = $validator;

}

/**
 * @inheritDoc
*/
 public function supports(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata $argument)
{
 return NewUserRequest::class === $argument->getType();
}

/**
 * @inheritDoc
*/
 public function resolve(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata $argument)
{
 $newUserRequest = $this->serializer->deserialize($request->getContent(), NewUserRequest::class,'json');
 $constraints = $this->validator->validate($newUserRequest);
 if ($constraints->count() == 0) {
 yield $newUserRequest;
}

 throw new \Symfony\Component\HttpKernel\Exception\BadRequestHttpException();
}
}


As a result, the controller can be used
public function registerNewUser(CreqteNewUserRequest $request)
And you can do to make it work for all classes in the specified namespace? Something like this:
public function supports(Request $request, ArgumentMetadata $argument)
{
 // $this->namespace can be set to, for example App\Request
 return 0 === strpos($argument->getType(), $this->namespace);
}
- jaycee commented on June 10th 19 at 16:29
of course - Darien46 commented on June 10th 19 at 16:32
, well, it is necessary to do a bundle :). Type ValidateRequestBundle...

not want, as an exercise? As I understand it, you still studying Symfony. Will practice. - Justice.Nienow commented on June 10th 19 at 16:35
why bundle? for one file? :) - Darien46 commented on June 10th 19 at 16:38
Why for one? Interfaces to stick to semantic configuration, unit tests, integration tests... It is Symfony, it's the enterprise.

PS I was joking :)

P. P. S. do not use @inheritdoc, in the Symphony it is used incorrectly, but with a specific meaning: https://github.com/symfony/symfony/issues/13696. - jaycee commented on June 10th 19 at 16:41
Thanks for the tips) - Darien46 commented on June 10th 19 at 16:44
I just started teaching in my spare time for your project. For he saw the shoals of Larval))
Listened to seniors, and I realized that the Symphony is more suitable.
About the bundle, in the Symphony no 4 is bundles.
Recipes, and packages. Washed down of course, but by itself, usually when there is opensorce, starts mess, all for all and for all. Not sure that there is time to cut this package. So far not even completed the validation regular as I should. - Justice.Nienow commented on June 10th 19 at 16:47
that is all what I have achieved:
Controller "App\Application\Controller\Common\AuthController::registerNewUser()" requires that you provide a 
value for the "$request" argument. Either the argument is null nullable and no value has been provided, no 
default value has been provided or because there is a non optional argument after this one.


Controller
namespace App\Application\Controller\Common;

/**
 * @Route("/registration/user" name="registration.user")
*
 * @param CreateNewUserRequest $request
*/
 public function registerNewUser(CreateNewUserRequest $request)
{
dump($request);


Request
<?php declare(strict_types = 1);

namespace App\Infrastructure\Validator\Request;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints;

class CreateNewUserRequest
{
/**
 * CreateNewUserRequest constructor.
*/
 public function __construct(
 // some injects
 ) {
}

 public function validateRequest(Request $request)
{
 $validator = Validation::createValidator();

 $constraint = new Constraints\Collection(
[
 'email' =--> new Constraints\Email(),
]
);

 $violations = $validator->validate($request->query, $constraint);

 return $violations;
}
}


Resolver
namespace App\Resolver;


CreateNewUserRequestResolver class implements ArgumentValueResolverInterface
{
 private $serializer;
 private $validator;

 public function __construct(
 SerializerInterface $serializer,
 ValidatorInterface $validator
 ) {
 $this->serializer = $serializer;
 $this->validator = $validator;
}

/**
 * @inheritDoc
*/
 public function supports(Request $request, ArgumentMetadata $argument)
{
 return CreateNewUserRequest::class === $argument->getType();
}

/**
 * @inheritDoc
*/
 public function resolve(Request $request, ArgumentMetadata $argument)
{
 $newUserRequest = $this->serializer->deserialize($request->getContent(), CreateNewUserRequest::class, 'json');
 $constraints = $this->validator->validate($newUserRequest);
 if ($constraints->count() == 0) {
 yield $newUserRequest;
}

 throw new BadRequestHttpException();
 }


In CreateNewUserRequestResolver it doesn't load, I don't know how to call it - Darien46 commented on June 10th 19 at 16:50
how much I remember for ArgumentValueResolverInterface autoconfigure is not available, it is necessary to affix the tag - jaycee commented on June 10th 19 at 16:53

Find more questions by tags SymfonyValidation