• Resolved tanckom

    (@tanckom)


    Hello, So i tried to implement a min and max number into a text field (info take from here: https://wpadverts.com/documentation/forms-api/).

    I included in my wpadverts/includes/functions.php this codes:

    add_filter( "adverts_form_load", "my_adverts_form_load" );
        function my_adverts_form_load( $form ) {
            if( $form["name"] != "advert" ) {
                return $form;
            }
         $form["field"][] = array(            
                "name" => "check",
                "type" => "adverts_field_text",
                "order" => 25,
                "label" => "Input a number between 10 and 100",
                "is_required" => true,
                "validator" => array(
        		array(
            		"name" => "in_between",
            		"min" => 10,
            		"max" => 100,
        		)),
                "validator" => array( 
                    array( "name" => "is_required" ),
                )
                
            );
      return $form;
        }
    
    function in_between( $data, $params = null ) {
            if( isset( $params["min"] ) && $data < $params["min"] ) {
                return "to_small";
            } 
            
            if( isset( $params["max"] ) && $data > $params["max"] ) {
                return "to_big";
            } 
            
            return true;
        }
    adverts_form_add_validator("in_between", array(
            "callback" => "in_between",
            "label" => "In Between",
            "default_error" => __( "Incorrect value.", "adverts" ),
            "message" => array(
                "to_small" => "Value has to be greater than %min%.",
                "to_big" => "Value has to be smaller than %max%."
            ),
            "validate_empty" => false
        ));

    However it’s not working. Whenever I insert a number outside of min-max, it continues like the limits do not exists. How can I change this?

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

    (@gwin)

    Hi, your field definition is incorrect you are using the “validator” key twice so the first validator (in_between) is cancelled with second one (is_required), to use both in_between and is_required your field definition should be

    
    function my_adverts_form_load( $form ) {
        if( $form["name"] != "advert" ) {
            return $form;
        }
    
        $form["field"][] = array(
            "name" => "check",
            "type" => "adverts_field_text",
            "order" => 25,
            "label" => "Input a number between 10 and 100",
            "is_required" => true,
            "validator" => array(
        	    array(
            	"name" => "in_between",
                    "params" => array("min" => 10, "max" => 100),
        	    ),
                array(
                    "name" => "is_required"
                ),
            )
        );
        return $form;
    }
    
    Thread Starter tanckom

    (@tanckom)

    Okay thanks, worked.

    My final question about this one is, how can i use random number?

    I found out if I give minimun a random number between 1-10 and max 11-20 it wont work always because for example:
    Current randoms are: min:1 ; max 20 and you insert 4 and hit preview.
    Page is going to reload randoms and the new randoms are:
    min: 9 ; max:13
    The input from before didnt work because it was compared to the new randoms. How can i check the input with current randoms instead with the next (because they are not always true).
    Example:

    add_filter( “adverts_form_load”, “my_adverts_form_load” );
    function my_adverts_form_load( $form ) {
    if( $form[“name”] != “advert” ) {
    return $form;
    }
    $form[“field”][] = array(
    “name” => “check”,
    “type” => “adverts_field_text”,
    “order” => 25,
    “label” => “Input a number between 10 and 100”,
    “is_required” => true,
    “validator” => array(
    array(
    “name” => “in_between”,
    “params” => array(“min” => rand ( 1 , 10 ), “max” => rand ( 11 , 20 )),
    ),
    array(
    “name” => “is_required”
    ),
    )

    );
    return $form;
    }

    function in_between( $data, $params = null ) {
    if( isset( $params[“min”] ) && $data < $params[“min”] ) {
    return “to_small”;
    }

    if( isset( $params[“max”] ) && $data > $params[“max”] ) {
    return “to_big”;
    }

    return true;
    }
    adverts_form_add_validator(“in_between”, array(
    “callback” => “in_between”,
    “label” => “In Between”,
    “default_error” => __( “Incorrect value.”, “adverts” ),
    “message” => array(
    “to_small” => “Value has to be greater than %min%.”,
    “to_big” => “Value has to be smaller than %max%.”
    ),
    “validate_empty” => false
    ));

    Plugin Author Greg Winiarski

    (@gwin)

    I am not sure, you would probably need to generate this numbers once and store them in user session or user cookie.

    Thread Starter tanckom

    (@tanckom)

    I tried with user session too, but somehow it didnt store them, is there something that disables it? ( I also used start_session() )

    Plugin Author Greg Winiarski

    (@gwin)

    I don’t know, this is more of a general PHP programming question, maybe sessions on your server do not work or you are starting the session after some content has been sent to browser n which case the session won’t start.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Validator Doc not working (FORMS API)’ is closed to new replies.