Development

#100 ([PATCH] sfValidatorManager::validate method must tolerate array type parameters)

You must first sign up to be able to contribute.

Ticket #100 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

[PATCH] sfValidatorManager::validate method must tolerate array type parameters

Reported by: brujahRg Assigned to:
Priority: critical Milestone: 0.6.3
Component: Version:
Keywords: Cc:
Qualification:

Description

Trying to write a combined date tag helper I have come upon the limitation of this method accepting only single string parameters.

To clarify the problem; below is the output of my combined date tag helper:

  <input type="text" name="birthdate[day]" id="birthdate_day" />
  <input type="text" name="birthdate[month]" id="birthdate_month" />
  <input type="text" name="birthdate[year]" id="birthdate_year" />

and in myDateValidator.class.php:

  ...
  public function execute (&$value, &$error)
  {
    if (is_array($value)) 
    {
      if (isset($value['day'] && is_numeric($value['day'])
  ...

The problem is php throws a notice message and bypasses the below conditional checks while the form is being validated. Which renders the usage of required validation rule for array parameters useless. Not using the required rule also doesn't prevent notice message from appearing since !$datarequired? check is being done after the intolerant strlen($value) == 0 check.

svValidatorManager.class.php (Line 248)

    // now for the dirty work
    if ($value == null || strlen($value) == 0)
    {
      if (!$data['required'] || !$force)
      {
        // we don't have to validate it
        $retval = true;
      }
      else
      {
        // it's empty!
        $error  = $data['required_msg'];
        $retval = false;
      }
    }

It may well be modified to accept a parameter array and check if it has any empty member:

    // now for the dirty work
    if ( $value == null 
	   || (!is_array($value) && strlen($value) == 0)
	   || (is_array($value) && in_array('', $value)) )
    {
      if (!$data['required'] || !$force)
      {
        // we don't have to validate it
        $retval = true;
      }
      else
      {
        // it's empty!
        $error  = $data['required_msg'];
        $retval = false;
      }
    }

Attachments

sfValidatorManager0.patch (0.6 kB) - added by brujahRg on 02/01/06 15:13:16.
patch
sfValidatorManager2.patch (1.5 kB) - added by brujahRg on 02/02/06 17:09:33.
this handles multi dimension arrays correctly

Change History

12/27/05 21:21:51 changed by brujahRg

  • type changed from enhancement to defect.

02/01/06 15:12:25 changed by brujahRg

In mojavi forums i've come upon this discussion for the same problem:

http://forum.mojavi.org/index.php?showtopic=899

Inspired by that solution I've created a better patch (hopefully)

02/01/06 15:13:16 changed by brujahRg

  • attachment sfValidatorManager0.patch added.

patch

02/02/06 17:09:33 changed by brujahRg

  • attachment sfValidatorManager2.patch added.

this handles multi dimension arrays correctly

04/10/06 11:11:13 changed by fabien

  • milestone set to 0.6.3.

06/16/06 04:17:23 changed by slickrick

  • summary changed from sfValidatorManager::validate method must tolerate array type parameters to [PATCH] sfValidatorManager::validate method must tolerate array type parameters.

06/16/06 09:46:47 changed by fabien

  • milestone changed from 0.6.3 to 1.0.0.

06/16/06 09:46:48 changed by fabien

  • milestone changed from 0.6.4 to 0.6.3.

06/21/06 13:56:37 changed by fabien

  • status changed from new to closed.
  • resolution set to fixed.

in r1499

template

<?php echo form_tag('test/index') ?>

<?php echo input_tag('test[foo]', 'foo') ?>
<?php echo input_tag('test[bar]', 'bar') ?>
<?php echo input_tag('test[foobar]', 'foobar') ?>

<?php echo submit_tag() ?>

validate

methods:
  post: [test]

names:
  test:
    required: true
    validators: myValidator

myValidator:
  class: myValidator

myValidator

<?php

class myValidator extends sfValidator
{
  public function execute (&$value, &$error)
  {
    $error = $value['foo'].' '.$value['foobar'];

    return false;
  }
}