开发者

Zend framework - addPrefixPath and namespaces

I tried to use $element->addPrefixPath() to load my custom validator but i开发者_运维百科t would not find the class until I added a namespace autoload to the ini file.

I thought the whole point of addPrefixPath was to only load the validator class for the form and not the whole application. Is this correct?


It depends upon how you invoke the validator and attach it to the element:

  1. Using an instance
  2. Using "abbreviated" format (my own terminology)

As an instance:

$validator = new My_Validate_SomeValidator(array(
    'option1' => 'value1',
    'option2' => 'value2',
));
$element->addValidator($validator, true);

In this case, you are instantiating the validator yourself. So, adding My_ as an autoloader namespace is required.

Alternatively, using the "abbreviated" format:

$element->addValidator(array('SomeValidator', array(
    'option1' => 'value1',
    'option2' => 'value2',
), true);

or

$element->setValidators(array(
    array('EmailAddress', true),
    array('SomeValidator', true, array(
        'option1' => 'value1',
        'option2' => 'value2',
    ),
));

In this case, you are giving the element only an abbreviated name - 'SomeValidator' - for the validator, implicitly expecting the element to handle the instantiation. So it makes sense that the element needs to be given some namespace/path information so it can do the job.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜