daYamlEditorPlugin - 1.0.0

Yaml edition form generator

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

daYamlEditorPlugin

This plugin allow you to easily create a Yaml edition form. Just define the file(s) and the field(s) and that's done. You can edit any Yaml file (your app.yml, or your customAwesomeConfig.yml, even both in the same form), and use all the sfValidator options you want.

Important notice

Use this plugin at your own risk, if you dont "ignore" the yaml files in your rsync and subversion settings, all your online modifications will be lost at your next deployement. So I suggest to add your edited yamls in the rsync_exclude.txt file and off course in your backup strategy.

Important notice 2

All the Yamls files edited will lose their comments

Installation

  • Install the plugin (download the package, or use a svn:externals)

    http://svn.symfony-project.com/plugins/daYamlEditorPlugin/trunk/
    
  • Active the plugin in your ProjectConfiguration

    $this->enablePlugins('aNicePlugin', 'daYamlEditorPlugin');
  • Enable the daYamlEditor module in your settings.yml

    all:
      .settings:
        enabled_modules:      [default, daYamlEditor]
  • Create your daYamlEditor.yml file and save it in one of your config/ dir.

    all:
      myaboutconfigform:              # Add this block for each Yaml. It's also the name used create specific form
        file: %SF_CONFIG_DIR%/app.yml # Path of the Yaml file to parse, can be anywhere, must be writable
        fields:
          sitename:                   # The unique name of the field, must be unique
            label: Nom du site
            path: "all/sitename"      # Path to the var in the yaml file (levels separated by "/")
            type: string              # Can be integer, number, string, boolean, email
            help: Min 10 char.        # A text helper can be set
            validate:                 # Validator options
              required: true          # Any option can be add here
              min_length: 10
            message:                  # Any message can be override here
              required: Le nom du site est obligatoire
     
          from:
            label: Expéditeur
            path: "all/mailer/from"
            type: email
            help: L'email utilisé par le site
            message:
              invalid: L'email doit être un email valide

    As you can see, you can define field for each env and use config var in the file (%VAR%). Messages and validates are optionals. The allowed types are :

    • string
    • number (float allowed)
    • integer
    • email
    • boolean

    You can also define unexisting path, the value will be add to the yaml file. Take a look at the config/daYamlEditor.yml file in the plugin, it's a great exemple.

  • Make your yaml files writtable

    $ chmod +rw ./config/app.yml
    
  • Clear you cache

    $ symfony cc
    
  • Enjoy your new form ! Navigate to the daYamlEditor module.

Create multiple forms

  • If you want to separate your fields in multiple forms, you just have to create a new form and say to it witch config block to use.

    class myYamlEditorForm extends daYamlEditorForm
    {
      protected $daYamlEditorName = "myaboutconfigform";
     
      public function configure()
      {
        parent::configure();
        // You can override the generated form here
      }
    }

    If $daYamlEditorName is null, all the blocks are used (it's the default behavior of daYamlEditorForm).

  • The next step is to build your form action :

    public function executeIndex(sfWebRequest $request)
    {
      $this->form = new myYamlEditorForm();
     
      if ($request->isMethod('post'))
      {
        $this->form->bind(
          $request->getParameter($this->form->getName())
        );
        if ($this->form->isValid())
        {
          if ($this->form->save())
          {
            // Do what you want, the config file is saved
          }
        }
      }
    }

    You can also override daYamlEditorActions, it's up to you.

Tips

  • Clear the cache after updating a config file

    If you need to clear the cache after edit some yaml (that's sound obvious), you will need to do it manualy. You can connect some code to the daYamlEditor.save event :

    $this->dispatcher->connect('daYamlEditor.save', array($this, 'clearCache'));

    (the first param of the sfEvent object will be the name of the block, or null). Read the symfony documentation to do it.

    The quick and dirty way for clearing the cache :

    sfToolkit::clearDirectory( sfConfig::get('sf_cache_dir').'/frontend/prod/template' );
    sfToolkit::clearDirectory( sfConfig::get('sf_cache_dir').'/frontend/prod/config' );
  • Remove the "Doctrine" style on my form

    By default, the form markup is close to the Doctrine Admin Generator one, thanks to the sfWidgetFormSchemaFormatterDaYamlEditor formatter. That's allow you to have consistant backend style. To remove this behavior, you can call this method :

    $this->widgetSchema->setFormFormatterName('table');
    // or
    $form->getWidgetSchema()->setFormFormatterName('table');

    And override the modules/daYamlEditor/templates/indexSuccess.php file with your own HTML.

  • Secure the module

    If you use sfGuard (you should), add some credentials restrictions on the daYamlEditor module :

    daYamlEditor:
      is_secure: true
      credentials: admin

TODO

  • Write unit and fonctionnal test
  • Allow array editing (ability to add multiple value to a yaml key)