I've been following the Documentation on creating custom form types given in the docs: http://symfony.com/doc/master/cookbook/form/create_custom_field_type.html#using-the-field-type
Having the GenderType from the given example, i'd like to "pimp" my entity, which i'm working on with the new FormType.
class Person {
const GENDER_MALE = "m";
const GENDER_FEMALE = "f";
private $gender;
....
/* generated getter/setter */
.....
public function getGenderAsText()
{
return $this->getGender() == self::GENDER_MALE?"male":"female";
}
}
My question, maybe has anyone a a good advise.. .how to combine the elegance of the GenderType which handels choice picking the form like a charm with my model, so ease usage in templates etc.. ?
Update
Alex pointed out, how to display a specific field with a self-written gender twig extension.
This works like a charm, when you especially call the extension on the field {{ item.gender|gender }}
I'm having a bunch of objects, using inheritance for the common fields - but there's a bunch of data which are unique for each class. I've written some logic to extract all properties, hand 'em over to the template and a TwigExtension handles rendering:
public function dynamicContractFilter($value)
{
// handle DateTime
if ($value instanceof \DateTime) {
return $value->format('d.m.Y');
}
....
} elseif (is_bool($value)) {
return $value ? 'yes' : 'no';
} else {
return $value; // plain string
}
}
I can't determine if it's a simple string/integer or a "GenderTypedField"..
I guess this is some kind of limitation of PHP itself, as it's typeless.. sight