A
A
Anton2016-06-01 11:08:51
Laravel
Anton, 2016-06-01 11:08:51

How to make changes to Request after validation?

Hello!
Knowledgeable and understanding people, help me, please, to understand.
Here is the registration processing code:

public function store(Request $request)
{
    $rules = [
        // Всякие правила.
    ];

    $validator = Validator::make($request->except(['_token']), $rules);

    if ($validator->fails()) {
        return redirect('registration')->withErrors($validator)->withInput();
    }

    /* Вот это мне нужно внести в Request */
    $birthday = $request->input('birthday');
    $birthday = implode('.', $birthday);
    /* ---------------------------------- */

    User::create($request->except(['_token']));
    return redirect('registration/success');
}

Question:
1. How can I make a change ($birthday) to the Request before or after validation?
2. Why, using withInput, the data in the fields after the redirect is not saved?
3. How else can I make my own check for the correctness of the password confirmation: in the rules somehow write down or write my own condition?
Here is an example html code:
<div class="form-group">
    <label for="password" class="col-sm-5 control-label">Пароль:</label>
    <div class="col-sm-7">
        <input id="password" name="password" type="password" class="form-control">
    </div>
</div>
<div class="form-group">
    <label for="password_repeat" class="col-sm-5 control-label">Подтверждение пароля:</label>
    <div class="col-sm-7">
        <input id="password_repeat" name="password_repeat" type="password" class="form-control">
    </div>
</div>

And this is an html explanation of why the field "Date of birth" is processed this way:
<div class="form-group">
    <label class="col-sm-5 control-label">Дата рождения:</label>
    <div class="col-sm-7">
        <div class="row">
            <div class="col-sm-3">
                <input name="birthday[]" type="text" class="form-control" placeholder="Day">
            </div>
            <div class="col-sm-4">
                <input name="birthday[]" type="text" class="form-control" placeholder="Month">
            </div>
            <div class="col-sm-5">
                <input name="birthday[]" type="text" class="form-control" placeholder="Year">
            </div>
        </div>
    </div>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-06-01
@Yadalay

1) You can get $request->all() and write a new birthday into the resulting array, and then validate it. But it's better to write your own custom validator here so that it validates the original birthday.
2) Because this data is not displayed anywhere. You need to add value="{{ old('password') }}" in the fields:
3) There is a rule "confirmed", which just checks the confirmation of the field:

$rules = [
  'password' => 'required|confirmed'
];

It looks to see if there is a password_confirmation field and whether it matches the password
field We study the available validation rules https://laravel.com/docs/5.2/validation#available-...
4) Do $request->except(['_token'] ) not necessary. (here User::create($request->except(['_token']));)
It is better to prescribe $fillable fields in User, then there will be no problems with saving.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question