Coder Social home page Coder Social logo

Comments (11)

cedriclombardot avatar cedriclombardot commented on August 16, 2024

First of all i invite you to read : http://symfony2admingenerator.org/documentation/form-advanced.html#choose-your-types

You have to ways to overwrite the type of a field by php overwriting the build method or by yaml.

Yaml is more fun :D so in your yaml so write something like that :

edit:
  fields:
    text_type:
      formType: choice
      formOptions:
         choices:
            text: A simple text
            html: With a html editor

Of course for file and wysiwyg its the same.

If your wysiwyg require js read :http://symfony2admingenerator.org/documentation/form.html#overwrite-the-rendering-of-a-field

from admingeneratorgeneratorbundle.

suncat2000 avatar suncat2000 commented on August 16, 2024

Thank for choices, this is what I need... Add in the documentation please.

For a file (preview_img) as I understand these steps:

  • Add a html to the template "NamespaceYourBundle: (Edit | New): index.html.twig"

    {% block form_preview_img%}
    {{parent ()}}

    {% endblock%}
  • Overloaded method updateAction in EditController
    ???

from admingeneratorgeneratorbundle.

cedriclombardot avatar cedriclombardot commented on August 16, 2024

No for the file :

edit:
  fields:
    image:
      formType: file
      formOptions: []

And in updateAction

<?php 

///....
    public function updateAction($id)
    {
        //....
            $this->saveObject(${{ builder.ModelClass }});

            // Add this http://symfony.com/doc/current/reference/forms/types/file.html
            $form['image']->getData()->move($dir, $someNewFilename);

            $this->get('session')->setFlash('success', 'The object was successfully saved');

            //....
    }

I'll add a postSave($form, ${{ builder.ModelClass }}) method to ease overriding this action

If you want a preview :

{% block form_image %}
<div class="preview">
            {% if MyModel.image  %}
                       <img src="{{ MyModel.image }}" />
            {% endif %}
</div>
{{ parent() }} {# parent() show the content of the autogenerated block #}
{% endblock %}

For the HTML editor :

{% block form_html %}
{{ parent() }} 
<srcipt>
// The js to load the editor
</script>
{% endblock %}

from admingeneratorgeneratorbundle.

suncat2000 avatar suncat2000 commented on August 16, 2024

Thanks, I'll try your version

from admingeneratorgeneratorbundle.

cedriclombardot avatar cedriclombardot commented on August 16, 2024

I just add a postSave method

from admingeneratorgeneratorbundle.

suncat2000 avatar suncat2000 commented on August 16, 2024

Thank, tomorrow will try to use

from admingeneratorgeneratorbundle.

suncat2000 avatar suncat2000 commented on August 16, 2024

For file field i add uload method in my model (entity).

 ...
 public function upload()
{
    if(null != $this->preview_img){
        $previewImgName = uniqid().'.'.$this->preview_img->guessExtension();
        $this->preview_img->move($this->getUploadRootDir(), $previewImgName);
        $this->preview_img = $previewImgName;
    }
}

...

and overload updateAction in EditController:

...
    if ($form->isValid()) {
        $Text->upload();
        $this->saveObject($Text);
        $this->postSave($form, $Text);

can add method beforeSave(\Entity $val) before calling:

 ...
 $this->beforeSave($Text); 
 $this->saveObject();
 ...

so i can immediately write to the database a new file name

from admingeneratorgeneratorbundle.

cedriclombardot avatar cedriclombardot commented on August 16, 2024

I have added a preSave() method with the same signature than postSave() but i think that you should move the picture only if there is non db exception on save() so i recommend you to calculate the image path on preSave() and to move it on postSave()

from admingeneratorgeneratorbundle.

suncat2000 avatar suncat2000 commented on August 16, 2024

Thanks...
Another little problem, how to make a field file is not required?

from admingeneratorgeneratorbundle.

cedriclombardot avatar cedriclombardot commented on August 16, 2024

set the form option with the yaml parameter formOptions or addFormOptions with the parameter required: false

fields:
  file:
    addFormOptions:
       required: false

from admingeneratorgeneratorbundle.

suncat2000 avatar suncat2000 commented on August 16, 2024

It works, i not properly declared 'require' property

from admingeneratorgeneratorbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.