使用整数字段通过按位运算存储选定的日期
Use an integer field to store the selectged days with bitwise op
我正在编写一个包含一周中所有日期的表单,但这些日期保存在一个 int 字段 $days 中。我正在使用按位运算来显示选定的日期。
{% if (day.days b-and 1) == 1 %}
{{ "sunday" |trans }}
{% endif %}
{% if (day.days b-and 2) == 2 %}
{{ "monday" |trans }}
{% endif %}
....
我不知道如何显示复选框数组并将其转换为 int 和相反。
这是表单类型的一部分
$informations = $builder->create('information', FormType::class, [
'label'=>'Information',
'inherit_data' => true,
'label_attr' => ['class'=>'catlabel']])
->add('categoryQualityView', ChoiceType::class, [
'required' => true,
'label' => 'viewQuality',
'choices' => PlaceRepository::$categoriesRates,
'attr' => [
'class' => 'selectpicker',
],
])
->add('categoryGastronomy', ChoiceType::class, [
'label' => 'Gastronomy',
'required' => true,
'choices' => PlaceRepository::$categoriesGastronomy,
'attr' => [
'class' => 'selectpicker',
],
])
->add('price', MoneyType::class, [
'required' => false,
'label' => 'Price',
])
->add('days', IntegerType::class, [
'required' => false,
'label' => 'Days',
])
->add('description', TextType::class, [
'required' => false,
'label' => 'Description',
])
;
对于您的情况,您可以创建自定义 "Form Field Type" (and maybe if needed a custom Data Transformer) 并按照文档中的描述自定义表单模板。
例如:
class DaysOfWeekType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'choices' => [
'Monday' => 1,
'Tuesday' => 2,
...
],
]);
}
public function getParent(): string
{
return ChoiceType::class;
}
}
我正在编写一个包含一周中所有日期的表单,但这些日期保存在一个 int 字段 $days 中。我正在使用按位运算来显示选定的日期。
{% if (day.days b-and 1) == 1 %}
{{ "sunday" |trans }}
{% endif %}
{% if (day.days b-and 2) == 2 %}
{{ "monday" |trans }}
{% endif %}
....
我不知道如何显示复选框数组并将其转换为 int 和相反。
这是表单类型的一部分
$informations = $builder->create('information', FormType::class, [
'label'=>'Information',
'inherit_data' => true,
'label_attr' => ['class'=>'catlabel']])
->add('categoryQualityView', ChoiceType::class, [
'required' => true,
'label' => 'viewQuality',
'choices' => PlaceRepository::$categoriesRates,
'attr' => [
'class' => 'selectpicker',
],
])
->add('categoryGastronomy', ChoiceType::class, [
'label' => 'Gastronomy',
'required' => true,
'choices' => PlaceRepository::$categoriesGastronomy,
'attr' => [
'class' => 'selectpicker',
],
])
->add('price', MoneyType::class, [
'required' => false,
'label' => 'Price',
])
->add('days', IntegerType::class, [
'required' => false,
'label' => 'Days',
])
->add('description', TextType::class, [
'required' => false,
'label' => 'Description',
])
;
对于您的情况,您可以创建自定义 "Form Field Type" (and maybe if needed a custom Data Transformer) 并按照文档中的描述自定义表单模板。
例如:
class DaysOfWeekType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'choices' => [
'Monday' => 1,
'Tuesday' => 2,
...
],
]);
}
public function getParent(): string
{
return ChoiceType::class;
}
}