PHP code example of rimote / rimote-validation-bundle

1. Go to this page and download the library: Download rimote/rimote-validation-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rimote / rimote-validation-bundle example snippets



    // AppBundle/Entity/Cat.php
    
    // ...

    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    use Doctrine\ORM\Mapping as ORM;

    /**
     * @Doctrine\ORM\Mapping\ORM\Entity(repositoryClass=Repository\CatRepository::class)
     * @Doctrine\ORM\Mapping\ORM\Table(name="cats")
     */
    class Cat
    {
        /**
         * @Assert\Type(type = "int"})
         * 
         * @ORM\Column(type = "integer")
         * @ORM\Id
         * @ORM\GeneratedValue
         */
        protected $id;
        
        /**
         * @Assert\Length(max = 255)
         * @Assert\NotNull
         * @Assert\Type(type = "string")
         * @Assert\NotBlank
         * 
         * @ORM\Column(length = 255)
         */
        protected $name;
        
        /**
         * @Assert\Length(max = 64)
         * @Assert\NotNull
         * @Assert\Type(type = "string")
         * @Assert\NotBlank
         * 
         * @ORM\Column(length = 64)
         */
        protected $fur_type;
        
        /**
         * @Assert\Length(max = 6)
         * @Assert\NotNull
         * @Assert\Type(type = "string")
         * @Assert\NotBlank
         * 
         * @ORM\Column(length = 6)
         */
        protected $gender;
    }



// ...

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Rimote\ValidationBundle\Validator\Exception\ErrorMessagesException;
use AppBundle\Entity\Cat;

/**
 * @Route("/cats/", name="api_cats_create")
 * @Method("POST")
 * @ParamConverter("cat", converter="fos_rest.request_body")
 */
public function createAction(Request $request, Cat $cat)
{
    // ...

    $validator = $this->get('rimote.validator');

    try {
        $validator->validate($cat);
    } catch (ErrorMessagesException $e) {
        return new JsonResponse(['errors' => $e->getErrors()], 500);
    }
    
    // Run some code to for instance persist the cat in our database
}