SiwappConditionalValidator?

This is a validator that checks a set of fields only if certain user-defined field is true

Source Code

http://dev.siwapp.org/projects/siwapp/browser/trunk/lib/validator/SiwappConditionalValidator.class.php

Validator Constructor

The constructor takes the following options:

  • 'control_field' (string) The field that determines if the validator defined at the "validator_schema" option is to be run. usually a checkbox. Unless 'callback' option specified, it's passed through a sfValidatorBoolean.
    • If the result is true, the validation occurs.
    • Otherwise, the validation defined in "validator_schema" doesn't occur.
  • 'validator_schema' (sfValidatorSchema) The validatorschema that contains the validators to be run in case the check run on control_field is proven to be true.
  • 'callback' (string) [optional] Function to be executed, with the value of "control_field" as an argument, to check if the validation defined at 'validator_schema' option is to occur. If defined, no sfValidatorBoolean is run.

An Example

class testForm extends sfForm 
{
....
  public function configure()
  {
    ....             // usual form configuration stuff
    ....

    /* We set the individual validators for the form fields, as usual */
    $this->setValidators(array(
      .... 
      ....
      'enabled'      =>   new sfValidatorBoolean();
      ...
      'period'       =>   new sfValidatorInteger();
      'period_type'  =>   new sfValidatorChoice(....);
      ....
    ));

    /* We define the set of validators to be validated according to the "control_field" field */
    $vd = new sfValidatorSchema(array(
                                  'period_type'=> $this->getValidator('period_type'),
                                  'period' => $this->getValidator('period')));
    ....
    ....

    /* Finally, we call our conditional validator. The 'enabled' field will be the "control_field". 
    If the user checks the "enabled" checkbox, the validators set defined previously at the $vd object
    will be run. otherwise, they won't */

    $this->validatorSchema->setPostValidator(new SiwappConditionalValidator(array(
                                                                          'control_field'  =>'enabled',
                                                                          'validator_schema'=>$vd)
                                                                        )
                                            );
    ...
    }
  ...
}

In this example, we call the "SiwappConditionalValidator?" with the following arguments:

  • the first option is "enabled" . this is the name of the "control_field". if the form is submitted with that field setted in a way that sfValidatorBoolean returns true, the validator_schema defined in the second argument will be run.
  • the second option is $vd, being $vd a sfValidatorSchema object, with two single field=>validator pairs. this is the set of validators that will be run if "enabled" is true.

SiwappValidatedFile?

This is a class for the SiwappValidatorFile? to return when its validation is done. It implements an extra "canSave()" method to check for writeability of the file.

Source Code

http://dev.siwapp.org/projects/siwapp/browser/trunk/lib/validator/SiwappValidatedFile.class.php

Class Details

The class simply extends the standard "sfValidatedFile" class. it only add a new method: canSave()

The method takes the following arguments:

  • string $file The filename of relative or absolute path. If the path is relative, it expects to have the path declared somewhere before (when invoking the sfValidatorFile, by instance).
  • boolean $create (optional) . Whenever the destination directory should be created, in case it's not there. True by default.
  • int $dirMode (optional) . The mode to use when creating the destination directory (if it needs to be created). 0777 by default.

And it returns the full-path filename on success.

It throws two kind of exceptions:

  • RuntimeException? : when it can't figure out the "path"
  • Exception: when some of these three cases takes place
    • The destination directory is not writable
    • The destination directory needs to be created, and it can't be done
    • The destination directory is not a directory.

An Example

This would be (roughly) the code to use inside the form declaration.

class testForm extends sfForm 
{
....
  public function configure()
  {
    ....             // usual form configuration stuff
    ....

    /* We set the individual validators for the form fields, as usual */
    $this->setValidators(array(
      .... 
      ....
      ...
      /* when defining sfValidatorFile, we instruct him to return -as a "cleaned" value- 
         a SiwappValidatedFile object */

      'my_file'       =>    new sfValidatorFile(array(                          
                                     ...
                                     'validated_file_class'=>'SiwappValidatedFile'
                                     ...
                                     )),
      ....
    ));

    /* We define a validator schema to use as post validator later .
       It will received a SiwappValidatedFile object. In this case, we'll use a
       callback validator */

    $vd = new sfValidatorSchema(array(
                                  'my_file'=> new sfValidatorCallback(
                                         array(
                                           'callback'=>'checkLogo'
                                           ),
                                         array(
                                               'invalid' => 'Can\'t write to the destination directory'
                                               )
                                         )
                ),array('allow_extra_fields'=>true));

    
    $this->validatorSchema->setPostValidator($vs);
    ....
    ....

    }
  ...
}

In this particular case, we need to define a "checkLogo" function somewhere in the system where symfony can find it, so it can be used by the sfValidatorCallback validator. It will receive a "SiwappValidatedFile?" as the "value" to validate, so it can use its "canSave()" method to check whenever the destination directory is ok.

  /* the sfValidatorCallback, will pass the $validator object and the value to be validated ($logo, in this case).*/
  /* the $logo arg is a SiwappValidatedFile object */
     
  function checkLogo(sfValidatorCallback $validator, SiwappValidatedFile $logo)
  {
    ....
    try
    {
      $logo->canSave($my_files_path.'/'.$logo->generateFilename());
    }
    catch(Exception $e)
    {
      $validator->setMessage('invalid',$e->getMessage());
      throw new sfValidatorError($validator,'invalid');
    }
    return $logo;
  }