Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean value after form submit #91

Closed
brunonic opened this issue Jan 20, 2017 · 2 comments
Closed

Boolean value after form submit #91

brunonic opened this issue Jan 20, 2017 · 2 comments

Comments

@brunonic
Copy link
Contributor

brunonic commented Jan 20, 2017

There is a difference between two case for a boolean value.
I have a simple symfony form for create and edit a BlogPost entity.

My form

    ->add(
        'visible',
        ChoiceType::class,
        [
                'label' => 'Etat',
                'choices' => [
                        false => 'Brouillon',
                        true => 'En ligne'
                ]
        ]
    )

My entity

    /**
     * @var bool
     *
     * @ORM\Column(name="visible", type="boolean")
     *
     * @Algolia\Attribute
     */
    private $visible;

And now this my index. On the top, the result of the index after form submit. On the bottom, the result after a algolia:reindex command.

screen shot 2017-01-20 at 07 12 13

Facet can't be used for visible:true

@rayrutjes
Copy link
Member

Hi @brunonic
I was informed of your issue internally, thanks for taking the time to actually submit an issue here.

In the end, my first conclusion would be that the casting of the values only takes place when retrieving the values from the database.

Currently, we do listen for the flush event and act upon that and in that case, the cast didn't take place yet.

I will investigate this and see if we can better handle this to be more consistent.
A quick win for you would be to store your boolean values as integers (0/1) but that would force you to do the casting yourself when using the values each time unless you use non-strict equalities in your codebase.

Give me some time to look into this ;)

@rayrutjes
Copy link
Member

Hey @brunonic , so the thing is that we send the entity in the state it is in just before being sent to the database. My guess is that your form actually sets 1 or 0 for the visibility property.

When re-indexing everything though, the results are being fetched from the database and casted to the correct type.

To enforce the boolean, here is what I suggest you add to your entity:

/**
     * Set visible
     *
     * @param boolean $visible
     *
     * @return Product
     */
    public function setVisible($visible)
    {
        $this->visible = (bool) $visible;

        return $this;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants