• Resolved sellyk

    (@sellyk)


    Hello,
    Using the custom fields add-on, I am able to successfully require various fields – except that of ‘PRICE’. Despite setting the ‘price’ fields to be required on all our forms, users are still able to proceed without inputing a price (of what it is they are selling).

    I looked at previous threads related to this matter and I tried using the below code, that unfortunately didn’t work. I added it to the functions.php:

    add_filter( "adverts_form_load", "customize_adverts_add" );
    function customize_adverts_add( $form ) {
      if( $form['name'] != "advert" ) {
        return $form;
      }
      foreach( $form["field"] as $key => $field ) {
        if( $field["name"] == "adverts_price" ) {
            $form["field"][$key]["validators"] = array();
            $form["field"][$key]["validators"][] = array( "name" => "is_required" );
        }
      }
      return $form;
    }

    Please Help. Thanks.

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

    (@gwin)

    Hi,
    this is because the price on submit is filtered and technically it is never empty (just set to 0), if you do not want to allow 0 to be a valid value in price field then you can add the code below in your theme functions.php file it should fix it

    
    add_filter( "adverts_form_load", "adverts_form_load_11115" );
    function adverts_form_load_11115( $form ) {
        if( $form['name'] != "advert" ) {
            return $form;
        }
        foreach( $form["field"] as $key => $field ) {
            if( $field["name"] != "adverts_price" ) {
                continue;
            }
            adverts_form_add_filter("no_leading_zero", array(
                "callback" => "no_leading_zero"
            ));
            $form["field"][$key]["filter"][] = array( "name" => "no_leading_zero" );
            $form["field"][$key]["validator"] = array( array( "name" => "is_required" ) );
        }
        return $form;
    }
    function no_leading_zero( $data ) {
        return ltrim( $data, "0" );
    }
    
    Thread Starter sellyk

    (@sellyk)

    Hi Greg,
    Thank you for this snippet. It worked out well!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Price Required Not Working’ is closed to new replies.