问题
I cannot get DB values of games to be selected:
Game:
actAs:
Timestampable: ~
columns:
id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
game_name: { type: string(100), notnull: true }
logo: { type: string(100), notnull: true, comment: "Game Logo" }
indexes:
it:
fields: game_name
type: unique
Campaign:
actAs:
Timestampable: ~
columns:
id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
name: { type: string(100), notnull: true }
percentage: { type: integer(4), notnull: true, unsigned: true }
is_active: { type: integer(1), notnull: true, unsigned: true }
start: { type: datetime, notnull: true }
end: { type: datetime, notnull: true }
CampaignGames:
actAs:
Timestampable: ~
columns:
id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
campaign_id: { type: integer(4), notnull: true, unsigned: true }
game_id: { type: integer(4), notnull: true, unsigned: true }
indexes:
tc:
fields: [campaign_id, game_id]
type: unique
relations:
Campaign: { onDelete: CASCADE, local: campaign_id, foreign: id, foreignAlias: CampaignCampaignGames }
Game: { onDelete: CASCADE, local: game_id, foreign: id, foreignAlias: GameCampaignGames }
I have added games checkbox here which belongs to Game model to let the user add games to CampaignGames, but unfortunately they never checked... And these values are present in DB.
class AdminconsoleCampaignForm extends CampaignForm
{
public function configure()
{
parent::configure();
$this->widgetSchema['is_active'] = new sfWidgetFormSelectRadio(array(
'choices' => array(1 => 'On', 0 => 'Off'),
));
$games = Doctrine_Core::getTable('Game')->getGames();
$this->widgetSchema['game_id'] = new sfWidgetFormSelectCheckbox(array(
'choices' => $games
));
$this->validatorSchema['game_id'] = new sfValidatorChoice(array(
'choices' => array_keys($games)
, 'multiple' => true
, 'required' => false
));
$this->removeFields();
}
Also tried to use
$this->widgetSchema['game_id']->setDefault(array($data));
No luck. How to resolve it? I'm really stuck on that.
回答1:
There are two things that caught my attention:
1. You're not using Doctrine's boolean
data type
Try changing your schema.yml
to the following:
Campaign:
[...]
columns:
[...]
is_active:
type: boolean
notnull: true
default: 0 # Or whichever default value you prefer
[...]
This way Symfony/Doctrine will take care of anything regarding the is_active
row of your Campaign
record.
If you now rebuild your model your BaseCampaignForm.class.php
will define the is_active
widget automatically like this:
$this->setWidgets(array(
[...]
'is_active' => new sfWidgetFormInputCheckbox(),
[...]
);
$this->setValidators(array(
[...]
'ist_active' => new sfValidatorBoolean(array('required' => false)),
[...]
);
Note: That required
is set to false
is there because if a checkbox isn't selected it's not posted either. The sfValidatorBoolean
takes care of this and disables the value all by itself. If you would set it to true than the user wouldn't be able to uncheck the box and submit the form without a validator exception.
2. You tried to set form object defaults in its form's widget
In your code you used:
$this->widgetSchema['game_id']->setDefault(array($data));
This won't work because you're using a Form with an object attached to it (a BaseFormDoctrine
). All the default values are taken right out of the object assigned to that form (in your case a Campaign
object because you're extending CampaignForm
).
(Major pitfall) If you want to set default values on a form object you have to set them on the form object itself :
$this->getObject()->setGameId($id);
Default object values can't be set using the object form's widgets. These default values will always be overwritten by the actual object values (which makes sense because the form represents the object).
Glad if I was able to help you in some way.
回答2:
If your choices are based on doctrine records (which they are), then you should use sfWidgetFormDoctrineChoice. Change the renderer_class option if you want to get radio buttons / checkboxes instead of a select tag.
回答3:
Your $game
array probably has the keys by order (0,1,2,3,4) and you end up having a select like:
<select>
<option value="0">Option 1</option>
<option value="1">Option 2</option>
<option value="2">Option 3</option>
</select>
But your object ids do not coincide with those keys. You have to change the line:
$games = Doctrine_Core::getTable('Game')->getGames();
to:
$c = Doctrine::getTable('Game');
$c->setAttribute(Doctrine_Core::ATTR_COLL_KEY, 'id');
$games = $c->getGames();
so that the arrays gets the keys = ids. Like:
<select>
<option value="3">Option 1</option>
<option value="7">Option 2</option>
<option value="9">Option 3</option>
</select>
来源:https://stackoverflow.com/questions/7916459/checkboxes-are-not-checked