C5-8.3: how to use select and color form widgets?

Permalink
I have the following in the form.php:
$color->output('color', $color, array('showAlpha' => 'false'));
echo $form->select('fonts', $fonts, $font);

and controller.php:
edit(){
$this->set('font', $font); // $font - user selected font
$this->set('fonts', $fonts); // $fonts - array of available fonts
$this->set('color', 'rgb(0, 0, 0)');
}
save() {
$args['color'] = isset($args['color']) ? trim($args['color']) : 'rgb(0, 0, 0)';
$args['font'] = isset($args['font']) ? trim($args['font']) : '';
}


1. How can I pass color to the form widget? With $color in there, it fails to open the form (must be throwing an uncaught exception). If I change $color to '', the form opens, but the color is obviously not set.
2. How can I save the selected font value, not the index? The $args['font'] saves the selected index only, not the option text value.

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
Ok, I've figured out how to get the color widget to accept a color value from the controller (the 2 $color variables might have had a conflict + the preferredFormat setting):
$ch = Core::make('helper/form/color');
$ch->output('color', $color ? $color : '#0099ff', array('showAlpha' => 'false', 'preferredFormat' => 'hex'));


So, how can the controller save the option text value rather than its index?
linuxoid replied on at Permalink Reply
linuxoid
Found half of the problem - I forgot the 'this' for the variable taken from the DB:
changed
$this->set('font', $font);
to
$this->set('font', $this->font);

and it works just fine!

However the problem remains - it's the select index which gets stored in $args['font'], I still need the actual text for that index to store in the DB.
linuxoid replied on at Permalink Reply
linuxoid
I made a public array variable $this->fonts which I populate with $filename => $path pairs in the on_start() function, then set $this->set('fonts', array_keys($this->fonts)); for use in the form select input. The select gets populated with font filenames - all is fine.

If I use this in the save(): $args['font'] = $args['font']; - it just saves the form's select box selected index.

But if I do this: $args['font'] = array_keys($this->fonts)[1]; or simply this: $args['font'] = $this->fonts[1]; - it saves nothing!

How come?

PS. The db.xml: <field name="font" type="string" size="100"></field>
linuxoid replied on at Permalink Reply
linuxoid
If something simple obviously has to work but it doesn't, there's gotta be a stupid error somewhere...

This shows the 2nd element of the fonts array in the form.php and view.php:
$this->set('font', array_keys($this->fonts)[1]);

but this doesn't save anything in the DB:
$args['font'] = array_keys($this->fonts)[1]; // NOTHING is saved
$args['font'] = 'test' // it DOES save this though!

I'm out of clues. $this->fonts is set in the on_start(), available for view() and edit(), how come it's not for save()?