2012-11-13 4 views
14

मैं एक कस्टम सत्यापनकर्ता बना रहा हूं जिसे इस बाधा को पारित करने के लिए डीबी में दो फॉर्म फ़ील्ड से मान को सत्यापित करने की आवश्यकता है।बिल्डिंग सिम्फनी 2 कस्टम वैलिडेटर जो एकाधिक फ़ील्ड्स का उपयोग करता है

मेरा प्रश्न यह है: अनुबंध वैलिडेटर की वैध विधि के हस्ताक्षर में केवल एक $ मूल्य है, तो मैं सत्यापन करने के लिए केवल एक फ़ील्ड से अधिक मूल्यों तक पहुंच कैसे प्राप्त करूं?

namespace Acme\WebsiteBundle\Validator\Constraints; 

use Symfony\Component\Validator\Constraint; 
use Symfony\Component\Validator\ConstraintValidator; 

class MyCustomValidator extends ConstraintValidator 
{ 
    public function validate($value, Constraint $constraint) 
    { 
    // check $value and return an error 
    // but in my case, i want the value from more than one form field to do a validation 
    // why? i'm checking that two pieces of information (ssn + dob year) match 
    // the account the user is registering for 
    } 
} 

यहाँ कुछ सत्यापन सेट के साथ एक फार्म के वर्ग का एक उदाहरण है:

namespace ACME\WebsiteBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Symfony\Component\Validator\Constraints\Collection; 
use Symfony\Component\Validator\Constraints\MinLength; 
use Symfony\Component\Validator\Constraints\NotBlank; 
use Symfony\Component\Validator\Constraints\Regex; 
use ACME\WebsiteBundle\Validator\Constraints\UsernameAvailable; 

class AccountRegistration extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
    $builder->add('ssn', 'number', array(
     'max_length' => 9, 
     'required' => true, 
     'error_bubbling' => true) 
    ); 

    $builder->add('year_of_birth', 'choice', array(
     'choices' => range(date("Y") - 100, date("Y")), 
     'required' => true, 
     'empty_value' => 'Select ...', 
     'label' => 'Year of Birth', 
     'error_bubbling' => true) 
    ); 

    $builder->add('username', 'text', array(
     'required' => true, 
     'error_bubbling' => true) 
    ); 

    $builder->add('password', 'password', array(
     'max_length' => 25, 
     'required' => true, 
     'error_bubbling' => true) 
    ); 

    $builder->add('security_question', 'choice', array(
     'empty_value' => 'Select ...', 
     'choices' => array(), 
     'label' => 'Security Question', 
     'required' => true, 
     'error_bubbling' => true) 
    ); 

    $builder->add('security_question_answer', 'text', array(
     'label' => 'Answer', 
     'required' => true, 
     'error_bubbling' => true) 
    ); 
    } 

    public function getName() 
    { 
    return 'account_registration'; 
    } 

    public function getDefaultOptions(array $options) 
    { 

    $collectionConstraint = new Collection(array(
     'allowExtraFields' => true, 
     'fields' => array(
     'ssn' => array(new MinLength(array('limit' => 9, 'message' => 'too short.')), new NotBlank()), 
     'year_of_birth' => array(new NotBlank()), 
     'username' => array(new NotBlank(), new UsernameAvailable()), 
     'password' => array(new NotBlank(), new Regex(array(
      'message' => 'password must be min 8 chars, contain at least 1 digit', 
      'pattern' => "((?=.*\d)(?=.*[a-z]).{8,25})")) 
     ), 
     'security_question' => array(new NotBlank()), 
     'security_question_answer' => array(new NotBlank())) 
    ) 
    ); 

    return array(
     'csrf_protection' => true, 
     'csrf_field_name' => '_token', 
     'intention'  => 'account_registration', 
     'validation_constraint' => $collectionConstraint 
    ); 
    } 
} 

उत्तर

4

आप cookbooks में वर्णित के रूप CLASS_CONSTRAINT उपयोग करने की आवश्यकता

यहाँ एक ठेठ कस्टम सत्यापनकर्ता है। फिर आप पूरी कक्षा पारित कर लेते हैं और इस वर्ग की किसी भी संपत्ति का उपयोग कर सकते हैं। ऊपर दिए गए आपके उदाहरण में, इसका मतलब यह होगा कि $value की बजाय एक स्ट्रिंग/पूर्णांक मधुमक्खी, यह वह संपूर्ण ऑब्जेक्ट होगी जिसे आप सत्यापित करना चाहते हैं।

केवल एक चीज जिसे आप बदलने की जरूरत है getTargets() फ़ंक्शंस है, जिसे self::CLASS_CONSTRAINT वापस करना है।

यह भी सुनिश्चित करें कि आप कक्षा स्तर पर अपने सत्यापनकर्ता को परिभाषित करें, संपत्ति स्तर पर नहीं। आप एनोटेशन का उपयोग करते हैं तो इसका मतलब है सत्यापनकर्ता वर्ग defnition ऊपर वर्णित किया जाना चाहिए कि, ऊपर नहीं एक विशिष्ट विशेषता परिभाषा: कि ConstraintValidator फैली

/** 
    * @MyValidator\SomeValidator 
    */ 
class MyClass { 

} 
+0

मैं एक वर्ग के खिलाफ मान्य नहीं कर रहा हूँ $contextExecutionContext का एक उदाहरण है कि आप सबमिट किए गए डेटा तक पहुँच देता है। मैं बस सार प्रकार का एक स्टैंडअलोन रूप बना रहा हूँ। – doremi

+0

लेकिन आपके पास कुछ ऑब्जेक्ट है जिसे आप मान्य कर रहे हैं? यदि नहीं, तो क्या आप कोड सेट कर सकते हैं कि आप सत्यापन कैसे सेट करते हैं? – Sgoettschkes

+0

जोड़ा गया। आपको पूरी चीज़ देखने के लिए स्क्रॉल करने की आवश्यकता होगी, लेकिन यह खाता पंजीकरण फॉर्म है। आप सत्यापन बाधाओं को देख सकते हैं जिन्हें मैंने पहले ही नीचे जोड़ा है। – doremi

16

किसी भी कस्टम सत्यापनकर्ता $context संपत्ति की पहुंच है।

उदाहरण::

<?php 

namespace My\Bundle\MyBundle\Validator\Constraints; 

use Symfony\Component\Validator\Constraint; 
use Symfony\Component\Validator\ConstraintValidator; 


class AppointorRoleValidator extends ConstraintValidator 
{ 

    public function validate($value, Constraint $constraint) 
    { 
     $values = $this->context->getRoot()->getData(); 
     /* ... */ 
    } 
} 
+3

$ value = $ this-> संदर्भ-> getRoot() -> getData() –

+0

आपका स्वागत है –

+0

मुझे आपका स्वागत है $ मूल्य = $ यह-> संदर्भ-> getRoot(), नहीं सोचा संभव था –

संबंधित मुद्दे