Zum Inhalt springen

Typo3 6.2 @validate not working Problem 2

Erstellt am 10. April 2015 · 3 Antworten · letzte Antwort am 18. August 2015

Tags: TYPO3 6.x, Frage

ironpfu ·

Hallo,
bzugnehmend auf mein Post vom 09.04:"Typo3 6.2 @validate not working"
scheitere ich nun leider am nächsten Validator:
Im Model:

	/**
	 * picture
	 *
	 * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference 
         * @validate PCON\Hbm\Domain\Validator\PictureValidator
         * @validate NotEmpty
         * 
	 */
	protected $picture;

im Validator:

namespace PCON\Hbm\Domain\Validator;

class PictureValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
		

    public function isValid($value) {
        

        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($value);
        }
}

Der Validator wird nie ausgeführt.
Komisch ist jedoch das der NotEmpty Validator von Extbase ausgeführt wird.
Kann mit jemand weiterhelfen?

ironpfu ·

Hi ich bin ein wenig weitergekommen.
Mein Denkfehler war, dass ich die FileReference validiere, welche in diesem Fall ja immer NULL ist...
Ich möchte jedoch den FileUpload validieren.
Folgende steht nun im Controller:

 protected function initializeActionMethodValidators() {
            parent::initializeActionMethodValidators();
            
                 if ($this->actionMethodName == 'newAction' || $this->actionMethodName == 'editAction') {
                        $this->controllerContext = $this->buildControllerContext();
                        // Validate File Upload
                         $validator = $this->objectManager->create('PCON\Hbm\Domain\Validator\PictureValidator');
                         $error = $validator->validate($_FILES['tx_hbm_search'])->getErrors();

                        /* Set Form Errors manually  - get results from property mapper and add new errors */
                        $result = $this->getControllerContext()->getRequest()->getOriginalRequestMappingResults();
                        foreach ($error as $key => $value) {
                            //$result->addError($value);
                            $result->forProperty('picture')->addError($value);
                        }
                        //\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($result);
                        $this->getControllerContext()->getRequest()->setOriginalRequestMappingResults($result);
                }
            
        }

und hier die Validator Klasse:

/**
 * The Validator for Image Fields
 * Usage: @validate PCON\Hbm\Domain\Validator\IsImageValidator
 *
 */
class PictureValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
    
    protected $acceptsEmptyValues = TRUE;
    /**
     * Checks if the property is a FileReference
     *
     * If at least one error occurred, the result is FALSE.
     *
     * @param mixed $value The value that should be validated
     * @return boolean TRUE if the value is valid, FALSE if an error occured
     */
    public function isValid($value) {
             
        if ($value['name']['picture'] === NULL || $value['name']['picture'] === '') {
            $this->addError(
                    $this->translateErrorMessage(
                                    'picture.null',
                                    'ubm'
                            ), 1262341473);
            return FALSE;
        }
        else
        {
            $fileType = '';
            switch ($value['type']['picture']) {
                case "image/gif":
                    $fileType ='gif';
                    break;
                case "image/jpeg":
                     $fileType ='jpeg';
                    break;
                case "image/jpg":
                     $fileType ='jpg';
                    break;
                case "image/png":
                     $fileType ='png';
                    break;
                case "image/bmp":
                     $fileType ='bmp';
                    break;
            }

            $allowedFileTypes = explode(',', $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']);
            if (!in_array($fileType, $allowedFileTypes)) {
                $this->addError(
                        $this->translateErrorMessage(
                                    'picture.filetype',
                                    'ubm'
                            ), 1262341475);
                return FALSE;
            }
        }
        return TRUE;
    }

}

Die Validierung läuft und es kommen auch entsprechende Error Message ...
Einziger ENTSCHEIDENDER Schönheitsfehler:
Die Argumente werden an die CreateAction weitergegeben und gespeichert.
Kann mir jemand weiterhelfen?

Fox1990 ·

Hi,

ich glaube ich habe gerade das gleiche Problem wie du und zwar möchte ich auch einen FileUpload validieren (http://stackoverflow.com/questions/31597749/how-can-i-validate-a-file-upload-field-in-typo3), wie du schon geschrieben hast, bringt die Anwendung von @validate Annotationen auf die FileReference recht wenig, da diese initial immer null ist.

Nun bin ich auf deinen Beitrag gestoßen und habe gesehen das du die Validierung durch die Funktion initializeActionMethodValidators manuell an die property gebunden hast. Also habe ich diese Funktion auch in meinem Controller eingebaut, allerdings werden bei mir keine Fehlermeldungen ausgegeben, aber wenn ich das $result debugge sehe ich das die Fehlermeldung innerhalb des $results gesetzt ist, aber dennoch wird der Formular submit ausgeführt und ich erhalte die Meldung:

Exception while property mapping at property path ""😛HP Catchable Fatal Error: Argument 1 passed to Fox\Example\Domain\Model\Data::setImage() must be an instance of TYPO3\CMS\Extbase\Domain\Model\FileReference, null given...

Hast du auch dieses Problem?