Skip to content

Instantly share code, notes, and snippets.

@liuggio
Last active September 27, 2017 10:40
Show Gist options
  • Save liuggio/8fb1eb3eb50e871ed744 to your computer and use it in GitHub Desktop.
Save liuggio/8fb1eb3eb50e871ed744 to your computer and use it in GitHub Desktop.
The DTO has its validation, the controller uses the negotiation lib to get the proper formatter (twig or json_encode), the binder validate and fill the dto and create a BindResult
class BindResult {
private $data;
private $errors;
function __construct($data, $errors)
{
$this->data = $data;
$this->errors = $errors;
}
/**
*
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* @return bool
*/
public function isValid()
{
return empty($this->errors);
}
}
{# tutti i campi uno solo di esempio #}
{# render_select(explicit true/false, multiple true/false, $options, $selected, htmlAttributes) #}
{% render_select(explicit true/false, multiple true/false, authorsFromController, bindResult.data.authors, ['class'=>'authors'] , bindResult.error.authors) %}
<?php
class PageController extends Controller {
public function createAction(Request $request) {
$page = null
//$this->formatter->getFormat() //html or json or xml
//$this->formatter->getHtmlTemplatePath() // UI/Resources/views/create_get.twig.html
return $this->formatter->render($page);
}
public function postAction(Request $request) {
//bind->isValid()
//bind->getData()
//bind->getErrors()
$bindResult = $this->bind($request, Pages\DTO\CreatePage::class);
if (!$bindResult->isValid() && $this->formatter->isHtml()) {
return $this->renderHtml('pages_create', $bindResult, $authorsFromController);
}
return $this->handle($bindResult->getData()); // will call the CreatePageHandler
}
}
<?php
namespace Pages\DTO;
use Symfony\Component\Validator\Constraints as Assert;
class POSTPage {
/**
* @Assert\NotBlank()
* @Assert\Length(
* min = 2,
* max = 50)
*/
private $title;
/**
* @Assert\NotBlank()
* @Assert\Length(
* min = 10,
* max = 500)
*/
private $body;
/**
* @Assert\NotBlank()
* @Assert\Length(
* min = 2,
* max = 50)
*/
private $seoTitle;
/**
* @Assert\NotBlank()
*/
private $seoDescription;
/**
* @Assert\NotNull
* @Assert\Type(
* type="array"
* )
*/
private $authors;
private $published;
function __construct($title, $body, $seoTitle, $seoDescription, $authors, $published)
{
$this->title = $title;
$this->body = $body;
$this->seoTitle = $seoTitle;
$this->seoDescription = $seoDescription;
$this->author = $author;
$this->published = $published;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @return mixed
*/
public function getBody()
{
return $this->body;
}
/**
* @return mixed
*/
public function getSeoTitle()
{
return $this->seoTitle;
}
/**
* @return mixed
*/
public function getSeoDescription()
{
return $this->seoDescription;
}
/**
* @return mixed
*/
public function getAuthor()
{
return $this->author;
}
/**
* @return mixed
*/
public function getPublished()
{
return $this->published;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment