Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Add conditional option #99

Merged
merged 4 commits into from
Jan 16, 2017
Merged

Add conditional option #99

merged 4 commits into from
Jan 16, 2017

Conversation

daguilarm
Copy link
Contributor

Sometimes, we required from our form a conditional behaviour, like:

$builder->text('email')->disabled(($profile->isAdmin()) ? true : false);

Sometimes, we required from our form a conditional behaviour, like:

$builder->text('email')->disabled(($profile->isAdmin()) ? true : false);
@coveralls
Copy link

coveralls commented May 14, 2016

Coverage Status

Coverage increased (+0.1%) to 94.749% when pulling 9b1a5ea on daguilarm:master into 8056cea on adamwathan:master.

@axyr
Copy link

axyr commented Aug 24, 2016

I would realy like to see this merged!

@adamwathan
Copy link
Owner

@daguilarm Can you add tests for this? Will merge when there's tests, thanks 👍

@jesseleite
Copy link
Contributor

I'm proposing a similar solution in #110, though I think a bit more flexible as it's a magic method implementation. Thoughts?

@coveralls
Copy link

coveralls commented Jan 16, 2017

Coverage Status

Coverage increased (+0.2%) to 94.86% when pulling 2b3cc89 on daguilarm:master into 8056cea on adamwathan:master.

@coveralls
Copy link

coveralls commented Jan 16, 2017

Coverage Status

Coverage increased (+0.2%) to 94.86% when pulling 003edff on daguilarm:master into 8056cea on adamwathan:master.

@axyr
Copy link

axyr commented Jan 16, 2017

I want this so bad.....
I have shitloads of:

@if($item->send_email)
    {!! BootForm::textarea('Message', 'message')->addClass('emailmessage')->required() !!}
@else
    {!! BootForm::textarea('Message', 'message')->addClass('emailmessage') !!}
@endif

@adamwathan
Copy link
Owner

Thanks for adding the tests! What do we think the expected behavior should be if someone did something like:

Form::text('name')->required()->required(false)

Right now, it looks like this field would still be required. What do you think?

@jesseleite I think I'm going to add the behavior from this PR even if we do add the magic method style you suggested in your PR, but we can consider the changes you've proposed separately. My instinct is that I don't love stuff that magically looks for substrings in method names (even bugs me when Laravel does it) but can still definitely discuss it.

@daguilarm
Copy link
Contributor Author

daguilarm commented Jan 16, 2017

    public function hasAttribute($attribute)
    {
        return in_array($attribute, $this->attributes);
    }

    public function conditional($condition, $attribute)
    {
        if ($condition) {
            $this->setAttribute($attribute, $attribute);
        } else {
            if($this->hasAttribute($attribute)) {
                $this->removeAttribute($attribute);
            }
        }
    }

   public function required($condition = true)
    {
        $this->conditional($condition, __FUNCTION__);
        return $this;
    }

something like this?

@adamwathan
Copy link
Owner

I think maybe something like this:

public function required($conditional = true)
{
    $this->setBooleanAttribute('required', $conditional);
    return $this;
}

protected function setBooleanAttribute($attribute, $value)
{
    if ($value) {
        $this->setAttribute($attribute, $attribute);
    } else {
        $this->removeAttribute($attribute);
    }
}

It's totally fine to call removeAttribute without first checking if it's there.

@daguilarm
Copy link
Contributor Author

Perfect!!

@coveralls
Copy link

coveralls commented Jan 16, 2017

Coverage Status

Coverage increased (+0.2%) to 94.824% when pulling 5699ef1 on daguilarm:master into 8056cea on adamwathan:master.

@adamwathan adamwathan merged commit c58d67d into adamwathan:master Jan 16, 2017
@axyr
Copy link

axyr commented Jan 20, 2017

Thank you all! I really missed this and i use this package a lot!!

@jesseleite
Copy link
Contributor

jesseleite commented Feb 2, 2017

@jesseleite I think I'm going to add the behavior from this PR even if we do add the magic method style you suggested in your PR, but we can consider the changes you've proposed separately. My instinct is that I don't love stuff that magically looks for substrings in method names (even bugs me when Laravel does it) but can still definitely discuss it.

@adamwathan I love the magic method implementation in #110. I understand what you mean on the substring If in the magic method name though. I feel like #110's implementation was more flexible because it allowed for a conditional expression on any attribute setter, including magic attribute setters...

Form::text('test')->wathan('sexy');
// <input type="text" name="test" wathan="sexy">

Form::text('test')->wathanIf(false, 'ugly');
// <input type="text" name="test">

This PR hardcodes the 'conditionalable' (oh geez) expression to attributes like required, disabled, etc.

...That said, if you want to close my other PR, I won't be sad :)

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

Successfully merging this pull request may close these issues.

5 participants