Code snippets for symfony 1.x


Snippets by user excessive demon

unset all not needed form widgets

Common Probem.

You have a large and small form. For example a contact form.

Form orginal includes 50 fields and your small form is extend form the orginal form and includes 15 fields. normaly you have to unset very 35 field in the small one.

But you could also write this lines:

class ContactSimpleForm extends ContactForm
  public function configure()
    $this->wantedFields = array(
    foreach ($this as $fieldName => $widget){
        if (!in_array($fieldName, $this->wantedFields)){
by excessive demon on 2009-09-18, tagged form  unset  validator  widget 

Change app by routing class

Create a file into lib/routing/sfChangeAppRoute.class.php and paste :

class sfChangeAppRoute extends sfRoute{
    public function generate($params, $context = array(), $absolute = true)
        return parent::generate($params, $context, $absolute);
    return $url;

Now into your routing.yml

  url: /application/:app
  params: {  }
  class:  sfChangeAppRoute

And after that you could use the route @app?app=MY_other_app

by excessive demon on 2009-08-26, tagged application  routing  url 
(1 comment)


A simple filter for Symfony 1.2.* which embed a js per request named after /js/modules/:module_:action.js rule.

class JsModuleActionFilter extends sfFilter
    public function execute($filterChain)
        if ($this->isFirstCall()) {
            $params = $this->getContext()->getRequest()->getRequestParameters();
            //gets a module action specific js file
            $this->getContext()->getResponse()->addJavascript('modules/' . $params['module'] . '_' . $params['action'] , 'last');
by excessive demon on 2009-03-15, tagged 12  filter  peytz 

Age Validation

Say you wanted to validate that someone was over 18 years of age, its acutally quite simple once you know what your doing, but it took me a while to work out all the bits, so i thought i would put it in here to save some one a bit of time...

on the template you have

  <label for="date_of_birth">Date of Birth:</label>
  <?php echo form_error('day');  ?>
  <?php echo form_error('month');  ?>
  <?php echo form_error('year');  ?>
  <?php echo select_day_tag('day', $sf_params->get('day')) ?>
  <?php echo select_month_tag('month', $sf_params->get('month')) ?>
  <?php echo select_year_tag('year', $sf_params->get('year'),array('year_start'=>'1900','year_end'=>date("Y"))) ?>

in signup.yml (due to the way i built my form, you dont actually need to validate the day and month, since there is no way to leave them empty, but i have put the code in for completness)

  post: [day, month, year, gender]
    required:     true
    required_msg: You must verify your age.
    required:     true
    required_msg: You must verify your age.
    required:     true
    required_msg: You must verify your age.
    validators:   ageValidator
    class:         myAgeValidator
      too_young_error: You must be over 18 to enter.

now in /lib/AgeValidator.class.php, we put the code to check the dates.

class myAgeValidator extends sfValidator
  public function initialize($context, $parameters = null)
    // initialize parent
    // set defaults
    $this->setParameter('too_young_error', 'Invalid input');
    return true;
  public function execute(&$value, &$error)
// get the passed values for the date fields
    $day_param = sfContext::getInstance()->getRequest()->getParameter('day');
    $month_param = sfContext::getInstance()->getRequest()->getParameter('month');
    $year_param = sfContext::getInstance()->getRequest()->getParameter('year');
    $min_age=strtotime("-18 YEAR");
    $entrant_age= strtotime( $year_param . "-" . $month_param . "-" . $day_param);
# just in case ;-)
if ($entrant_age <$min_age ){
    return true;
$error = this->getParameter('too_young_error');
return false;
by excessive demon on 2008-04-16, tagged age  dateofbirth  validation