开发者

Using Zend_Form input in validation

I have a form for uploading a CSV file. It ask开发者_运维技巧s for the delimiter and the CSV file:

public function init()
    {
        $this->setName('UploadCsvForm');

        $delimiter = new Zend_Form_Element_Text('delimiter');
        $delimiter->setLabel('Delimiter')
                  ->setValue(',')
                  ->setAttrib('size', 2);

        $csv = new Zend_Form_Element_File('csvFile');
        $csv->setLabel(
            'Pick a CSV file to upload')
            ->setRequired(true)
            ->addValidator('Count', false, 1)
            ->addValidator('Extension', false, 'csv')
            ->addValidator('Size', false, 102400)
            ->setDestination('/tmp');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Upload bestand');

        $this->addElements(array($delimiter, $csv, $submit));
    }

I have a custom CSV Validate file, that extends Zend_Validate_Abstract. It works, but I want to be able to validate my inputted form CSV file with it. I can add something like this, but as the $delimiter isn't set yet, it won't work.

->addValidator(new Custom_Validate_Csv(array('name', 'postcode'), $delimiterString), false)

Any ideas? Thanks.


When Zend_Form calls isValid() on its elements, it passes a second parameter $context containing all the form values.

So, in your custom validator, the isValid() method should look like:

public function isValid($value, $context)
{
     $delimiter = $context['delimiter'];

     // continue with your validation using the delimeter
     // ...
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜