0

I need to include a conditional statement into the following array called $fields.

$fields = [
    'program_id'     => [
        'type'        => 'select',
        'label'       => 'Program',
        'opts'        => ["One", "Two", "Three"],
    ],
    'name'           => [
        'label' => 'Job Name'
    ],
    'start_date'         => [
        'class' => 'date-picker',
        'label' => 'Job Starts' . $req,
        'val'   => $job->start_date ? dateToPicker($job->start_date) : null
    ],
    'end_date'           => [
        'class' => 'date-picker',
        'label' => 'Job Ends' . $req,
        'val'   => $job->end_date ? dateToPicker($job->end_date) : null
    ],
    'section'            => [
        'type' => 'hidden',
        'val'  => 'details'
    ],
];
if (!$job->id && $program)
{
    $fields['job_copy'] = [
        'label'       => 'Copy Job From',
        'type'        => 'select',
        'textAsValue' => false,
        '_comment'    => 'Selecting a job here will copy all job information except the name.',
        'opts'        => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
    ];
}
$fields[] = [
    'type'  => 'submit',
    'label' => "Save",
    'class' => 'btn btn-primary !important'
];

}

I need to move the conditional statement to the top so that it is the first thing displayed on the form. However, when I move it to the top it disappears. How can I integrate the conditional check into the top of the form as opposed to at the bottom where it currently displays?

0

1 Answer 1

1
$fields  = [];
if (!$job->id && $program)
{
    $fields['job_copy'] = [
        'label'       => 'Copy Job From',
        'type'        => 'select',
        'textAsValue' => false,
        '_comment'    => 'Selecting a job here will copy all job information except the name.',
        'opts'        => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
    ];
}

$fields2 = [
    'program_id'     => [
        'type'        => 'select',
        'label'       => 'Program',
        'opts'        => ["One", "Two", "Three"],
    ],
    'name'           => [
        'label' => 'Job Name'
    ],
    'start_date'         => [
        'class' => 'date-picker',
        'label' => 'Job Starts' . $req,
        'val'   => $job->start_date ? dateToPicker($job->start_date) : null
    ],
    'end_date'           => [
        'class' => 'date-picker',
        'label' => 'Job Ends' . $req,
        'val'   => $job->end_date ? dateToPicker($job->end_date) : null
    ],
    'section'            => [
        'type' => 'hidden',
        'val'  => 'details'
    ],
];
$fields = array_merge($fields,$fields2);
$fields[] = [
    'type'  => 'submit',
    'label' => "Save",
    'class' => 'btn btn-primary !important'
];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.