PHP code example of adamsafr / form-request-bundle
1. Go to this page and download the library: Download adamsafr/form-request-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/ */
adamsafr / form-request-bundle example snippets
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
new Adamsafr\FormRequestBundle\AdamsafrFormRequestBundle(),
];
// ...
}
// ...
}
// src/Request/UserRequest.php
namespace App\Request;
use Adamsafr\FormRequestBundle\Http\FormRequest;
use App\Service\Randomizer;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;
class UserRequest extends FormRequest
{
/**
* @var Randomizer
*/
private $randomizer;
/**
* You can inject services here
*
* @param Randomizer $randomizer
*/
public function __construct(Randomizer $randomizer)
{
$this->randomizer = $randomizer;
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return $this->randomizer->getNumber() > 0.5;
}
/**
* Get the validation rules that apply to the request.
*
* @return null|Constraint|Constraint[]
*/
public function rules()
{
return new Assert\Collection([
'fields' => [
'email' => [
new Assert\NotBlank(),
new Assert\NotNull(),
new Assert\Email(),
],
'firstName' => new Assert\Length(['max' => 255]),
'lastName' => new Assert\Optional([
new Assert\Length(['max' => 3]),
]),
],
]);
}
}
// src/Controller/ApiTestController.php
namespace App\Controller;
use App\Request\UserRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ApiTestController extends AbstractController
{
public function index(UserRequest $form)
{
$email = $form->getRequest()->request->get('email');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.