Mastering symfony 1.4 form widgets

Usage examples of lots of symfony form widgets.

sfWidgetFormDateTime

$months = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
$years = range(2010, date("Y") +5);
$years_list = array_combine($years, $years);
$this->widgetSchema["data"] = new sfWidgetFormDateTime(array("date" => array("months" => $months, "format"=>" %day% / %month% / %year%", "years" => $years_list)));
$this->setValidator("data" , new sfValidatorDateTime(array("required" => false)));
 

sfWidgetFormChoice

$cultures = array("en" => "en" , "pt" => "pt");
$this->widgetSchema["culture"] = new sfWidgetFormChoice(array("choices" => $cultures));
$this->widgetSchema["culture"] = new sfValidatorChoice(array("choices" => $cultures, "empty_value" => "", "required" => true));
 

post validator

// post validator
$this->validatorSchema->setPostValidator(
  new sfValidatorCallback(array("callback" => array($this, "checkSomething")))
);

public function checkSomething($validator, $values)
{    
  if ($values["fase_id"] == 5)
  {      
    $error = new sfValidatorError($validator , "Cannot use fase_id = 5");
    throw new sfValidatorErrorSchema($validator, array("ingresso_preco" => $error));    
  }

  return $values;
}
 

embeding i18ned forms

// embeding i18ned forms
// http://symfony.com/legacy/doc/jobeet/1_2/en/19?orm=Propel
$this->embedI18n(array("en", "pt"));
$this->widgetSchema->setLabel("en", "English");
$this->widgetSchema->setLabel("pt", "Português");

//http://stackoverflow.com/a/1831580/597060
$mce_config = "valid_elements: "*[*]", cleanup: false, inline_styles: true";
$this->widgetSchema["en"]["texto"] = new sfWidgetFormTextareaTinyMCE(array("config" => $mce_config));
$this->widgetSchema["pt"]["texto"] = new sfWidgetFormTextareaTinyMCE(array("config" => $mce_config));
 

handling files

// handling files
$this->setWidget("cover_image", new sfWidgetFormInputFileEditable(
    array(
        "edit_mode" => false,
        "with_delete" => false,
        "file_src" => "/uploads/cover_image/".$this->getObject()->getCoverImage(),
    )
));
$this->setValidator("cover_image", new sfValidatorFile(
    array(
        "max_size"             => 1000000,
        "mime_types"           => "web_images", //you can set your own of course
        "path"                 => sfConfig::get("sf_web_dir")."/uploads/cover_image/",
        "required"             => false,
        "validated_file_class" => "sfValidatedFileCoverImage"
    )
));

// lib/validator/sfValidatedFileCoverImage.class.php
class sfValidatedFileCoverImage extends sfValidatedFile {

  public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) {

    // let the parent class save the file and do what it normally does
          $saved = parent::save($file, $fileMode, $create, $dirMode);

    /* Here we will put all our custom logic. Say, to create a thumbnail,
     or maybe manipulate it in whatever way you see fit.
    There are many possibilities here, so see the extended
    class sfValidatedFile in the Symfony 1.4 API Documentation
    to get a better idea about what you can manipulate and extend */

    $img = new sfImage(sfConfig::get("sf_web_dir")."/uploads/cover_image/".$saved, "image/jpg");
    $img->thumbnail(1105,456);
    $img->setQuality(90);
    $img->save();

    // return the saved file as normal
    return $saved;
  }
}
 

sfWidgetFormInputCheckbox

$this->widgetSchema["use_name"] = new sfWidgetFormInputCheckbox();
$this->setValidator("use_name" , new sfValidatorBoolean(array("required" => false)));
 

sfWidgetFormInputPassword

$this->widgetSchema["pass"] = new sfWidgetFormInputPassword();
$this->widgetSchema["pass"]->setLabel("Password");
$this->setValidator("pass" , new sfValidatorString(array("min_length" => 6, "required" => true), array("required" => "Password field required." , "min_length" => "Password field shoud have 6 caracters at least.")));
 

sfWidgetFormInputText – sfValidatorUrl

$this->widgetSchema["website_url"] = new sfWidgetFormInputText();
$this->setValidator("website_url", new sfValidatorUrl(array("required" => false), array("invalid" => "Invalid Website Url")));
 

sfWidgetFormInputText – sfValidatorString

$this->widgetSchema["text"] = new sfWidgetFormInputText();
$this->setValidator("text", new sfValidatorString(array("max_length" => 200, "required" => false)));
 

sfWidgetFormInputText – sfValidatorInteger

$this->widgetSchema["number"] = new sfWidgetFormInputText();
$this->setValidator("number", new sfValidatorInteger(array("min" => -128, "max" => 127)));
 

sfWidgetFormInputHidden – sfValidatorPropelChoice

// propel fk hidden
$this->widgetSchema["user_id"] = new sfWidgetFormInputHidden();
$this->setValidator("user_id", new sfValidatorPropelChoice(array("model" => "User", "column" => "id")));
 

sfWidgetFormPropelChoice – sfValidatorPropelChoice

// propel fk select choice
$this->widgetSchema["user_id"] = new sfWidgetFormPropelChoice(array("model" => "User", "add_empty" => false));
$this->setValidator("user_id", new sfValidatorPropelChoice(array("model" => "User", "column" => "id")));
 

Suggestions? Always welcome.