Code snippets for symfony 1.x


Refine Tags

Snippets tagged "forms"

Converting the form fields in paragraphs with a decorator

I created a reusable object, where I want to display select fields blocked, because this way I display only the value in DIVs, adding an input hidden (or not).

Not resolved the idea of a print version because only a few fields have the option renderer_class.

If someone yells at me to think of something :)


class g1mrWidgetFormPrint extends sfWidgetFormInputHidden
  protected function configure($options = array(), $attributes = array())
    $this->addOption('renderHidden', true);
    $this->addOption('class', 'form_print');
    $this->addOption('break', "<br>\n");
    $this->addOption('print_template', <<<EOF
<div class="%class%">
   parent::configure($options, $attributes);
   * Renders the widget.
   * @param  string $name        The element name
   * @param  string $value       The value selected 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())
    if (is_null($value))
      $value = '';
    $choices = $this->getOption('choices');
    if ($choices instanceof sfCallable)
      $choices = $choices->call();
      if (is_array($choices)) 
        $vc = $value;
        if (!is_array($vc))
         $vc= array($vc);
        foreach ($choices as $k => $array)
          if (is_array($array))
            foreach($array as $key => $o)
              if (in_array(strval($key), $vc))
                 $associated[$key] = $o;
            foreach($choices as $key => $o)
              if (in_array(strval($key), $vc))
                 $associated[$key] = $o;
        $associated = $value;
    $hiddenField = '';
        $hiddenField = parent::render($name, $value, $attributes, $errors);
    return strtr($this->getOption('print_template'), array(
      '%class%'              => $this->getOption('class'),
      '%value%'              => $this->processValue($associated),
      '%hiddenField%'        => $hiddenField
  public function processValue($x)
      return implode($this->getOption('break'),$x); 
      return $x;


    public function setPrintable($field, $renderHidden = true)
            $this->widgetSchema[$field]->setOption('renderer_class', 'g1mrWidgetFormPrint');
            $this->widgetSchema[$field]->setOption('renderer_options', array('renderHidden' => $renderHidden));
    public function setPrintables($renderHidden = true)
        foreach($this->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
            $this->setPrintable($key, $renderHidden);


  public function executeEdit(sfWebRequest $request)
    $this->pedido = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->pedido);
    if($this->pedido->getIsSent() == 1)
by Gilmar Pupo on 2010-10-02, tagged decorator  forms  widget 

Integration of the TinyFCK widget

There are two big open source rich text editors:

Some people prefer the first, some other the second, but there’s a problem for who likes to use TinyMCE: it lacks of a free file manager/uploader.

What we actually need is to have a TinyMCE installation with a free file manager/uploader, thus we take it from FCKEditor.

This is TinyFCK: a TinyMCE with the FCKEditor’s file manager/uploader, nothing less, nothing more.

Installation of TinyFCK rich text editing is described here. You need to download the editor from the project website ( and unpack it in a temporary folder. Copy the tiny_fck/ directory into your project web/js/ directory, and define the path to the library and new rich editor class in settings.yml.

    rich_text_editor_class: TinyFCK
    rich_text_js_dir:  js/tiny_fck

After that create new text editor class file sfRichTextEditorTinyFCK.class.php in your project or application lib/ folder.

 * (c) 2004-2006 Fabien Potencier <>
 * (c) 2007 Eugene Krasichkov <>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 * sfRichTextEditorTinyFCK implements the TinyFCK rich text editor.
 * <b>Options:</b>
 *  - css - Path to the TinyFCK editor stylesheet
 *    <b>Css example:</b>
 *    <code>
 *    / * user: foo * / => without spaces. 'foo' is the name in the select box
 *    .foobar
 *    {
 *      color: #f00;
 *    }
 *    </code>
class sfRichTextEditorTinyFCK extends sfRichTextEditor
   * Returns the rich text editor as HTML.
   * @return string Rich text editor HTML representation
  public function toHTML()
    $options = $this->options;
    // we need to know the id for things the rich text editor
    // in advance of building the tag
    $id = _get_option($options, 'id', $this->name);
    // use tinymce's gzipped js?
    $tinymce_file = _get_option($options, 'tinymce_gzip') ? '/tiny_mce_gzip.php' : '/tiny_mce.js';
    // tinymce installed?
    $js_path = sfConfig::get('sf_rich_text_js_dir') ? '/'.sfConfig::get('sf_rich_text_js_dir').$tinymce_file : '/sf/tinymce/js'.$tinymce_file;
    if (!is_readable(sfConfig::get('sf_web_dir').$js_path))
      throw new sfConfigurationException('You must install TinyFCK to use this helper (see rich_text_js_dir settings).');
    $tinymce_options = '';
    $style_selector  = '';
    // custom CSS file?
    if ($css_file = _get_option($options, 'css'))
      $css_path = stylesheet_path($css_file);
      $css    = file_get_contents(sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.$css_path);
      $styles = array();
      preg_match_all('#^/\*\s*user:\s*(.+?)\s*\*/\s*\015?\012\s*\.([^\s]+)#Smi', $css, $matches, PREG_SET_ORDER);
      foreach ($matches as $match)
        $styles[] = $match[1].'='.$match[2];
      $tinymce_options .= '  content_css: "'.$css_path.'",'."\n";
      $tinymce_options .= '  theme_advanced_styles: "'.implode(';', $styles).'"'."\n";
      $style_selector   = 'styleselect,separator,';
    $culture = sfContext::getInstance()->getUser()->getCulture();
    $tinymce_js = '
  mode: "exact",
  language: "'.strtolower(substr($culture, 0, 2)).'",
  elements: "'.$id.'",
  plugins : "table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,flash,searchreplace,print,paste,directionality,fullscreen,noneditable,contextmenu",
  theme: "advanced",
  theme_advanced_buttons1_add_before : "save,newdocument,separator",
    theme_advanced_buttons1_add : "fontselect,fontsizeselect",
    theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor,liststyle",
    theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
    theme_advanced_buttons3_add_before : "tablecontrols,separator",
    theme_advanced_buttons3_add : "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
  theme_advanced_resizing : true,
  theme_advanced_resize_horizontal : false,
  plugin_insertdate_dateFormat : "%Y-%m-%d",
  plugin_insertdate_timeFormat : "%H:%M:%S",
  file_browser_callback : "tinyFCKFileBrowserCallBack",
  paste_use_dialog : false,
  extended_valid_elements: "img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]",
  apply_source_formatting : true,
  relative_urls: false,
  debug: false
  '.($tinymce_options ? ','.$tinymce_options : '').'
  '.(isset($options['tinymce_options']) ? ','.$options['tinymce_options'] : '').'
  function tinyFCKFileBrowserCallBack(field_name, url, type, win) {
    var connector = "../../filemanager/browser.html?Connector=connectors/php/connector.php";
    var enableAutoTypeSelection = true;
    var cType;
    tinyfck_field = field_name;
    tinyfck = win;
    switch (type) {
      case "image":
        cType = "Image";
      case "flash":
        cType = "Flash";
      case "file":
        cType = "File";
    if (enableAutoTypeSelection && cType) {
      connector += "&Type=" + cType;
    }, "tinyfck", "modal,width=600,height=400");
    if (isset($options['tinymce_options']))
      content_tag('script', javascript_cdata_section($tinymce_js), array('type' => 'text/javascript')).
      content_tag('textarea', $this->content, array_merge(array('name' => $this->name, 'id' => get_id_from_name($id, null)), _convert_options($options)));

Then clear cache:

symfony cc

Once this is done, toggle the use of rich text editing in text areas by adding the rich=true option. You can also specify custom options for the TinyFCK editor using the tinymce_options option.

<?php echo textarea_tag('name', 'default content', 'rich=true size=10x20')) ?>
 => a rich text edit zone powered by TinyFCK
<?php echo textarea_tag('name', 'default content', 'rich=true size=10x20tinymce_options=language:"fr",theme_advanced_buttons2:"separator"')) ?>
=> a rich text edit zone powered by TinyFCK with custom parameters
by Eugene Krasichkov on 2007-12-27, tagged editor  fckeditor  form  forms  helper  rich  tinyfck  tinymce 
(1 comment)

Form validation in AJAX

The example is for a blog. The page that displays a post also proposes an AJAX form to add a comment. We want that when the validation of this form fails, it displays again in the page with an error message, and when the validation succeeds, the form is replaced byu the comment just posted.

The idea is to take advantage of the way the update option of the form_remote_tag() helper works. It accepts an associative array, where you can specify different zones to update in case of success and failure. The only problem is that for Prototype, a failure is a return code other than 2XX. So when we return the form showing the error message again, we need to set the status code to 404, for instance, for Prototype to choose to update the correct zone.

That, plus the usual use of partials here and there, and you have a working solution:

in modules/post/actions/action.class.php

// Display the form
public function executeShow()
  $this->post = PostPeer::retrieveByPk($this->getRequestParameter('post_id'));

in modules/post/templates/showSuccess.php

// Display question detail here
// Beginning of Comment zone
<div id="added_comment" style="display_none"> </div>
<div id="add_comment">
  <?php include_partial('comment/add_comment', array('post' => $post)) ?>

in modules/comment/templates/_add_comment.php

<?php use_helper('Javascript', 'Validation') ?>
<?php echo form_remote_tag(array(
  'url'     => 'comment/add',
  'update'  => array('success' => 'added_comment', 'failure' => 'add_comment'),
  'script'  => true,
  'loading' => "'indicator')",
  'success' => "Element.hide('indicator');'added_comment');Element.hide('add_comment');",
  'failure' => "Element.hide('indicator');",
)) ?>
  <?php echo input_hidden_tag('post_id', $post->getId()) ?>
  <?php echo form_error('body') ?>
  <label for="body">Your comment</label>
  <?php echo textarea_tag('body') ?>
  <?php echo submit_tag('Send') ?>

in modules/comment/validate/add.yml

  post: [body]
  activate:        Yes
    required:      Yes
    required_msg:  You must provide a comment
    validators:    spamValidator
   class:          sfRegexValidator
     match:        No     
     pattern:      /http.*http/
     match_error:  Do not provide more than one URL - It is considered Spam

in modules/comment/actions/action.class.php

public function handleErrorAdd()
  $this->post = PostPeer::retrieveByPk($this->getRequestParameter('post_id'));
  return sfView::ERROR;
public function executeAdd()
  $post = PostPeer::retrieveByPk($this->getRequestParameter('post_id'));
  $comment = new Comment();
  $this->comment = $comment;

in modules/comment/templates/addError.php

<?php include_partial('comment/add_comment', array('post' => $post)) ?>

in modules/comment/templates/addSuccess.php

Your comment has been added:
<div class="comment">
  <?php echo $comment->getBody() ?>

As a bonus, the form is still there after a successful submission (but hidden), so with a few more lines of code, you can still provide a Digg-like "edit comment for the next 60 seconds" feature.

by Francois Zaninotto on 2006-10-16, tagged ajax  forms  validation 

How to check radiobutton after post.

The boolean is set to The third argument.

<?php echo radiobutton_tag('status', 'value1', true) ?>
<?php echo radiobutton_tag('status', 'value2', false) ?>

It write like this.

<?php echo radiobutton_tag("status", "value1", $sf_params->get("status") == 'value1'); ?>
<?php echo radiobutton_tag("status", "value2", $sf_params->get("status") == 'value2'); ?>
by Kota Sakoda on 2006-08-30, tagged forms  view 

How to perform an action when input_date_tag changes.

What is worth noting in the snippet below is the usage of the 'onchange'=> remote_function( ... you can use the remote_function in many places to call other functions ... like in the "update", "complete" etc...

<div id="js_updating">Stand by..</div>
<?php echo form_tag('/module/action', 'method=get class=simpleForm') ?>
Please select the day:
echo input_date_tag('day', 'now', array('rich' => true, 'readonly'=>true, 'onchange'=> remote_function( array(
      'update'   => 'Area To Update (DIV TAG)',
      'url'      => 'MODULE/ACTION',
      'loading'  => "'js_updating')",
      'complete' => "Element.hide('js_updating')"
by Fuad Arafa on 2006-08-23, tagged ajax  forms  javascript 

Rich select_date_tag

select_date_tag is not available in 'rich' version. Waiting for a real enhancement, this is my workaround.

This is the RichDateHelper.php file:

function rich_select_date_tag($name, $value = null, $options = array(), $html_options = array()) {
    $context = sfContext::getInstance();
  if (isset($options['culture']))
    $culture = $options['culture'];
    $culture = $context->getUser()->getCulture();
    // register our javascripts and stylesheets
  $langFile = '/sf/js/calendar/lang/calendar-'.strtolower(substr($culture, 0, 2));
  $jss = array(
    is_readable(sfConfig::get('sf_symfony_data_dir').'/web/'.$langFile.'.js') ? $langFile : '/sf/js/calendar/lang/calendar-en',
  foreach ($jss as $js)
  $js = '
        function updateSelect(cal) {
            var date =;
            var selectMonth = document.getElementById("'.get_id_from_name($name).'_month");
            selectMonth.selectedIndex = date.getMonth();
            var selectDay = document.getElementById("'.get_id_from_name($name).'_day");
            selectDay.selectedIndex = (date.getDate() - 1);
            var selectYear = document.getElementById("'.get_id_from_name($name).'_year");
            selectYear.selectedIndex = (date.getFullYear() - '.$options['year_start'].');
    document.getElementById("trigger_'.$name.'").disabled = false;
            inputField : "'.$name.'_rich_sel_date",
            ifFormat : "%Y-%m-%d",
            button : "trigger_'.$name.'",
            singleClick : true,
            onUpdate : updateSelect,
            showsTime : false,
            range : ['.$options['year_start'].', '.$options['year_end'].'],
            showOthers : false,
            cache : 1,
            weekNumbers : false,
            firstDay : 1
    $html = select_date_tag($name, $value, $options, $html_options);
  // calendar button
  $calendar_button = '...';
  $calendar_button_type = 'txt';
  if (isset($options['calendar_button_img']))
    $calendar_button = $options['calendar_button_img'];
    $calendar_button_type = 'img';
  else if (isset($options['calendar_button_txt']))
    $calendar_button = $options['calendar_button_txt'];
    $calendar_button_type = 'txt';
  if ($calendar_button_type == 'img')
    $html .= image_tag($calendar_button, array('id' => 'trigger_'.$name, 'style' => 'cursor: pointer'));
    $html .= content_tag('button', $calendar_button, array('type' => 'button', 'disabled' => 'disabled', 'onclick' => 'return false', 'id' => 'trigger_'.$name));
  // add javascript
  $html .= content_tag('script', $js, array('type' => 'text/javascript'));
  $html .= '<div id="'.$name.'_rich_sel_date" style="display: inline;"></div>';
  return $html;
by Jacopo Romei on 2006-07-31, tagged date  forms  helper  rich  select 

object_select_tag related classes

When you use the object_select_tag you specify related_class=Author for example. This will return all Authors and use the __toString() method to display the author.

However how do you select a subset of Authors based on some criteria.

To do this you can create a new Peer class that extends your BaseAuthorPeer, for example LiveAuthorPeer.

class LiveAuthorPeer extends BaseAuthorPeer {
    public static function doSelect(Criteria $criteria, $con = null)
        $criteria = new Criteria();
        $criteria->add(AuthorPeer::IS_LIVE, 1);
        return AuthorPeer::doSelect($criteria);

You can the call this new class from you object_select_tag and it will return select options for only those authors with the value of IS_LIVE = 1.

<?php echo object_select_tag($books, 'getAuthorId', array (
  'related_class' => 'LiveAuthors'
)) ?>
by Alistair Stead on 2006-06-20, tagged forms  objects  propel 

turn on form repopulation

add this to your validation file for the action.

eg: indexSuccess.php would have have a file called index.yml in the validate directory for that module if you configured validation.

  activate: on   # activate the form repopulation
    name: test   # name of the form
by Russ Cann on 2006-05-23, tagged forms  repopulation  symfony  validation  yml