I’m trying to build a form gathering information about new player in a game.
To start a game one need to provide nickname, email and a code.
Codes are stored in another table connected with player table with one-to-one relation
What I need to do during validation is to check if provided token exists and if so store Player id in Code record.
To do that I’m trying to build a form:
class PlayerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code','text')
->add('email', 'email')
->add('nick', 'text')
;
}
...
}
but in that way during validation (or probably during creating instance of Code)
Argument 1 passed to Player::setCode() must be an instance of Code, string given
which is obvious since string has been provided.
What to do to perform a lookup during form validation and pass not token string but token instance?
Look into data transformers. This will let you to create a form with a scalar type field that gets transformed into an entity when populating the model on a form submit.
Regarding the validation, you’ll need to create a custom validation constraint that checks if the given code exists in the database.