Quellcode durchsuchen

Add help message for form element

Thomas Rabaix vor 14 Jahren
Ursprung
Commit
15a587b377

+ 15 - 1
Admin/BaseFieldDescription.php

@@ -107,6 +107,11 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
      */
     protected $associationAdmin;
 
+    /**
+     * @var string the help message to display
+     */
+    protected $help;
+
     /**
      * set the field name
      *
@@ -189,7 +194,6 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
      */
     public function setOptions(array $options)
     {
-
         // set the type if provided
         if (isset($options['type'])) {
             $this->setType($options['type']);
@@ -439,4 +443,14 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
     {
        return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
     }
+
+    public function setHelp($help)
+    {
+        $this->help = $help;
+    }
+
+    public function getHelp()
+    {
+        return $this->help;
+    }
 }

+ 11 - 0
Form/FormMapper.php

@@ -201,4 +201,15 @@ class FormMapper
     {
         return $this->formBuilder->create($name, $type, $options);
     }
+
+    public function setHelps(array $helps = array())
+    {
+        foreach($helps as $name => $help) {
+            if ($this->admin->hasFormFieldDescription($name)) {
+                $this->admin->getFormFieldDescription($name)->setHelp($help);
+            }
+        }
+
+        return $this;
+    }
 }

+ 25 - 17
Resources/doc/reference/form_field_definition.rst

@@ -26,11 +26,17 @@ Example
         {
             // equivalent to :
             $formMapper
-              ->add('author', array(), array('edit' => 'list'))
-              ->add('enabled')
-              ->add('title')
-              ->add('abtract', array(), array('required' => false))
-              ->add('content');
+                ->add('author', array(), array('edit' => 'list'))
+                ->add('enabled')
+                ->add('title')
+                ->add('abtract', array(), array('required' => false))
+                ->add('content')
+
+                // you can define help messages like this
+                ->setHelps(array(
+                   'title' => $this->trans('help_post_title')
+                ));
+
         }
     }
 
@@ -40,21 +46,22 @@ Example
     field. This can be an issue for HTML5 browsers as they provide client-side
     validation.
 
+
 Types available
 ---------------
 
-- array
-- boolean
-- choice
-- datetime
-- decimal
-- integer
-- many_to_many
-- many_to_one
-- one_to_one
-- string
-- text
-- date
+    - array
+    - boolean
+    - choice
+    - datetime
+    - decimal
+    - integer
+    - many_to_many
+    - many_to_one
+    - one_to_one
+    - string
+    - text
+    - date
 
 If no type is set, the Admin class will use the one set in the doctrine mapping
 definition.
@@ -85,6 +92,7 @@ model definition).
                 ->add('cdnIsFlushable', array('required' => false))
                 ->add('description', array('required' => false))
                 ->add('copyright', array('required' => false))
+
                 // add a custom type, using the native form factory
                 ->addType('binaryContent', 'file', array('type' => false, 'required' => false));
         }

+ 6 - 4
Resources/public/css/layout.css

@@ -19,7 +19,6 @@ table.sonata-ba-list td img{
     vertical-align: bottom
 }
 
-
 table.sonata-ba-list td.sonata-ba-list-label {
     width: 350px;
 }
@@ -30,21 +29,18 @@ td.pager ul {
     margin: 2px;
     margin-left: auto;
     margin-right: auto;
-
 }
 
 td.pager ul li {
     float: left;
 }
 
-
 td.pager ul li a {
     border: 1px solid #cccccc;
     padding: 4px;
     margin: 2px;
 }
 
-
 div.sonata-ba-modal-edit-one-to-one td.sonata-ba-list-field-batch,
 div.sonata-ba-modal-edit-one-to-one div.sonata-ba-list-actions,
 div.sonata-ba-modal-edit-one-to-one th.sonata-ba-list-field-header-batch
@@ -83,4 +79,10 @@ th.sonata-ba-list-field-header-order-desc.sonata-ba-list-field-order-active:hove
 
 th.sonata-ba-list-field-header-order-asc.sonata-ba-list-field-order-active:hover {
     background: url(../famfamfam/bullet_arrow_up.png) no-repeat center left;
+}
+
+em.sonata-ba-field-help {
+    display: block;
+    color: #999;
+    margin-bottom: 10px;
 }

+ 4 - 0
Resources/views/CRUD/base_standard_edit_field.html.twig

@@ -23,6 +23,10 @@ file that was distributed with this source code.
 
         {% block field %}{{ form_widget(field_element) }}{% endblock %}
 
+        {% if field_description.help %}
+            <em class="sonata-ba-field-help">{% block help %}{{ field_description.help }}{% endblock %}</em>
+        {% endif %}
+
         <div class="sonata-ba-field-error-messages">
             {% block errors %}{{ form_errors(field_element) }}{% endblock %}
         </div>