ठीक है दोस्तों, मेरे पास एक और तरीका है। यह अधिक जटिल है और केवल विशिष्ट मामलों के लिए।
मेरे मामले:
मैं एक रूप है और बाद प्रस्तुत मैं एपीआई सर्वर से डेटा पोस्ट। और त्रुटियों को मैं एपीआई सर्वर से भी मिला।
एपीआई सर्वर त्रुटि प्रारूप है:
array(
'message' => 'Invalid postal code',
'propertyPath' => 'businessAdress.postalCode',
)
मेरा लक्ष्य लचीला समाधान प्राप्त करने के लिए है। आइए संबंधित फ़ील्ड के लिए त्रुटि सेट करें।
$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);
यही है!
नोट!addError()
विधि error_mapping विकल्प बाईपास करता है।
मेरे प्रपत्र (पता प्रपत्र कंपनी के रूप में एम्बेडेड):
कंपनी
<?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;
}
}
पता
<?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;
}
}
धन्यवाद। यह मेरी समस्या हल हो गई है। – pltvs
@ m2mdas, महान जवाब! हम इसका अनुवाद कैसे करेंगे? क्योंकि एक बार जब हम फॉर्मरर उदाहरण बनाते हैं, तो यह इसका अनुवाद नहीं करेगा, क्या मैं सही हूँ? मैंने कोशिश की और यह इसका अनुवाद नहीं करता है, और मुझे लगता है कि यह समझ में आता है। आप FormError इंस्टेंस का अनुवाद कैसे करेंगे? – Mick
हैलो @ पैट, देर से उत्तर के लिए खेद है। त्रुटि संदेशों को फॉर्म में जोड़े जाने से पहले वैलिडेटर घटक फॉर्म बाधा उल्लंघन संदेशों का अनुवाद करने का ख्याल रखता है। कस्टम त्रुटि जोड़ने के लिए आपने संदेश को उसी तरह अनुवादित किया है जैसा कि आप अन्य तारों के लिए करते हैं, '$ this-> प्राप्त करें (' अनुवादक ') -> ट्रांस (' त्रुटि संदेश ') ' –