Symfony2 forms interpret blank strings as nulls

前端 未结 7 2331
温柔的废话
温柔的废话 2021-02-19 09:12

I have a Symfony2 form with a variety of fields, including one optional text field called recap.

This recap field saves perfectly when there\'s

7条回答
  •  时光取名叫无心
    2021-02-19 09:26

    True, lost many hours on it ;-( the "transformation" is hardcoded in component Form.php line 1113 !

    private function viewToNorm($value)
    {
        $transformers = $this->config->getViewTransformers();
    
        if (!$transformers) {
            return '' === $value ? null : $value;
        }
    
        for ($i = count($transformers) - 1; $i >= 0; --$i) {
            $value = $transformers[$i]->reverseTransform($value);
        }
    
        return $value;
    }
    

    In my opinion, its a big error (because is the role of DataTransformer, not Form). So the solution is to create your own DataTransformer and associate it to the text type. Currently I am losing so much time with Symfony2, always seeking in the sources these kind of little hack ;-(

提交回复
热议问题