• Resolved tanckom

    (@tanckom)


    Hello Greg, me again.

    Sorry that I ask so many questions however I really want to get the most out of this tool ??

    One thing I would appreciate is to have a custom textfield with only numbers.

    F.ex. the price textfield. This textfield only allows numbers however adds a currency sign to it :/ (which is why I cant use the filter-money class).

    Would this be possible?
    I currently have this:
    My wpadverts/includes/functions.php

    $form["field"][] = array(            
                "name" => "adverts_number",
                "type" => "adverts_field_text",
                "order" => 19,
                "label" => "Followers",
                "class" => "adverts-filter-number",
                "is_required" => true,
                "validator" => array( 
                    array( "name" => "is_required" ),
                )
            );

    My wpadverts/assets/js/adverts-frontend.js

    if($(".adverts-filter-number").length > 0) {
            $(".adverts-filter-number").autoNumeric('init',{ 
            aSep: '.', 
            mDec: '0'});
        }

    And for the rest, idk what else I could do so that this would work.

    thanks in advance!

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

    (@gwin)

    Right now to have an input field which allows numbers only you would need to register new field

    
    add_action( 'plugins_loaded', 'register_number_field');
    function register_number_field() {
        adverts_form_add_field("my_custom_number_field", array(
            "renderer" => "my_custom_number_field",
            "callback_save" => "adverts_save_single",
            "callback_bind" => "adverts_bind_single",
        ));
    }
    
    function my_custom_number_field( $field ) {
        $attr = array(
            "type" => "number",
            "step" => 1,
            "name" => $field["name"],
            "id" => $field["name"],
            "value" => isset($field["value"]) ? $field["value"] : "",
            "placeholder" => isset($field["placeholder"]) ? $field["placeholder"] : null,
            "class" => isset($field["class"]) ? $field["class"] : null
        );
        
        if( isset( $field["attr"] ) && is_array( $field["attr"] ) ) {
            foreach( $field["attr"] as $key => $value ) {
                if( $value !== null && is_scalar( $value ) ) {
                    $attr[$key] = $value;
                }
            }
        }
        
        $html = new Adverts_Html( "input", $attr );
        
        echo $html->render();
    }
    

    Next change your field definition to

    
    $form["field"][] = array(            
      "name" => "adverts_number",
      "type" => "my_custom_number_field",
      "order" => 19,
      "label" => "Followers",
      "class" => "adverts-filter-number",
      "is_required" => true,
      "validator" => array( 
        array( "name" => "is_required" ),
      )
     );
    

    It should now allow only integers.

    Thread Starter tanckom

    (@tanckom)

    I inserted the first code into functions.php and now is the field disabled? Seems the code is not working?

    Plugin Author Greg Winiarski

    (@gwin)

    You can replace line

    
    add_action( 'plugins_loaded', 'register_number_field');
    

    with

    
    add_action( 'init', 'register_number_field');
    

    other than that the code seems to work fine on my dev server.

    Plugin Author Greg Winiarski

    (@gwin)

    Deleted

    • This reply was modified 8 years, 6 months ago by Greg Winiarski. Reason: wrong thread
    Thread Starter tanckom

    (@tanckom)

    Okay it’s working now. However what I wanted to achieve was to have a same field like the Price field:
    – Only allowed to insert numbers, when trying to insert letters, they wont show up
    – Automatically add a . between thousands
    – No decimal or comma’s
    – No currency

    • This reply was modified 8 years, 6 months ago by tanckom.
    Plugin Author Greg Winiarski

    (@gwin)

    Try removing this line "step" => 1, from my code, if that won’t help, then i suppose having such field is not possible right now.

    Thread Starter tanckom

    (@tanckom)

    Didn’t work. Thanks anyway

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom textfield with only number input’ is closed to new replies.