0

How will I validate the current entries from here , this is the json response inside the Chrome Devtools after submitting the form thru axios ?

JSON Response

And this is in my UpdateProfileRequest.php

    public function rules()
    {
     return [
        'username'         => 'required|unique:users|min:3',
       'password'    =>      'required|min:8|confirmed',
       'confirm_password'        => 'required|',
       'current_password'       => 'required|u',
       'country_id'      => 'required|integer',
       'display_name'     => 'required|min:5',
       'email'    => 'required|unique:users,email_address',
       'phone_number' => 'required|alpha_num',
       'image' => 'mimes:jpg,png,gif'
     ];

This is the whole response from the request

In the App\Http\Requests\UpdateProfileRequest

This is inside my UpdateProfile.vue

<div>
        <div v-if="user != null">
            <form class="bg-white m-auto h-full p-4 w-full" id="setup-billing-form" @submit.prevent="submitProfile()" method="POST">
                <div class="flex inline-block">
                    <div id="input-group" class="w-3/5">    
                        <label for="name" class="block uppercase tracking-wide text-black-v2 text-xs font-bold mb-2">Username
                        </label>
                        <input v-model="form.username" type="text" class="hover:bg-grey-lightest bg-grey-lighter w-full mb-2 p-2 leading-normal" id="pin" name="pin" autocomplete="name" placeholder="Your Username" required>
                    </div>
                    <div id="input-group" class="ml-2 w-3/5">   
                        <label for="name" class="block uppercase tracking-wide text-black-v2 text-xs font-bold mb-2">Email
                        </label>

this is is inside my axios request

submitProfile(){
        let data = new FormData();
        axios.put(this.endpoint, { 
            form : this.form , 
            image : this.image
        }).then(response => {
            console.log(response.data);             
        }).catch(error => {
            console.log(error);
        });
    },

Now, I want to ask if how do I validate those requests inside my UpdateProfileRequest, should I add the form. to each of those requests ?

8
  • Can u post your view's code? Commented Dec 2, 2019 at 1:51
  • What view do you mean @TsaiKoga Commented Dec 2, 2019 at 2:07
  • I means the form. Commented Dec 2, 2019 at 2:59
  • Please read the description above, I already gave you the request form Commented Dec 2, 2019 at 3:07
  • plz post your axios code. Commented Dec 2, 2019 at 3:28

1 Answer 1

1

Two Methods:

1. just use form.username to validate

$rule = [
        'form.username'         => 'required|unique:users|min:3',
       'form.password'    =>      'required|min:8|confirmed',
       'form.confirm_password'        => 'required|',
       'form.current_password'       => 'required|u',
       'form.country_id'      => 'required|integer',
       'form.display_name'     => 'required|min:5',
       'form.email'    => 'required|unique:users,email_address',
       'form.phone_number' => 'required|alpha_num',
       'image' => 'mimes:jpg,png,gif'
     ];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
}

2. Flatten the object to request:

It seems that you new a formdata without using it.

And pass the data with keys form and image.

You need to flatten your object before put it:

submitProfile(){
        // if you use formdata
        // let form = new FormData(this.form);
        // let form = form.append('image', this.image);
        let form = Object.assign({}, this.form);  // clone this.form
        form['image'] = this.image;

        axios.put(this.endpoint, form).then(response => {
            console.log(response.data);             
        }).catch(error => {
            console.log(error);
        });
    },

This request that you can directly validate by 'username', 'password', ... without prefix form.

and if you want to insert the datas, you can just use $request->except('image').

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

13 Comments

Is it advisable to use formdata ??
Because I'm not using formdata inside it
@TheBAST ok, I have changed it to object.
Please show me a way on how yo handle that in your controller
axios.put(this.endpoint, { form : this.form , image : this.image }).then(response => { console.log(response.data); }).catch(error => { console.log(error); }); My form looks like this form : { username : this.user.username, email : this.user.email, id : this.user.id, current_password : "", password : "", confirm_password : "", display_name : this.user.display_name, phone_number : this.user.phone_number, country_id : this.user.country_id // uploadUrl : urls.update_profiles_endpoint, }
|

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.