Ok, here is a very basic example...
// productForm class extends myForm
public function configure()
{
$schema = new sfWidgetFormSchema(array(
'is_enabled' => new sfWidgetFormSelectRadio(array(
'choices' => array(1 => 'Yes', 0 => 'No'),
)),
));
}
// myForm extends sfForm
public function updateDefaultsFromObject()
{
// $object instanceof Doctrine_Record
if ($object = $this->getObject())
{
$values = $object->toArray();
$defaults = array_merge($values, $this->getDefaults());
$this->setDefaults($defaults);
}
}
//myTemplate, $form instance of productForm
<?php echo $form['is_enabled']->renderRow(); ?>
This is just a dumbed down example, but I think it paints the
picture. I've built a very basic (so far) implementation that works
similar to sfFormPropel in order to inject my doctrine model data into
a form.
The problem is that doctrine returns a boolean field into the $object,
it does so as a php type true/false. So when sfForm tries to map
"false" to my radio button, in order to select the default value, it
can't do it because it thinks false = "". A form field cannot hold
true or false, so if you set true/false as a default value in your
form, its not useful.
So there are two solutions to this problem:
A.) Symfony can detect boolean values true/false are set as "defaults"
and type cast them to integers (1, 0).
B.) I have to loop through all values in a updateDefaultsFromObject()
class and check if the field type is a boolean, then convert to
integers before setDefaults is called.
Personally, I don't see it as a problem to move php booleans to
integers when being outputted into form HTML. Does this example make
sense?
On May 15, 7:50 am, "Jonathan Wage" <[EMAIL PROTECTED]> wrote:
> Sorry. We'll have to wait for Ian to post his example. I was helping him
> with the problem yesterday.
>
> I understand now that what I was saying doesn't make sense because the value
> from the array is the value used to determine if it is checked or not, not
> what is used for the form value.
>
> Something else may be going on here.
>
> - Jon
>
> On Thu, May 15, 2008 at 8:46 AM, Fabien POTENCIER <
>
>
>
> [EMAIL PROTECTED]> wrote:
>
> > I really don't understand.
>
> > The true/false value from your Doctrine object is the value used to
> > determine if the checkbox is checked or not, which is really different
> > from the checkbox HTML attribute "value" which is the value returned
> > when the checkbox is checked.
>
> > Can you provide an example ? It will perhaps be easier to understand the
> > problem you have.
>
> > Fabien
>
> > --
> > Fabien Potencier
> > Sensio CEO - symfony lead developer
> > sensiolabs.com | symfony-project.com | aide-de-camp.org
> > Tél: +33 1 40 99 80 80
>
> > Jonathan Wage wrote:
> > > He says that when the form is submitted, value="1" works, but value=""
> > > does nothing. I assume because the form things no data was submitted for
> > > that field.
>
> > > - Jon
>
> > > On Thu, May 15, 2008 at 8:21 AM, Fabien POTENCIER
> > > <[EMAIL PROTECTED]
> > > <mailto:[EMAIL PROTECTED]>> wrote:
>
> > > Jonathan Wage wrote:
> > > > He is setting the default values for a form, by getting the
> > > values from
> > > > a Doctrine object like this, "$object->toArray()". Columns in
> > > Doctrine
> > > > which are boolean get hydrated to php type boolean. This array
> > > does not
> > > > seem to be able to be passed directly to the form for yes/no
> > > radios or
> > > > single checkbox and automatically recognized and converted to the
> > > html
> > > > counterpart value 1 and 0.
>
> > > > The checkbox or radio ends up with value="1"
> > > > for true and value="" for false.
>
> > > which looks good, no?
>
> > > Fabien
>
> > > > - Jon
>
> > > > On Thu, May 15, 2008 at 5:15 AM, Fabien POTENCIER
> > > > <[EMAIL PROTECTED]
> > > <mailto:[EMAIL PROTECTED]>
> > > > <mailto:[EMAIL PROTECTED]
> > > <mailto:[EMAIL PROTECTED]>>> wrote:
>
> > > > I'm not sure I understand the problem here. A checkbox and a
> > > radio
> > > > button can have values that are not Booleans.
>
> > > > Can you give an example of what does not work like you think
> > > it should?
>
> > > > Fabien
>
> > > > --
> > > > Fabien Potencier
> > > > Sensio CEO - symfony lead developer
> > > > sensiolabs.com <http://sensiolabs.com>
> > > <http://sensiolabs.com> | symfony-project.com
> > > <http://symfony-project.com>
> > > > <http://symfony-project.com> | aide-de-camp.org
> > > <http://aide-de-camp.org>
> > > > <http://aide-de-camp.org>
> > > > Tél: +33 1 40 99 80 80
>
> > > > Ian wrote:
> > > > > There are often times when populating the "default" values
> > > of a form,
> > > > > where I may be passing in a php boolean true/false value,
> > and
> > > > > currently the sfForm framework cannot parse this correctly.
>
> > > > > For example, I when you have a field in doctrine that is
> > > defined as a
> > > > > boolean, it will return php type true or false, as the
> > > value of that
> > > > > field, regardless of if it is stored in the db as 1 or 0.
>
> > > > > However, if I have a radio button or check box widget that
> > is
> > > > > technically a boolean, it should recognize if I pass a php
> > > type true/
> > > > > false into the default value for that tag and properly
> > > type cast this
> > > > > to an integer, 1 or 0 respectively. An HTML form can
> > > never hold
> > > > a php
> > > > > boolean value, so I don't see how this would ever harm
> > > anything.
>
> > > > > I took a look at the code, and there are several possible
> > > place where
> > > > > this check/conversion could take place, but I think its up
> > > to you,
> > > > > Fabien, to decide where this should go. I would assume it
> > > would be
> > > > > best coverted just before HTML output.
>
> > > > > Thoughts?
>
> > > > > --Ian
>
> > > > --
> > > > Jonathan Wage
> > > >http://www.jwage.com
> > > >http://www.centresource.com
>
> > > --
> > > Jonathan Wage
> > >http://www.jwage.com
> > >http://www.centresource.com
>
> --
> Jonathan Wagehttp://www.jwage.comhttp://www.centresource.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---