Ich überprüfe einige Validierungen in meinem Controller. Und ich möchte bei einem Fehler einem bestimmten Element meines Formulars einen Fehler hinzufügen. Mein Formular:
use Symfony\Component\Form\FormError;
// ...
$config = new Config();
$form = $this->createFormBuilder($config)
->add('googleMapKey', 'text', array('label' => 'Google Map key'))
->add('locationRadius', 'text', array('label' => 'Location radius (km)'))
->getForm();
// ...
$form->addError(new FormError('error message'));
Die Methode addError() fügt dem Formular einen Fehler hinzu, nicht dem Element. Wie kann ich dem locationRadius-Element einen Fehler hinzufügen?
Du kannst tun
$form->get('locationRadius')->addError(new FormError('error message'));
Als Formelemente eignen sich auch z FormInterface
Typ.
OK Leute, ich habe einen anderen Weg. Es ist komplexer und nur für bestimmte Fälle.
Mein Fall:
Ich habe ein Formular und poste nach dem Absenden Daten an den API-Server. Und Fehler, die ich auch vom API-Server bekommen habe.
API-Server-Fehlerformat ist:
array(
'message' => 'Invalid postal code',
'propertyPath' => 'businessAdress.postalCode',
)
Mein Ziel ist eine flexible Lösung. Legen wir den Fehler für das entsprechende Feld fest.
$vm = new ViolationMapper();
// Format should be: children[businessAddress].children[postalCode]
$error['propertyPath'] = 'children['. str_replace('.', '].children[', $error['propertyPath']) .']';
// Convert error to violation.
$constraint = new ConstraintViolation(
$error['message'], $error['message'], array(), '', $error['propertyPath'], null
);
$vm->mapViolation($constraint, $form);
Das ist es!
HINWEIS! addError()
Methode umgeht error_mapping Möglichkeit.
Mein Formular (Adressformular eingebettet in das Firmenformular):
Gesellschaft
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints;
class Company extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('companyName', 'text',
array(
'label' => 'Company name',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
->add('businessAddress', new Address(),
array(
'label' => 'Business address',
)
)
->add('update', 'submit', array(
'label' => 'Update',
)
)
;
}
public function getName()
{
return null;
}
}
Adresse
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints;
class Address extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('postalCode', 'text',
array(
'label' => 'Postal code',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
->add('town', 'text',
array(
'label' => 'Town',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
->add('country', 'choice',
array(
'label' => 'Country',
'choices' => $this->getCountries(),
'empty_value' => 'Select...',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
;
}
public function getName()
{
return null;
}
}