Jump to content

Customizing error messages for InputFields


gRegor
 Share

Recommended Posts

Hi, all. I've read through most of soma's thread "Create simple forms using API" and think I understand the basics pretty well. I fall in love more and more with Processwire each day. :)

First, I am wondering if it's possible to not display the error messages inline, with each form field, and instead show a list of errors at the top of the form. The typical UI I use for form error messages is "Please fix the following errors and re-submit the form" followed by an unordered list of the error messages. I see that the getErrors() method returns an array of the field names and error messages, so that is simple enough. I'm not sure how to turn off the error messages that display with each form field, though.

Secondly, I am wondering if it's possible to customize the error message for each field, via the API. I understand the text can be internationalized, but I am looking for more specific messages like "Please enter your First Name" instead of "First Name: Missing required value."

Thanks for any help!

Link to comment
Share on other sites

Thanks, adrian. That actually adds to the error message array, so both messages appear, which is undesirable. I had experimented with that a bit previously.

My form is set up in the same way as soma's first post in that thread, using the render() method to display the form after building all the fields via the API.

Link to comment
Share on other sites

For the sake of time (I need to get this form set up rather quickly), I'm doing something similar to Soma's code here: https://gist.github.com/somatonic/5233338

I'm still curious if there's a method to achieve what I want without additional code to process the fields - using mostly native API methods, that is. Please let me know any suggestions.

Link to comment
Share on other sites

  • 2 weeks later...

You could retrieve the errors from the Inputfield, clear them out, and then replace with your own:

$errors = $inputfield->getErrors(true); // true=clear the errors out
// $errors is a plain PHP array of error messages... 
// ...if you want to iterate it or do anything with it
$inputfield->error('Your custom error message'); 
 
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...