Author: francois
Date: 2010-04-23 14:15:00 +0200 (Fri, 23 Apr 2010)
New Revision: 29247

Added:
   plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorSchemaRemove.php
   plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPlain.php
Modified:
   plugins/sfPropel15Plugin/trunk/README
   plugins/sfPropel15Plugin/trunk/doc/admin_generator.txt
   plugins/sfPropel15Plugin/trunk/doc/form.txt
   plugins/sfPropel15Plugin/trunk/lib/generator/sfPropelGenerator.class.php
Log:
[sfPropel15PLugin] Added the ability to use "plain" type for form fields, just 
like in symfony 1.2

Modified: plugins/sfPropel15Plugin/trunk/README
===================================================================
--- plugins/sfPropel15Plugin/trunk/README       2010-04-23 09:30:46 UTC (rev 
29246)
+++ plugins/sfPropel15Plugin/trunk/README       2010-04-23 12:15:00 UTC (rev 
29247)
@@ -105,6 +105,7 @@
 ### Filter and Edit forms enhancement
 
 - **YAML widget customization**: The `generator.yml` format was extended to 
allow widget and validator customization directly in YAML, without the need to 
edit a form object. You can also safely omit a field from a `display` list in a 
form definition, without any risk to loose data.
+- **Plain text widget**: If you want to display some data in a form without 
allowing the user to edit it, use the `type: plain` attribute, just like in the 
old days of symfony 1.2. This is very useful for columns managed by the model, 
like `created_at` and `updated_at` columns.
 
 The new options for the `admin15` generator theme are fully documented, and 
illustrated by real life examples, in the 
[`doc/admin_generator.txt`](http://trac.symfony-project.org/browser/plugins/sfPropel15Plugin/doc/admin_generator.txt)
 file in this plugin source code.
 

Modified: plugins/sfPropel15Plugin/trunk/doc/admin_generator.txt
===================================================================
--- plugins/sfPropel15Plugin/trunk/doc/admin_generator.txt      2010-04-23 
09:30:46 UTC (rev 29246)
+++ plugins/sfPropel15Plugin/trunk/doc/admin_generator.txt      2010-04-23 
12:15:00 UTC (rev 29247)
@@ -210,12 +210,12 @@
       }
     }
 
-Now just modify the `filters.display` setting in the `generator.yml` to remove 
the `title` and `isbn` filters, and replace them with the new `full_text` 
filter:
+Now just modify the `filter.display` setting in the `generator.yml` to remove 
the `title` and `isbn` filters, and replace them with the new `full_text` 
filter:
 
     [yaml]
     # in modules/book/config/generator.yml
     config:
-      filters:
+      filter:
         display: [full_text]
 
 The amdin generator looks for a `filterByXXX()` method in the query class, 
where `XXX` is the CamelCase version of the custom filter you add.
@@ -238,7 +238,7 @@
           widgetClass:   sfWidgetFormChoice
           widgetOptions: { choices: { '': '' , male: Male, female: Female } }
 
-To remove the "is empty" checkbox for a given filter, just set the relevant 
option in YAML:
+To remove the "is empty" checkbox for a given filter, just set the relevant 
widget option in YAML:
 
     [php]
     filter:
@@ -246,7 +246,7 @@
         title: 
           widgetOptions: { with_empty: false }
 
-To replace a text input by a textarea in the edit form, change the input type:
+To replace a text input by a textarea in the edit form, change the widget 
class:
 
     [php]
     form:
@@ -254,4 +254,20 @@
         body:
           widgetClass:   sfWidgetFormTextarea
 
-The configuration cascade works as usual for these attributes, so a field 
customization defined under the `form` key affects both the `new` and `edit` 
forms, and a field customization defined under the main `field` key affects all 
forms, including the filter form.
\ No newline at end of file
+The configuration cascade works as usual for these attributes, so a field 
customization defined under the `form` key affects both the `new` and `edit` 
forms, and a field customization defined under the main `field` key affects all 
forms, including the filter form.
+
+Plain fields
+------------
+
+If you want to display some data in a form without allowing the user to edit 
it, use the `type: plain` attribute, just like in the old days of symfony 1.2. 
This is very useful for columns managed by the model, like `created_at` and 
`updated_at` columns:
+
+    [yaml]
+    # in modules/book/config/generator.yml
+    config:
+      edit:
+        display:      [title, author_id, created_at, updated_at]
+        fields:
+          created_at: { type: plain }
+          updated_at: { type: plain }
+
+This displays the field in the edit view, but not in a form input. Submitting 
the form will leave these fields unchanged - or, in the previous example, will 
let the model take care of their values.
\ No newline at end of file

Modified: plugins/sfPropel15Plugin/trunk/doc/form.txt
===================================================================
--- plugins/sfPropel15Plugin/trunk/doc/form.txt 2010-04-23 09:30:46 UTC (rev 
29246)
+++ plugins/sfPropel15Plugin/trunk/doc/form.txt 2010-04-23 12:15:00 UTC (rev 
29247)
@@ -1,7 +1,7 @@
 Form Extensions
 ===============
 
-The `sfPropel15Plugin` provides one widget and two validators, to help build 
and validate forms bound to a Model object.
+The `sfPropel15Plugin` provides two widgets and three validators, to help 
build and validate forms bound to a Model object.
 
 `sfWidgetPropelChoice` and `sfValidatorPropelChoice`
 ----------------------------------------------------
@@ -89,7 +89,6 @@
 * `min`: The minimum number of values that need to be selected (this option is 
only active if multiple is true)
 * `max`: The maximum number of values that need to be selected (this option is 
only active if multiple is true)
 
-
 `sfValidatorPropelUnique`
 -------------------------
 
@@ -119,3 +118,36 @@
 * `primary_key`: The primary key column name in Propel field name format 
(optional, will be introspected if not provided). You can also pass an array if 
the table has several primary keys
 * `connection`: The Propel connection to use (null by default)
 * `throw_global_error`: Whether to throw a global error (false by default) or 
an error tied to the first field related to the column option array
+
+`sfWidgetFormPlain` and `sfValidatorSchemaRemove`
+-------------------------------------------------
+
+To display a field without any possiblity to change its value, no need to use 
a partial field. Just use the `sfWidgetFormPlain` widget to display the value 
in a div. Don't forget to disable the validator on that field, too, using the 
`sfValidatorPass` validator. 
+
+But symfony expects to receive all form fields for binding, including plain 
fields. If the field is not present in the request, symfony uses a null value, 
which may erase the data in the column you want to display. To avoid erasing 
data, use the new `sfValidatorSchemaRemove` to remove plain fields from the 
binding process. This is a post validator, and it expects an array of field 
names in the `fields` option.
+
+Here is an example for `created_at` and `updated_at` columns, that you may 
want to display without allowing their edition:
+
+    [php]
+    class ArticleForm extends BaseArticleForm
+    {
+      public function configure()
+      {
+        // ...
+        $this->widgetSchema['created_at']->setWidget(new sfWidgetFormPlain()));
+        $this->widgetSchema['updated_at']->setWidget(new sfWidgetFormPlain()));
+        $this->validatorSchema['created_at']->setValidator(new 
sfValidatorPass(array('required' => false)));
+        $this->validatorSchema['updated_at']->setValidator(new 
sfValidatorPass(array('required' => false)));
+        $this->mergePostValidator(
+          new sfValidatorSchemaRemove(array('fields' => array('created_at', 
'updated_at')))
+        );
+      }
+    }
+
+**Tip**: If you use the admin generator, setting a field with `type: plain` 
produces the same effect, only in much less code.
+
+    [yaml]
+    edit:
+      fields:
+        created_at: { type: plain }
+        updated_at: { type: plain }
\ No newline at end of file

Modified: 
plugins/sfPropel15Plugin/trunk/lib/generator/sfPropelGenerator.class.php
===================================================================
--- plugins/sfPropel15Plugin/trunk/lib/generator/sfPropelGenerator.class.php    
2010-04-23 09:30:46 UTC (rev 29246)
+++ plugins/sfPropel15Plugin/trunk/lib/generator/sfPropelGenerator.class.php    
2010-04-23 12:15:00 UTC (rev 29247)
@@ -359,6 +359,7 @@
    * Get the code to modify a form object based on fields configuration.
    *
    * Configuration attributes considered for customization:
+   *  * type
    *  * widgetClass
    *  * widgetOptions
    *  * widgetAttributes (same effect as the 'attributes' attribute)
@@ -373,10 +374,14 @@
    *   display: [foo1, foo2]
    *   fields:
    *     foo1: { widgetOptions: { bar: baz } }
-   *     foo2: { widgetClass: sfWidgetFormInputText, validatorClass: 
sfValidatorPass }   
+   *     foo2: { widgetClass: sfWidgetFormInputText, validatorClass: 
sfValidatorPass }
+   *     foo3: { type: plain }  
    * $form->getWidget('foo1')->setOption('bar', 'baz');
    * $form->setWidget('foo2', new sfWidgetFormInputText());
    * $form->setValidator('foo2', new sfValidatorPass());
+   * $form->setWidget('foo3', new sfWidgetFormPlain());
+   * $form->setValidator('foo3', new sfValidatorPass(array('required' => 
false)));
+   * $form->mergePostValidator(new sfValidatorSchemaRemove(array('fields' => 
array('foo3'))));
    * unset($form['foo']);
    * </code>
    *
@@ -394,11 +399,22 @@
     $defaultFieldNames = array_keys($form->getWidgetSchema()->getFields());
     $unusedFields = array_combine($defaultFieldNames, $defaultFieldNames);
     $fieldsets = ($view == 'filter') ? array('NONE' => 
$this->configuration->getFormFilterFields($form)) : 
$this->configuration->getFormFields($form, $view);
+    $plainFields = array();
     
     foreach ($fieldsets as $fieldset => $fields)
     {
       foreach ($fields as $fieldName => $field) 
       {
+        // plain widget
+        if ($field->getConfig('type', false) == 'plain')
+        {
+          $plainFields[]= $fieldName;
+          $customization .= "    \$this->" . $formVariableName . 
"->setWidget('$fieldName', new sfWidgetFormPlain());
+";
+          $customization .= "    \$this->" . $formVariableName . 
"->setValidator('$fieldName', new sfValidatorPass(array('required' => false)));
+";
+        }
+        
         // widget customization
         if (!$widgetConfig = $field->getConfig('widget', array()))
         {
@@ -489,6 +505,13 @@
       }
     }
     
+    // remove plain fields from validation
+    if (!empty($plainFields))
+    {
+      $customization .= "    \$this->" . $formVariableName . 
"->mergePostValidator(new sfValidatorSchemaRemove(array('fields' => " . 
$this->asPhp($plainFields) .  ")));
+";
+    }
+    
     // remove unused fields
     if (!empty($unusedFields))
     {

Added: plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorSchemaRemove.php
===================================================================
--- plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorSchemaRemove.php    
                        (rev 0)
+++ plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorSchemaRemove.php    
2010-04-23 12:15:00 UTC (rev 29247)
@@ -0,0 +1,66 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * sfValidatorSchemaRemove removes some values from the input.
+ * This validator can only be used as a post validator.
+ *
+ * @package    symfony
+ * @subpackage validator
+ * @author     François Zaninotto
+ */
+class sfValidatorSchemaRemove extends sfValidatorSchema
+{
+
+  /**
+   * Constructor.
+   *
+   * @param array  $options   An array of options
+   * @param array  $messages  An array of error messages
+   *
+   * @see sfValidatorSchema
+   */
+  public function __construct($options = array(), $messages = array())
+  {
+    $this->addRequiredOption('fields');
+    $this->addOption('throw_global_error', false);
+    
+    parent::__construct(null, $options, $messages);
+  }
+
+  /**
+   * @see sfValidatorBase
+   */
+  protected function doClean($values)
+  {
+    if (null === $values)
+    {
+      $values = array();
+    }
+
+    if (!is_array($values))
+    {
+      throw new InvalidArgumentException('You must pass an array parameter to 
the clean() method');
+    }
+    
+    foreach ($this->getOption('fields') as $field)
+    {
+      // isset() doesn't work here since the value is null
+      if (array_key_exists($field, $values))
+      {
+        // remove the (empty) value from the list to avoid erasing data
+        unset($values[$field]);
+      }
+    }
+
+    return $values;
+  }
+
+}


Property changes on: 
plugins/sfPropel15Plugin/trunk/lib/validator/sfValidatorSchemaRemove.php
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:keywords
   + "Id Rev Revision Author"
Added: svn:eol-style
   + native

Added: plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPlain.php
===================================================================
--- plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPlain.php             
                (rev 0)
+++ plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPlain.php     
2010-04-23 12:15:00 UTC (rev 29247)
@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ * 
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * sfWidgetFormTextarea represents a textarea HTML tag.
+ *
+ * @package    symfony
+ * @subpackage widget
+ * @author     Francois Zaninotto
+ */
+class sfWidgetFormPlain extends sfWidgetForm
+{
+  /**
+   * @param array $options     An array of options
+   * @param array $attributes  An array of default HTML attributes
+   *
+   * @see sfWidgetForm
+   */
+  protected function configure($options = array(), $attributes = array())
+  {
+    $this->addOption('tag', 'div');
+  }
+
+  /**
+   * @param  string $name        The element name
+   * @param  string $value       The value displayed in this widget
+   * @param  array  $attributes  An array of HTML attributes to be merged with 
the default HTML attributes
+   * @param  array  $errors      An array of errors for the field
+   *
+   * @return string An HTML tag string
+   *
+   * @see sfWidgetForm
+   */
+  public function render($name, $value = null, $attributes = array(), $errors 
= array())
+  {
+    return $this->renderContentTag($this->getOption('tag'), 
self::escapeOnce($value), $attributes);
+  }
+}


Property changes on: 
plugins/sfPropel15Plugin/trunk/lib/widget/sfWidgetFormPlain.php
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:keywords
   + "Id Rev Revision Author"
Added: svn:eol-style
   + native

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to