Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Add fragment backoffice title and improve validation #22

Merged
merged 3 commits into from Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Admin/ArticleAdmin.php
Expand Up @@ -108,6 +108,11 @@ public function validate(ErrorElement $errorElement, $object)
AbstractArticle::getStatuses() : AbstractArticle::getContributorStatus()))
->end()
;

$fragmentAdmin = $this->getChild('sonata.article.admin.fragment');
foreach ($object->getFragments() as $fragment) {
$fragmentAdmin->validate($errorElement, $fragment);
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions FragmentService/AbstractFragmentService.php
Expand Up @@ -13,8 +13,8 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down Expand Up @@ -109,8 +109,13 @@ public function configureOptions(OptionsResolver $resolver)
/**
* {@inheritdoc}
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context)
public function validate(ErrorElement $errorElement, $object)
{
if (empty($object->getBackofficeTitle())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a more Symfony-ish way of doing this, by using the NotBlank constraint?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could save you some translations…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole validation part is still a work in progress, for the moment we can't use symfony constraints, we are working on that on another PR. I will add the todo in doc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

$errorElement
->addViolation(sprintf('Fragment %s - `Backoffice Title` must not be empty', $this->getName()))
;
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions FragmentService/FragmentServiceInterface.php
Expand Up @@ -13,8 +13,8 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down Expand Up @@ -42,8 +42,13 @@ public function buildCreateForm(FormMapper $form, FragmentInterface $fragment);
*
* @param FragmentInterface $fragment
* @param ExecutionContextInterface $context
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uuuh… what?

* Validates the fragment (you'll need to add your violations through $errorElement).
*
* @param ErrorElement $errorElement
* @param object $object
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context);
public function validate(ErrorElement $errorElement, $object);

/**
* Returns the Fragment service readable name.
Expand Down
12 changes: 5 additions & 7 deletions FragmentService/TextFragmentService.php
Expand Up @@ -13,8 +13,8 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down Expand Up @@ -42,13 +42,11 @@ public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
/**
* {@inheritdoc}
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context)
public function validate(ErrorElement $errorElement, $object)
{
if (empty($fragment->getSettings()['text'])) {
$context
->buildViolation('`Text` must not be empty')
->atPath('settings.text')
->addViolation()
if (empty($object->getSetting('text'))) {
$errorElement
->addViolation('Fragment Text - `Text` must not be empty')
;
}
}
Expand Down
21 changes: 9 additions & 12 deletions FragmentService/TitleFragmentService.php
Expand Up @@ -13,9 +13,9 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\ArticleBundle\Model\FragmentInterface;
use Sonata\CoreBundle\Validator\ErrorElement;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down Expand Up @@ -44,20 +44,17 @@ public function buildEditForm(FormMapper $form, FragmentInterface $fragment)
/**
* {@inheritdoc}
*/
public function validate(FragmentInterface $fragment, ExecutionContextInterface $context)
public function validate(ErrorElement $errorElement, $object)
{
if (empty($fragment->getSettings()['text'])) {
$context
->buildViolation('`Title` must not be empty')
->atPath('settings.text')
->addViolation()
if (empty($object->getSetting('text'))) {
$errorElement
->addViolation('Fragment Title - `Text` must not be empty')
;
}
if (strlen($fragment->getSetting('text')) > 255) {
$context
->buildViolation('`Title` must not be longer than 255 characters.')
->atPath('settings.text')
->addViolation()

if (strlen($object->getSetting('text')) > 255) {
$errorElement
->addViolation('Fragment Text - `Text` must not be longer than 255 characters.')
;
}
}
Expand Down