Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony form builder中对象内部的函数_Php_Forms_Function_Symfony - Fatal编程技术网

Php symfony form builder中对象内部的函数

Php symfony form builder中对象内部的函数,php,forms,function,symfony,Php,Forms,Function,Symfony,如果我尝试以下方法,我不知道为什么会出错: $builder ->add('product', EntityType::class, array( 'data' => $options['product'], 'placeholder' => 'Wyszukiwarka', 'mapped' => false, 'multiple' => false, 'class' => Products::class, 'attr' => [ 'class'

如果我尝试以下方法,我不知道为什么会出错:

$builder
->add('product', EntityType::class, array(
'data' => $options['product'],
'placeholder' => 'Wyszukiwarka',
'mapped' => false,
'multiple' => false,
'class' => Products::class,
'attr' => [
    'class' => 'chosen-select order_product',
    'data-placeholder'=>'Wybierz produkt',
    'single_price' => function ($product) {
       $cena = $product->getJson()["products"]["price"];
      dump($cena);
      return $cena;
      }
  ],

'choice_label' => function ($product) {
return  ''.$product->getJson()["products"]["name"] .' | Stan Magazynowy: '.$product->getJson()["products"]["stock"].'';
},
'label' => 'Wybierz produkty'

))

这样,“CouthyLable”中的函数是完美的,为什么我不能在AtTo中这样做,它包含了这个输入的额外属性。 我得到这个错误:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Closure could not be converted to string").

为什么attr对象内部的函数不工作?

您使用了错误的配置选项。
attr
选项不接受回调。在您的情况下,要根据每个选项的值添加属性,您需要使用中的
choice\u attr

示例实现可能如下所示:

$builder->add('attending', ChoiceType::class, array(
    // [..]
    'choice_attr' => function($choiceValue, $key, $product) {
        return [
            'data-single-price' => $product->getPrice()
        ];
    }
));

您使用了错误的配置选项。
attr
选项不接受回调。在您的情况下,要根据每个选项的值添加属性,您需要使用中的
choice\u attr

示例实现可能如下所示:

$builder->add('attending', ChoiceType::class, array(
    // [..]
    'choice_attr' => function($choiceValue, $key, $product) {
        return [
            'data-single-price' => $product->getPrice()
        ];
    }
));

Symfony的表单生成器允许您为任何顶级选项(数据、占位符等)提供回调,但我认为您不能对单个表单属性使用回调。您应该能够重构它以提供生成整个
attr
数组的回调,而不仅仅是针对
单个\u price
元素。
'attr'=>函数($product){$cena=$product->getJson()[“products”[“price”];返回数组('class'=>'所选的选择订单产品','data placeholder'=>'Wybierz produkt','single_price'=>$cena,);},
我仍然得到:
带有值闭包的选项“attr”应为“array”类型,但为“Closure”类型.
您需要使用
choice\u attr
而不是
attr
。请参阅我的答案。Symfony的表单生成器允许您为任何顶级选项(数据、占位符等)提供回调,但我认为您不能对单个表单属性使用回调。您应该能够对其进行重构,以提供生成整个
attr
数组的回调,而不仅仅是针对
单个price
元素的回调。
'attr'=>函数($product){$cena=$product->getJson()[“products”[“price”];返回数组('class'=>'选择的选择订单产品','数据占位符'=>'Wybierz产品','single_price'=>$cena,);},
我仍然得到:
带值闭包的选项“attr”应为“数组”类型,但为“闭包”类型.
你需要使用
choice\u attr
而不是
attr
。请看我的答案。是的,因为attr代表选择,而choices\u attr代表每个选项。Thx!是的,因为attr代表选择,而choices\u attr代表每个选项。Thx!