Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    You can make Category and Location fields required by adding following code to your theme functions.php file

    add_filter( 'adverts_form_load', 'my_adverts_form_require_category' );
    function my_adverts_form_require_category( $form ) {
        if($form['name'] != 'advert') {
            return $form;
        }
    
        if(!is_admin()) {
            foreach($form['field'] as $k => $field) {
                if(in_array($field['name'], array('advert_category', 'adverts_location'))) {
                    $field['is_required'] = true;
                    if( ! isset($field['validator']) || !is_array($field['validator'])) {
                        $field['validator'] = array();
                    }
                    $field['validator'][] = array( 'name' => 'is_required' );
    
                    $form['field'][$k] = $field;
                    break;
                }
            }
        }
    
        return $form;
    }

    This will do both make the field required and show red asterisk next to field name, you can use it in your custom field.

    Thread Starter hiltonmw

    (@hiltonmw)

    I’m getting a 500 server error when I add this code… could there be a syntax error somewhere in it?

    Plugin Author Greg Winiarski

    (@gwin)

    I tested this code on my dev site it works fine, you might only want to remove the ‘break;’ line other than that i can’t see any other problem with it.

    You can check your site error_log file for actual error message, as the 500 error is just a general error message.

    Thread Starter hiltonmw

    (@hiltonmw)

    It’s working now – although – it doesn’t show the “asterisk” in red next to category or location… is there a way to make that show up – just like it does with the other required fields?

    Plugin Author Greg Winiarski

    (@gwin)

    I am not sure, the line

    $field['validator'][] = array( 'name' => 'is_required' );

    is responsible for both adding asterisk and making the field required, if you have it then asterisk should show, is it visible on your site for Email field?

    This doesn’t work with location unfortunately, however, does work with category. I also tried with price and doesn’t work. Anyway to get it to work with price and location?

    Regards,
    mark

    Plugin Author Greg Winiarski

    (@gwin)

    When i am adding ‘adverts_price’ to the fields array this seems to be working fine for me and the price is required field.

    Hi Greg,

    When I add only adverts_price or adverts_location by themselves (literally one at a time) without any others in the array, the red * shows up. But, the moment I add all the others to the array, the red * disappears from all but the advert_category item.

    Another thing I noticed, when adding adverts_price (by itself, no other) to the array, the red * shows up, but when you submit a blank form the “Field cannot be empty” DOES NOT work as intended.

    However, when adding adverts_location (by itself, no other) to the array, the red * shows up, and when you submit a blank form the “Field cannot be empty” DOES work as intended.

    I’m baffled. I tried with my theme and the default WordPress theme. Also tried adding the function in the functions.php and a custom plugin file and same problem.

    Could this be a plugin conflict? The only thing I haven’t done is disable all plugins.

    UPDATE: Disabled all plugins while using default theme, same issue occurs.

    Seems when adding in array format things act up. I’m not a coder, but, can these be added individually to that function instead of in an array? And, would that even make a difference? Maybe worth trying. Idk.

    Plugin Author Greg Winiarski

    (@gwin)

    From the code i posted in second message try removing line break; this should allow to have more than one element in array.

    Thanks Greg. Interesting. I removed the break and now all the fields (category, description, price, and location) have a red * next to them. However, if I submit a blank form all fields except the “price” field return the “Field cannot be empty” message. Any other tricks up your sleeve to get the price field to work as well?

    Regards,

    Mark

    Plugin Author Greg Winiarski

    (@gwin)

    Currently i am afraid not, the Price field is filtered before validation (if it’s empty it is set to 0) so this field actually is never empty as far as the validator is concerned.

    Thanks for looking into this and providing a solution Greg. I understand about the Price field and can make do without this one.

    Regards,

    Mark

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Location and Category Required’ is closed to new replies.