Skip to content

Conditional Field

TinaH edited this page Dec 8, 2020 · 2 revisions

You can display a field based on a condition.

Only null or FooField::make() are allowed conditional values.

Example 1:

    public function fields()
    {
        return [
           $this->someBoolean ? Input::make('Conditional Field 1')->required() : Input::make('Conditional Field 2')->required(),
        ];
   }

Example 2:

public function conditional()
{
   if($this->something === "No") {
    return false; //the field will be omitted
   } else {
    return true; //the field will be included
   }
}

//this field will be omitted if the method returns false
public function fields()
{
    return [
        $this->conditional() ? Input::make('Conditional Field')->required() : null,
    ];
}

Example 3:

public function fields()
{
    return [
        $this->someMethod(),
    ];
}

public function someMethod()
{
    $boolean = //something that returns true or false;
    if($boolean)) {
        return Input::make('Conditional Field')->required(); 
    } else {
        return null;
    }
}
Clone this wiki locally