Tuesday, April 11, 2017

laravel fom validation during edit not working

I am using laravel 5.2.In that have form for to edit the country information description country_code these are my form field.After clicking the edit button of particular county the contry information will be auto filled according to country_id. When user not interested to updating the country code then it should return true and validate ny form

$("#edit_country").validate({
    rules: {
        desc: {
            required: true
        },
        country_code: {
            required: true,               
            remote: {
                url: "/country/check-country-code-exist",
                type: "post",
                data: {
                    action_type:"edit",
                    old_country_code:function(){return $('#old_country_code').val()},
                    country_code: function () {return $("#country_code_edit").val();},
                }
            }
        },        

    },
    messages: {
        desc: {
            required: "Please enter description",
        },
        country_code: {               
            required: "Please enter country code",
        },


    },
    submitHandler: function (form) {
        updateCountry()
    }
});

I want to validate my form when the user not updating the country code and just updating the description.In this case old_country_code and country_code are same

This is my controller function for cheeking the availability of country code

 public function checkCountryCodeExist(Request $request){
        if($request->action_type=='edit' && $request->country_code==$request->old_country_code ){
        return json_encode(array(true));
    }           

    }

This is my form

<form id="edit_country" name="edit_country" method="post">
                    <div class="form-group">
                        <label for="desc">Description</label>
                        <input type="text" id="desc_edit" name="desc" class="form-control editable" onfocus="return removeError(event)" readonly>
                        <div id="desc_edit_error" class="error"></div>
                    </div>
                    <div class="form-group">
                        <label for="country_code">Country Code</label>
                        <input type="text" id="country_code_edit" name="country_code" class="form-control editable" onfocus="return removeError(event)" readonly>
                        <input type="hidden" id="old_country_code" name="old_country_code">

                        <div id="country_code_edit_error" class="error"></div>
                    </div>                    

                    <input type='hidden' name='_token' value="{!! csrf_token() !!}" /> 
                    <input type='hidden' id='id_edit' name="id" value="" /> 
                    <button type="submit" class="btn btn-primary" name="edit_country_submit" id="edit_country_submit" > Update </button>
                </form>

When user not interested to updating the country code then it should return true and validate ny form



via vaibhav

Advertisement