guycas's avatar

Ajax form validation

Hi,

I'm having an issue validating a form properly on 5.1 using Polymer Iron Form.

My schema defines an email field to be unique in the database and I have a form validator with the same validation rule.

When I submit the form normally (no ajax), it all works as expected and I can render the errors to the view. However, when I use ajax, instead of getting a 422 response with an error object as the documents suggest, I get a 500 error.

Looking at my error logs it looks like the 500 error is because I'm trying to insert an email that already exists in the database. This means that Laravel skips form validation and I'm getting the error from the database.

When I submit correct values (no duplicate email entries) with or without ajax everything works fine.

I'm not sure what I'm missing...?

 /**
     * Store a newly created resource in storage.
     *
     * @param  LeadRequest  $request
     * @return Response
     */
    public function store(LeadRequest $request)
    {


        $input = $request->json()->all();
        //$input = $request->input();

        $location = Location::get($request->ip());
        $input['location'] = $location;

        return Lead::create($input);
    }
/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'unique:leads',
        ];
    }
0 likes
7 replies
billmurrin's avatar

Is the AJAX being used for an update method or for the creation method?

guycas's avatar
guycas
OP
Best Answer
Level 3

Managed to fix it. Posting here in case someone else has the same problem.

Firstly it wasn't that the validator wasn't being triggered, it just wasn't getting the input data. Because I didn't set the field to be required and only be unique, an empty value passed validation. When I added a 'required' rule I got an error.

The reason it wasn't receiving data is because the way headers are set for iron-forms In Polymer v1.0. They are missing certain keys that Laravel looks for in order to detect a json ajax request.

once I set the headers in the Iron Form element everything was working perfectly! This aspect of iron form had to be "hacked" to work with Laravel. Hopefully will have time in the next couple of days to submit a pull request to the Polymer project to make it more extensible. for now this is what I needed to add to the element (notice that I also had to add the csrf token from laravel):

// TODO Extend the element to include this and remove hack from this element
      this._requestBot.headers = {'X-CSRF-Token' : json._token, "X-Requested-With": "XMLHttpRequest", "Content-Type" : "application/json"};
maximl337's avatar

try telling laravel which column to validate against

public function rules()
    {
        return [
            'email' => 'unique:leads,email', // the name of the email column in leads table
        ];
    }
guycas's avatar

Thanks maximl337! Just found what the problem was and posted my solution.

Please or to participate in this conversation.