2017 © Pedro Peláez
 

library forms

Extend Nette Forms for WordPress use.

image

wenprise/forms

Extend Nette Forms for WordPress use.

  • Saturday, July 21, 2018
  • by iwillhappy1314
  • Repository
  • 1 Watchers
  • 0 Stars
  • 73 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 109 % Grown

The README.md

wenprise-forms

Extend Nette Forms for WordPress use., (*1)

Basic usage: Nette Form, (*2)

Usage

Quick start

use Wenprise\Forms\Form;

$form = new Form;
wprs_form( $form );

// Set form method
$form->setMethod( 'POST' );
$form->setAction('https://www.example.com');

// Set form field
$form->addText('first_name', 'First Name');

// Set submit button
$form->addSubmit( 'send', 'Save' );

// Validate form and get form data
if ( $form->isSuccess() ) {

    $values = $form->getValues();

    $first_name = $values->first_name;

}   

Set Required

$form->addText('first_name', 'First Name')
     ->setRequired();

Add rule

$form->addPassword('re_password', 'Password again:')
     ->addRule($form::EQUAL, 'Password mismatch', $form['password']);

Rule Documation, (*3)

Add field description

$form->addText('first_name', 'First Name')
     ->setOption('description', 'This is your first name.');

With Html, (*4)

$form->setOption('description', Html::el('p')
    ->setHtml('This number remains hidden. <a href="...">Terms of service.</a>')
    );

Set condition display

$form->addColorPicker('first_name3', 'First Name')
     ->setHtmlAttribute('data-cond', '[name=first_name2] == 2');

Multiple Submit Button

$form->addSubmit('save', 'Save');
$form->addSubmit('delete', 'Delete');

if ($form->isSuccess()) {
    if ($form['save']->isSubmittedBy()) {
        ....
    }

    if ($form['delete']->isSubmittedBy()) {
        ....
    }
}

Use html in label or description

$confirm = Html::el('span')->setHtml('I agree the <a href="#">Terms of service.</a>');

$form->addCheckbox('confirm', $confirm)->setOption('description', $confirm);

Set Datastore

$form->setDatastore(new \Wenprise\Forms\Datastores\PostMetaDatastore(1, $form));

$form->save();

Fields

nonce field

$form->addCsrf('post-form', 'Nonce invaliadte');

WordPress Tinymce editor

Settings, (*5)

$form->addEditor('post_extra', 'Extra content', []);

Ajax uploader

$form->addAjaxUpload('photos', 'Photos', true, )
             ->setUrl(  admin_url( 'admin-ajax.php?action=upload' ) );

Uploader backend sample.

add_action('wp_ajax_upload', 'ajax_uploader');
add_action('wp_ajax_nopriv_upload', 'ajax_uploader');

function ajax_uploader()
{

    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');

    $attachment_id = media_handle_upload('file', 0);

    $thumb_url  = wp_get_attachment_thumb_url($attachment_id);
    $origin_url = wp_get_attachment_url($attachment_id);

    $thumb = get_post($attachment_id);

    $file_data = [
        'id'       => $attachment_id,
        'original' => $thumb->post_title,
        'size'     => $thumb->size,
        'state'    => 'SUCCESS',
        'title'    => $thumb->post_title,
        'type'     => $thumb->post_mime_type,
        'thumb'    => $thumb_url,
        'url'      => $origin_url,
    ];

    wp_send_json($file_data, 200);

    return false;
}

Slider input

Setting, (*6)

$form->addSlider('price', 'Price', []);

Data Picker

Setting, (*7)

$form->AddBirthdaypicker('_birthday', 'Date of Birth', [
    'format' => 'YYYY-MM-DD', 
    'template' => 'YYYY-MM-DD',
    'minYear' => '1900',
    'maxYear' => date("Y")
]);

Color Picker

Setting, (*8)

$form->addColorPicker('color', 'Color', []);

Chosen

$choices = [
    'php'         => 'PHP',
    'javascript'  => 'JavaScript',
    'css'         => 'CSS',
    'java'        => 'Java',
];

$form->addChosen('category', 'Category', $choices);

Multi Chosen

$form->addMultiChosen('post_tags', 'Tags', $choices);

Signature Filed

$options = [
   'width'      => '500',
   'height'     => '250',
   'border'     => '#999',
   'background' => '#f3f3f3',
];

$form->addSignature('first_name9', 'First Name', $options);

Star rating input

$options = [
    'displayOnly' => false,
    'showClear'   => false,
    'theme'       => 'krajee-svg',
    'step'        => 1,
    'min'         => 1,
    'max'         => 5,
];

$form->addStarRating('rating', 'Rating', $options);

Image Select

$options = [
    'light' => 'https://via.placeholder.com/64/EEEEEE/000000/?text=Light',
    'dark'  => 'https://via.placeholder.com/64/000000/FFFFFF/?text=Dark',
];

$form->addImagePicker('theme', 'Theme', $options);

Autocomplete Input

Source is an array or a url returns an array., (*9)

$form->addAutocomplete('name', 'Name')->setSource([
    [
        'value' => 'aaa',
        'data'  => 'AAA',
    ],
    [
        'value' => 'bbb',
        'data'  => 'BBB',
    ],
    [
        'value' => 'ccc',
        'data'  => 'CCC',
    ],
]);

If Source is an ajax url, the backend need return data as bellow., (*10)

suggestions: [
  { "value": "United Arab Emirates", "data": "AE" },
  { "value": "United Kingdom",       "data": "UK" },
  { "value": "United States",        "data": "US" }
]

Table input

$fields = [
    [
        'name'    => 'name',
        'display' => '商品名称',
        'type'    => 'text',
    ],
    [
        'name'    => 'quantity',
        'display' => '数量',
        'type'    => 'text',
    ],
    [
        'name'    => 'price',
        'display' => '单价',
        'type'    => 'text',
    ],
];

$values = [
    [
        'name'     => 'Macbook Pro',
        'quantity' => '1',
        'price'    => '8500',
    ],
    [
        'name'     => 'Pixel XL',
        'quantity' => '2',
        'price'    => '8500',
    ],
];


$form->addTableInput('table', 'Table', [], $fields)
     ->setDefaultValue($values);

Clone Input

Allow input multi text value., (*11)

$form->addCloneInput('photo1', 'Photo');

Group Input

Set a prefix or suffix for text input., (*12)

$form->addGroupInput('day1', 'Day')
     ->setPrefix('Email')
     ->setSuffix('gmail.com');

SMS input

Send SMS code, (*13)

$form->addSmsInput('photo11', '短信', )
             ->setUrl(admin_url('admin-ajax.php?action=captcha'));

Backend Example, (*14)


/** * 发送短信验证码 */ add_action('wp_ajax_captcha', 'get_validate_code'); add_action('wp_ajax_nopriv_captcha', 'get_validate_code'); function get_validate_code() { $phone = Input::get( 'phone', null ); if ( ! $phone && is_user_logged_in() ) { $phone = OpenAuth::get_open_id( 'phone', get_current_user_id() ); } $random = mt_rand( 100000, 999999 ); // 发送验证码之前,保存验证码到数据库 $code = PhoneCode::query()->firstOrCreate( [ 'phone' => $phone ] ); $code->code = $random; $code->save(); $msg = Helper::send_sms( $phone, $code->code ); wp_send_json( $msg, '200' ); }; /** * 发送短信 */ function send_sms( $mobile, $content ) { $config = Config::get( 'sms' ); // 模板接口网关 $url = "https://sms.yunpian.com/v2/sms/tpl_single_send.json"; $args = [ 'body' => [ 'apikey' => $config[ 'apikey' ], 'mobile' => $mobile, // 如果是模板短信 'tpl_id' => $config[ 'tpl_id' ], 'tpl_value' => "#code#=$content", ], ]; $result = json_decode( wp_remote_retrieve_body( wp_remote_post( $url, $args ) ) ); // 根据网关返回的数据返回消息 return [ 'code' => $result->code, 'msg' => $result->msg, ]; }

Captcha Input

 $form->AddCaptcha('captcha', 'Captcha')
             ->setUrl(admin_url('admin-ajax.php?action=captcha'));
#### Uploader backend sample.
add_action('wp_ajax_captcha', 'get_captcha');
add_action('wp_ajax_nopriv_captcha', 'get_captcha');
function get_captcha($type)
{

    header('Content-type: image/jpeg');

    // use gregwar/captcha to generate captcha.
    $builder                             = new Gregwar\Captcha\CaptchaBuilder();
    $_SESSION[ 'wprs-security-captcha' ] = $builder->build(150, 36)->getPhrase();

    $builder->build()
            ->output();

}

The Versions

21/07 2018

dev-master

9999999-dev

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

21/07 2018

1.2.3

1.2.3.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

10/07 2018

1.2.2

1.2.2.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

08/07 2018

1.2.1

1.2.1.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

06/07 2018

1.2.0

1.2.0.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

04/07 2018

1.1.3

1.1.3.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

03/07 2018

1.1.2

1.1.2.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

28/06 2018

1.1.1

1.1.1.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

09/06 2018

1.1.0

1.1.0.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

04/06 2018

1.0.1

1.0.1.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

30/12 2017

v0.1-beta

0.1.0.0-beta

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires

 

30/12 2017

1.0

1.0.0.0

Extend Nette Forms for WordPress use.

  Sources   Download

MIT

The Requires