2012-08-28 12 views
9

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

namespace WNC\SoldierBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* WNC\SoldierBundle\Entity\Soldier 
* 
* @ORM\Table(name="soldier") 
* @ORM\Entity(repositoryClass="WNC\SoldierBundle\Entity\SoldierRepository") 
* @ORM\HasLifecycleCallbacks() 
*/ 
class Soldier 
{ 

    /** 
    * @var string $picture 
    * @Assert\Image() 
    * @ORM\Column(name="picture", type="string", length=255) 
    */ 
    private $picture; 

    /** 
    * @var string $file 
    * 
    * @Assert\Image() 
    * @Assert\NotBlank() 
    */ 
    public $file; 


    public function getAbsolutePath() 
    { 
     return null === $this->picture ? null : $this->getUploadRootDir().'/'.$this->picture; 
    } 

    public function getWebPath() 
    { 
     return null === $this->picture ? null : $this->getUploadDir().'/'.$this->picture; 
    } 

    protected function getUploadRootDir() 
    { 
     // the absolute directory path where uploaded documents should be saved 
     return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
    } 

    protected function getUploadDir() 
    { 
     // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view. 
     return 'uploads/pictures'; 
    } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 

     if($this->picture && file_exists($this->getAbsolutePath())) { 
      unlink($this->getAbsolutePath()); 
     } 

     if (null !== $this->file) { 
      // do whatever you want to generate a unique name 
      $this->picture = uniqid().'.'.$this->file->guessExtension(); 
     } 

    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() 
    { 
     if (null === $this->file) { 
      return; 
     } 


     // if there is an error when moving the file, an exception will 
     // be automatically thrown by move(). This will properly prevent 
     // the entity from being persisted to the database on error 
     $this->file->move($this->getUploadRootDir(), $this->picture); 

    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() 
    { 
     if ($file = $this->getAbsolutePath()) { 
      unlink($file); 
     } 
    } 
} 

पर्चा बिल्डर:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('mothers_name') 
     ->add('service_end_date', 'date',array(
      'widget' => 'single_text', 
      'format' => 'MM/dd/yyyy', 
      'attr' => array('class' => 'date six columns') 
     )) 
     ->add('army_unit') 
     ->add('city', 'city_selector') 
     ->add('gender', 'choice', array(
      'choices' => array(0 => 'Male', 1 => 'Female'), 
      'required' => false, 
      'expanded' => true, 
      'label' => 'Male/Female', 
      'data' => 0 
     )) 
     ->add('file','file', array(
      'data_class' => 'Symfony\Component\HttpFoundation\File\File', 
      'label' => 'Picture' 
     )) 
     ->add('self_description', 'textarea') 
     ->add('video', null, array(
      'attr' => array(
      'placeholder' => 'some link here' 
     ))) 
     ->add('wants_to_contact', null, array(
      'label' => Soldier::getLabel('wants_to_contact') 
     )) 
     ->add('comments', 'textarea') 
     ->add('user', new NameFormType('Application\Sonata\UserBundle\Entity\User')) 
     ->add('city', 'city_selector') 

    ; 


} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'validation_groups' => array('Registration'), 
     'cascade_validation' => true, 
    )); 


} 

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

नियंत्रक:

/** 
* Creates a new Soldier entity. 
* 
* @Route("/create", name="soldier_create") 
* @Method("POST") 
* @Template("WNCSoldierBundle:Soldier:new.html.twig") 
*/ 
public function createAction(Request $request) 
{ 
    $entity = new Soldier(); 
    $form = $this->createForm(new SoldierType(), $entity); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 

     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('soldier_show', array('id' => $entity->getId()))); 
    } 

    return array(
     'entity' => $entity, 
     'form' => $form->createView(), 
    ); 
} 
+0

अलग-अलग विषय: मैंने देखा है कि आपके पास पथों के साथ एक ही समस्या है, आप इसे देखना चाहते हैं: http://stackoverflow.com/questions/12168086/how-to-deal-with-relative-paths -इन-सिम्फनी -2 – ChocoDeveloper

+0

धन्यवाद, लेकिन काम को निर्दोष बचाते हैं लेकिन मुझे कोई कारण नहीं मिल रहा है कि सत्यापन क्यों काम नहीं कर रहा है। शायद क्योंकि यह केवल एक वर्चुअल फ़ील्ड है जिसे डीबी कॉलम के रूप में उपयोग नहीं किया जाता है? –

+0

मैंने अभी एक परीक्षण ऐप बनाया है जो इस जोर का उपयोग करता है और यह ठीक काम करता है। गैर छवियों ने एक त्रुटि दी, और छवियों के माध्यम से चला गया। यह सिम्फनी 2.1 पर है। आपका फॉर्म बिल्डर कैसा दिखता है? – Kris

उत्तर

3

मुझे समाधान मिला। फॉर्म परिभाषा में मैं `validation_groups '=> सरणी (' पंजीकरण ') का उपयोग कर रहा हूं। मैंने सोचा कि जब वैधता के लिए कोई समूह नहीं है तो यह फॉर्म परिभाषा में से किसी से भी मेल खाएगा।

जब मैंने वैधता में समूह संपत्ति जोड़ दी है तो आखिरकार सबकुछ काम कर रहा था। तो का उपयोग कर validation.yml उदाहरण के लिए:

WNC\SoldierBundle\Entity\Soldier: 
    properties: 
     file: 
      - Image: {groups: [Registration]} 
5

चेक आउट यह पिछले तो सवाल: Symfony2 validation using Assert annotation does not work। आप यह सुनिश्चित करना चाहते हैं कि आप Symfony2 का उपयोग करने के लिए सभी अनुशंसित विन्यासों को पूरा कर चुके हैं।

इसके अलावा, $picture को Image बाधा के साथ मान्य करना आवश्यक नहीं है क्योंकि यह फ़ाइल/छवि नहीं है।

/** 
* @var string $picture 
* @Assert\Image()          <-- Should be removed 
* @ORM\Column(name="picture", type="string", length=255) 
*/ 
private $picture; 

/** 
* @var string $file          <-- @var UploadedFile $file 
* 
* @Assert\Image() 
* @Assert\NotBlank() 
*/ 
public $file; 

मैं वास्तव में मान्य करने के लिए है कि एक अपलोड की गई फ़ाइल YAML विकल्प का उपयोग कर तो आप भी करके देख सकते हैं एक छवि है कर रहा था कि कुछ भी नहीं आता है।

+0

उत्तर के लिए धन्यवाद, दुर्भाग्य से आपके सुझावों ने समस्या को ठीक नहीं किया है। आपके द्वारा प्रदान किया गया लिंक अन्य सत्यापनकर्ता से संबंधित है जो मैं उपयोग कर रहा हूं। मुझे मानक मान्यताओं जैसे कि आवश्यक, खाली नहीं आदि के साथ कोई समस्या नहीं है। मैंने VichUploaderBundle की जांच की और जीवन चक्र एनोटेशन हटा दिए लेकिन इससे कोई फर्क नहीं पड़ता। आप किस सिम्फनी संस्करण का उपयोग कर रहे हैं? –

+0

दूसरी ओर yml सत्यापन फ़ाइल भी इस मामले में अप्रभावी थी –

+0

हाय @ ल्यूकएडडज़ेस्की, हां, मुझे पता है कि मैंने जो लिंक प्रदान किया है वह एक अलग सत्यापनकर्ता के बारे में था। मुझे लगता है कि वास्तव में यह जानकारी उपयोगी थी, हालांकि वास्तव में यह जवाब intl एक्सटेंशन के संबंध में http://stackoverflow.com/a/7946408/1349295 था। शायद एसएफ 2 के लिए सभी अनुशंसित विन्यास सुनिश्चित किए गए हैं? बीटीडब्ल्यू, मुझे यह एसएफ 2.0 पर काम मिला। मैं 2.1 से 2.0 तक कुछ कोड पोर्ट कर रहा हूं और कुछ महत्वपूर्ण बदलावों का सामना करना पड़ा (और अभी भी बदल रहा है)। संयोग से, एसएफ 2.1 बीटा पर नहीं है! अभी अभी चेक किया गया। शायद नवीनतम वितरण में उन्नयन करने का प्रयास करें। :) –

0

आप बाधा जो अपने क्षेत्र के लिए उपयुक्त नहीं है का उपयोग कर। फ़ाइल फ़ाइल संपत्ति पर बस फ़ाइल बाधा के साथ चिपके रहें।

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