-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Alchemy\AdminBundle\Field; | ||
|
||
use Alchemy\AdminBundle\Form\YamlType; | ||
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait; | ||
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface; | ||
|
||
final class YamlField implements FieldInterface | ||
{ | ||
use FieldTrait; | ||
|
||
/** | ||
* @param string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
->setFormType(YamlType::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Alchemy\AdminBundle\Form; | ||
|
||
use Symfony\Component\Yaml\Yaml; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Yaml\Exception\ParseException; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
class YamlType extends AbstractType | ||
{ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'attr'=> [ | ||
'rows' => 10, | ||
'style' => 'font-family: "Courier New"', | ||
], | ||
'constraints' => [ | ||
new Assert\Callback( | ||
function (mixed $data, ExecutionContextInterface $context) { | ||
try { | ||
Yaml::parse($data); | ||
} catch (ParseException $e) { | ||
$context | ||
->buildViolation(sprintf('YAML error: %s', $e->getMessage())) | ||
->addViolation(); | ||
} | ||
} | ||
) | ||
] | ||
]); | ||
} | ||
|
||
public function getParent(): ?string | ||
{ | ||
return TextareaType::class; | ||
} | ||
} |