Sonata Admin Bundle - String Validation
bundle, sonata-admin, string, symfony, validation
Solution
You can add a custom callback validator to verify your strings with all cases you want.
Just add in your admin class:
/**
* {@inheritdoc}
*/
public function validate(ErrorElement $errorElement, $object)
{
$errorElement
->assertCallback(array('validateMyEntity'))
;
}
and in your entity:
use Symfony\Component\Validator\ExecutionContext;
/**
* Validates my entity and throw violations
*/
public function validateMyEntity(ExecutionContext $context)
{
$title = $this->getTitle();
if (empty($title)) {
$context->addViolation('Title can\'t be empty.');
}
}
Hope it helps.
Problem
I've some forms in my Sonata Admin Bundle and some fields are set as "required = true". Now, you can circumvent this requirement with an empty string, e.g. with space. It works even for integer-types.. How can I build a validation, which secures from some unallowed entries ? Thank you