Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm very glad that there is such a dedicated website for WP.

My problem is data validation upon saving attachment fields. I have added many custom fields to attachment/media library dialog, most of them are marked as mandatory. (Ex. $form_fields["some_custom_field"]["required"] = true;) But this just adds the class, but no validation or checking is done on WP side, so I want to check them myself with functions attached to attachment_fields_to_save filter. I can check and so on, BUT THE QUESTION IS: How to validate these fields, so if validation do not pas - WP returns an error in it's regular style, just like in registration form if you fill bad email.

In registration process in register_post action you can do this $errors->add('some_error_code', "<strong>Klaida</strong>: Nenurodėte įmonės kodo"); and it will not save data and return to visitor an error. I want that in saving attachment form :D

share|improve this question
    
Please consider to create the answer below and accept it (for posterity). –  Max Yudin May 9 '12 at 9:58
    
@Tommix - please do not post answers in the question. Post it as an answer and then accept it (you will have to wait 24 hours to accept it...), thanks :) –  Stephen Harris May 9 '12 at 10:07
    
I'll do that when it will allow me :) –  Tommixoft May 9 '12 at 11:31
add comment

2 Answers

up vote 1 down vote accepted

THE ANSWER

Well as it happens many times in my life - i found the answer myself. For people who struggles as me here the answer:

$post['errors']['field_name']['errors'][] = __('Error text here.');

if you do that - error text will be displayed at the bottom of that field. In my case this doesn't work because i'm using inline media uploader, but with standard WP media uploader (the popup version) it's working just fine.

BTW best article about custom fields is here: http://net.tutsplus.com/tutorials/wordpress/creating-custom-fields-for-attachments-in-wordpress/

share|improve this answer
add comment

I think this should help you: http://www.kevinleary.net/add-custom-meta-fields-media-attachments-wordpress/

share|improve this answer
    
Thanks for link but it doesn't help, there was nothing new to me. He did not do validation of data, which i need :) –  Tommixoft May 9 '12 at 9:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.