Custom controller validation is not working Api-platform

1.3k Views Asked by At

Hello I'm trying to validate User Entity validation but only one property with PATCH request but I'm getting Cannot validate values of type \"NULL\" automatically. Please provide a constraint.

I'm getting the same result even if I remove Default from validation_groups

your help is very appreciated.

App\Entity\User

     /**
         * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
         * @ApiResource(
         *     itemOperations={
         *          "get",
         *          "put" = {
         *              "method" = "PUT",
         *              "security"="(is_granted('ROLE_ADMIN') and object.getAssociation() == user.getAssociation())",
         *              "security_message"="Only Admin or the owner can edit this field",
         *              "denormalization_context"={"groups"={"user:update"}, "disable_type_enforcement"=true},
         *              "validation_groups"={"Default", "user_update"}
         *          },
         *          "patch_add_new_member_to_pay" = {
         *              "method" = "PATCH",
         *              "path" = "/users/{id}/addNewMemberToPay",
         *              "controller" = PayingMembershipForOthersController::class,
         *              "denormalization_context" = {"groups" = {"add_new_member_to_pay"}},
         *              "validation_groups" = {"Default", "add_new_member_to_pay"}
         *          }
         *      },
         *     collectionOperations={
         *          "get",
         *          "post" = {
         *               "method" = "POST",
         *               "security"="is_granted('ROLE_ADMIN')",
         *               "denormalization_context"={"groups"={"user:create"}, "disable_type_enforcement"=true},
         *               "validation_groups"={"Default", "user_create"}
         *          },
         *      },
         *      normalizationContext={"groups"={"user:read"}}
         * )
         */

    
    class User {
       /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         *
         * @Groups({"user:read"})
         */
        private ?int $id = null;

       /**
         * @ORM\Column(type="string", length=100)
         * @Assert\NotBlank(groups={"user_create", "user_update"})
         * @Assert\Regex(pattern="/\d/", match=false, message="Your name should not contain numbers")
         * @Groups({"user:read", "user:create", "user:update"})
         */
        private ?string $fullname;
    
       /**
         * @Groups({"add_new_member_to_pay"})
         * @Assert\NotNull(groups={"add_new_member_to_pay"})
         */
        private ?string $payForNewMember = null;
        
        ...getters and setters
   }

   

App\Controller\PayingMembershipForOthersController

namespace App\Controller;

use ApiPlatform\Core\Validator\ValidatorInterface;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;

class PayingMembershipForOthersController
{

    private ValidatorInterface $validator;

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

    public function __invoke(User $data, Request $request)
    {
        $this->validator->validate($data);
    }

}

so by purpose i set empty value {"payForNewMember": ""} validation don't show violations list

1

There are 1 best solutions below

2
On

Your custom controller has to return the user or a Response.

class PayingMembershipForOthersController
{

    private ValidatorInterface $validator;

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

    public function __invoke(User $data)
    {
        $this->validator->validate($data);
        return $data;
    }

}