0

I searched for similar question but none seem to fit into what i intend doing. I have a form with a hidden part that is only displayed when an option is selected. I do not want my controller to worry about the values of this hidden part if it was hidden (meaning no user input was made as the user did not see it). But if there was input then the controller should deal with the request values. Below is the code

<form class="form-horizontal" role="form" method="POST" action="/employees">
{{ csrf_field() }}

<div class="panel-title">Employee Details</div><br>

<div class="form-group">
    <label for="firstname" class="col-md-4 control-label">Firstname</label>

    <div class="col-md-6">
        <input id="firstname" type="text" class="form-control" name="firstname" value="{{ old('firstname') }}" required autofocus>
    </div>
</div>

<div class="form-group>
    <label for="lastname" class="col-md-4 control-label">Lastname</label>

    <div class="col-md-6">
        <input id="lastname" type="text" class="form-control" name="lastname" value="{{ old('lastname') }}" required autofocus>
    </div>
</div>

<div class="form-group}">
    <label for="department" class="col-md-4 control-label">Department</label>

    <div class="col-md-6">
        <select name="department"  id="dept" class="form-control" required autofocus>
            <option class="text-muted">-- Select Department --</option>
            <option value="administration">Adminstration</option>
            <option value="worker">Worker</option>
        </select>
    </div>
</div>


<div class="col-md-12" id="teacred">
    <div class="panel-title">Create Teacher Authentication</div><br>

    <div class="form-group">
        <label for="username" class="col-md-4 control-label">Username</label>

        <div class="col-md-6">
            <input id="username" type="username" class="form-control" name="username" value="{{ old('username') }}">
        </div>
    </div>

    <div class="form-group">
        <label for="password" class="col-md-4 control-label">Password</label>

        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password">
        </div>
    </div>

    <div class="form-group">
        <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

        <div class="col-md-6">
            <input id="password-confirm" type="password" class="form-control" name="password_confirmation">
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-md-6 col-md-offset-4">
        <button type="submit" class="btn btn-primary">
            Save
        </button>
    </div>
</div>

public function store(Request $request)
{
    $emp = new Employee;

    $emp->firstname = request('firstname');
    $emp->lastname = request('lastname');
    $emp->department = request('department');

    $emp->save();

    if ($emp->department != 'administration') {

        $user = new User;

        $user->firstname = request('firstname');
        $user->lastname = request('lastname');
        $user->role = 'worker';
        $user->username = request('username');
        $user->password = bcrypt(request('password'));

        $user->save();

        $job = new Job;

        $job->firstname = request('firstname');
        $job->lastname = request('lastname');
        $job->subject = request('department');

        $job->save();

    } 

    return redirect('/employees');

}

Unfortunately my code doesn't work, from google chrome web dev tool console the error returned is

An invalid form control with name='username' is not focusable.

But I have not set that attribute as you can see from the form. How do i make it work?

0

1 Answer 1

1

In your form; Type should be text not username

<input id="username" type="username" class="form-control" name="username" value="{{ old('username') }}">

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That fixed it. I also achieved the same thing by splitting my forms into different pages so that when one is successful, the redirect goes to the next page. What will be the best practice, to have it done all in one go or in different steps?
I would say best practice is reading code carefully. Either it is your own or someone else's code as soon as you get ability to read code you will spot errors easily

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.