Why is my password field / value not being accurately processed?
Here's the counsel I'm attempting to implement:
Here's the counsel that I'm attempting to implement:
One other thing of note. Regardless of whether your password is validated or not, the request will still contain the password field. What this means is that if you don't fill in the password (and it skips the validation part), then your request will be valid, but the input->password will be NULL. You obviously don't want to be inserting that into the database:
<?phpnamespace App\Http\Requests;use Illuminate\Foundation\Http\FormRequest;use Illuminate\Validation\Factory;class UpdateUserRequest extends FormRequest{ /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ // 'name' => 'required | string | max:255', ]; } public function messages() { return [ 'name.required' => 'name is required!', 'password.confirmed' => 'make sure both your "password" and your "confirm password" fields match!' ]; } public function validator(Factory $factory) { $validator = $factory->make($this->input(), $this->rules(), $this->messages()); $validator->sometimes('password', 'required | string | min:8 | confirmed', function($input) { return $input->update_pass == "1"; }); return $validator; }}
The part in my Controller that I have in bold is the issue because, while my code worked when I used this:
$user->password=$validated['password'];
I got this error: "Array callback has to contain indices 0 and 1" when I used $user->password = Hash::make($validated('password'));
My question is two fold...
First of all, where $validated = $request->validated(); is that functionality coming from? when I look at my "UpdateUserRequest," I see my rules and I see how I'm adding some additional parameters based on the whether or not my "update_pass" value is true, but where is "$validated" coming from?
Secondly, what does "Array callback has to contain indices 0 and 1" actually mean and how can I fix it?
Got it!