Basically I have an app that is a database of all of my music/films/etc. Each item is stored in an entity called AVItem. For each of these items I can have zero or more tracks and I do this with a CollectionType. Within this embedded form/collection I have simple text fields and also a Collection of EntityType and they all work perfectly when submitting the main form; however, only the one field that uses my custom field type and transformer does not.
My custom field is for a "duration". In the database I save any lengths of time purely as an integer of seconds; however, I display it and want it entered as a string in the form [HH:]mm:ss. When displaying, the custom field and transformer works, it reads the number of seconds from the entity and then the "transform" method correctly displays that as HH:mm:ss. However, when I submit the main form and one of those tracks has something entered into it, when the "reverseTransform" method is called, the value it receives is always null!
However, in my main entity, where I also have a filled called "totalRunningTime" which also uses the same custom field type and transformer, the transform and reverseTransform always work perfectly.
This seems to be an issue purely within the Collection/embedded form.
Does anyone have an ideas?
Here are some code snippets.
This is my custom data transformer:
class TimeDurationTransformer implements DataTransformerInterface
{
private ?string $invalidMessage = null;
public function __construct( ?string $invalidMessage = null )
{
$this->invalidMessage = $invalidMessage;
}
/**
* Transform an integer (number of seconds) into a string of the form HH:mm:ss.
*
* @param mixed $value
* @return mixed
*/
public function transform( mixed $value ) : mixed
{
if ( ( null === $value ) || ( intval( $value ) == 0 ) ):
return null;
endif;
return sprintf( '%02d:%02d:%02d', ( $value/ 3600 ), ( $value / 60 % 60 ), ( $value% 60 ) );
}
/**
* Transform a string of the form HH:mm:ss into a number of seconds as an integer.
*
* @param mixed $value
* @return mixed
*/
public function reverseTransform( mixed $value ) : mixed
{
if ( ( null === $value ) || empty( $value ) || ( strlen( $value ) < 1 ) ) :
return null;
endif;
$matchResult = preg_match( pattern: '/^((\d{1,3}):)?(\d{1,2}):(\d{1,2})$/', subject: $value, matches: $matches, flags: PREG_UNMATCHED_AS_NULL );
// If the string entered does not match the pattern, throw an exception
if ( ( false === $matchResult) || ( 0 === $matchResult ) ) :
throw new TransformationFailedException( message: $this->invalidMessage, invalidMessage: $this->invalidMessage );
else:
if ( $matchResult === 1 && ( count( $matches ) == 5 ) ) :
if ( round( $matches[4] ) != null ):
return( round( $matches[4] ) + ( $matches[3] ? ( $matches[3] * 60 ) : 0 ) + ( $matches[3] ? ( $matches[2] * 3600 ) : 0 ) );
endif;
endif;
throw new TransformationFailedException( message: $this->invalidMessage, invalidMessage: $this->invalidMessage );
endif;
}
}
This is my custom field type:
class TimeDurationType extends AbstractType
{
private $translator;
public function __construct( TranslatorInterface $translator ) {
$this->translator = $translator;
}
public function buildForm( FormBuilderInterface $builder, array $options ) : void
{
$builder->addModelTransformer( new StuggiBearTimeDurationTransformer( invalidMessage: $options[ 'invalid_message' ] ) );
}
public function configureOptions( OptionsResolver $resolver ): void
{
$resolver->setDefaults( [
'data_class' => null,
'required' => false,
'constraints' => [
new PositiveOrZero(),
],
'invalid_message' => $this->translator->trans( 'message.assert.time_duration_format' ),
'attr' => [
'placeholer' => '',
],
] );
}
public function getParent(): string
{
return TextType::class;
}
}
Within my main form for my entity AVItem, I have one field that uses this custom field type - and it works fine:
public function buildForm( FormBuilderInterface $builder, array $options ): void
{
$this->editType = $options['edit_type'];
$this->isWishlistItem = $options[ 'isWishlistItem' ];
$builder
...
->add( 'totalRunningTime', TimeDurationType::class, [
'required' => false,
'label' => $this->translator->trans( 'form.field.label.total_running_time' ),
'label_html' => true,
'attr' => [
'placeholder' => 'HH:mm:ss',
],
'empty_data' => null,
] )
...
Within this form I then have the collection of Tracks:
$builder
...
->add( 'tracks', CollectionType::class, [
'label' => $this->translator->trans( 'form.field.label.tracks' ),
'label_html' => true,
'entry_type' => TrackEmbeddedFormType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'empty_data' => [],
] )
...
...and then the embedded form type for the tracks:
$builder
->add( 'seriesEpisodeNumber', null, [
'required' => false,
'label' => $this->translator->trans( 'form.field.label.atce_series_episode_number' ),
] )
->add( 'title', null, [
'required' => ( $this->editType == FormEditType::SEARCH ) ? false : true,
'label' => $this->translator->trans( 'form.field.label.atce_title' ),
'label_html' => true,
] )
->add( 'additionalArtistsActors', EntityType::class, [
'required' => false,
'label' => $this->translator->trans( 'form.field.label.atce_additional_artists_actors' ),
'mapped' => true,
'class' => PersonGroup::class,
'by_reference' => true,
'multiple' => true,
'expanded' => false,
'choices' => [],
] )
->add( 'comment', null, [
'required' => false,
'mapped' => true,
'label' => $this->translator->trans( 'form.field.label.atce_comment' ),
'label_html' => true,
] )
->add( 'trackChapterNumber', null, [
'required' => true,
'label' => $this->translator->trans( 'form.field.label.atce_number' ),
'label_html' => true,
] )
->add( 'duration', TimeDurationType::class, [
'required' => false,
'label' => $this->translator->trans( 'form.field.label.atce_duration' ),
'label_html' => true,
'attr' => [
'placeholder' => 'HH:mm:ss',
],
'empty_data' => null,
] );
...
So, as I mentioned, the field "totalRunningTime" works perfectly with the data transformer, converting between integer seconds and a HH:mm:ss string and back.
However, the "duration" field within my collection/embedded tracks form only works reading from the database (the "transform" function works), but when I go to submit the form, the "reverseTransform" function always receives null.
If I change this field to be one of the standard types such as TextType or the like, then the value from the field seems to get passed.
Any help would be greatly appreciated.
0 comments:
Post a Comment
Thanks