0

I use Laravel 5.5 and PHP 7, and I'm creating a form like this.

<form id="form" name="xxx" action="post">
  <input type="text" name="body_color[1][en]">
  <button type="submit" name="submit">Submit</button>
</form>

I post data via Ajax, and I receive the following response from Laravel when I have an error.

error: body_color.1.en: ["The body color.1.en field is required."] success: false

If Laravel doesn't replace "[" and "]" with a dot, I can show an error message on the browser efficiently using jQuery.

$('#form *[name=body_color[1][en]]').after('input error!');

How can I show a simple error message in the web browser?

2 Answers 2

1

With your jQuery requirement, a simple regex replacement should work for this case:

var str = "body_color.1.en"
str.replace(/\.(.+)\./, "[$1][") + "]" // returns body_color[1][en]

Or you could just split the string on the dots and rebuild it:

str.split('.')  // returns ['body_color', '1', 'en'];

There are many existing Javascript classes out there to handle the error responses, ex: https://github.com/spatie/form-backend-validation

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

Comments

0

run your response through JSON.parse(response) and you should be able to use standard json dot syntax to step through the data.

var r = JSON.parse(response):

console.log(r);

Comments

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.