Shiva
7 months ago

How to only submit password when typed

Posted 7 months ago by Shiva

So I'm trying to create a page where once the user has registered they can then add more info and update their password should they need to. The problem I'm having is that, when the user submits the form without doing anything to the password field I get the

Integrity constraint violation: 1048 Column 'password' cannot be null

What I would like to know is, if there is a way to submit the form without touching the password field unless the user clicks on the textbox. I want to leave the password field blank so that when they try to update their password they first have to enter their existing password and then the password is checked against the database to see if it is correct before updating the password.

Here is my form

<form action="{{ route('post.personal-details') }}" method="post">
    @csrf
    <div class="row">
        <div class="col">
            <div class="form-group"> 
                <h1>Change your email address</h1>
                <label for="email">Email Address</label>
                <input type="text" name="email" id="email" class="form-control" value="{{ $user->email }}">

                <label for="email_confirmation">Confirm Email Address</label>
                <input type="text" name="email_confirmation" id="email_confirmation" class="form-control">
            </div>
        </div>

        <div class="col">
            <div class="form-group">
                <h1>Change your password</h1>
                <label for="existing_password">Existing Password</label>
                <input type="text" name="existing_password" id="existing_password" class="form-control" >

                <label for="password">New Password</label>
                <input type="text" name="password" id="password" class="form-control">

                <label for="confirm_password">Confirm New Password</label>
                <input type="text" name="confirm_password" id="confirm_password" class="form-control">
            </div>
        </div>
    </div>
</form>

and here is my controller function

public function postPersonalDetails(Request $request)
{
    $id = Auth::user()->id;

    $user = User::findOrFail($id);

    $input = $this->getInput($request);

    $user->fill($input);
    $user->save();

    return redirect()->route('account.details')->with('success', 'Details saved successfully');
}

protected function getInput(Request $request)
{
    return $request->only([
        'name',
        'surname',
        'email',
        'password',
        'phone_number',
        'telephone_2',
        'trading_name',
        'company_name',
        'vat_number',
        'ck_number',
        'office_number',
        'work_email',
        'office_address',
        'contact_person',
    ]);
}

Please sign in or create an account to participate in this conversation.