2017 © Pedro Peláez
 

library recaptcha-field

A field for protecting forms against spam in ATK14 applications

image

atk14/recaptcha-field

A field for protecting forms against spam in ATK14 applications

  • Wednesday, March 28, 2018
  • by yarri
  • Repository
  • 2 Watchers
  • 0 Stars
  • 122 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 4 % Grown

The README.md

RecaptchaField

RecaptchaField is a field for protecting forms against spam in ATK14 applications., (*1)

It uses reCAPTCHA v2 from Google (see https://www.google.com/recaptcha/intro/index.html), (*2)

Installation

Just use the Composer:, (*3)

cd path/to/your/atk14/project/
composer require atk14/recaptcha-field

You must define two constants in config/settings.php. Get their right values at https://www.google.com/recaptcha/admin#list, (*4)

<?php
// file: config/settings.php

// ...
define("RECAPTCHA_SITE_KEY","fksjfu2094389SAKJDPOSAIIaskalkslamcbuyid");
define("RECAPTCHA_SECRET_KEY","pwofe994883eiDJKHFISIYTTSSSSSkfdt7poieqnx");

Optionally you can symlink the RecaptchaField files into your project:, (*5)

ln -s ../../vendor/atk14/recaptcha-field/src/app/fields/recaptcha_field.php app/fields/recaptcha_field.php
ln -s ../../vendor/atk14/recaptcha-field/src/app/widgets/recaptcha_widget.php app/widgets/recaptcha_widget.php

Usage in a ATK14 application

In a form:, (*6)

<?php
// file: app/forms/users/create_new_form.php
class CreateNewForm extends ApplicationForm {

  function set_up(){
    $this->add_field("firstname", new CharField([
      "label" => "Firstname",
      "max_length" => 200,
    ]));

    $this->add_field("lastname", new CharField([
      "label" => "Lastname",
      "max_length" => 200,
    ]));

    // other fields

    $this->add_field("captcha",new RecaptchaField([
      "label" => "Spam protection"
    ]));
  }

  function clean(){
    list($err,$values) = parent::clean();

    // perhaps you may not want to have "captcha" in the cleaned data
    if(is_array($values)){ unset($values["captcha"]); }

    return [$err,$values];
  }
}

In a template (a shared template from the Atk14Skelet is used):, (*7)

<h1>User Registration</h1>

{form}

  <fieldset>
    {render partial="shared/form_field" fields="firstname,lastname,..."}
  </fieldset>

  <fieldset>
    {render partial="shared/form_field" fields="captcha"}
  </fieldset>

  <button type="submit">Register</button>

{/form}

In a controller:, (*8)

<?php
// file: app/controllers/users_controller.php
class UsersController extends ApplicationController {

  function create_new(){
    if($this->request->post() && ($values = $this->form->validate($this->params))){
      // There's no need to care about the $values["captcha"] since it was unset in CreateNewForm::clean()
      User::CreateNewRecord($values);
      $this->flash->success("Your registration has been successfully performed");
      $this->_redirect_to("main/index");
    }
  }
}

Example of usage

The RecaptchaField is used in the registration form on ATK14 Forum for instance., (*9)

License

RecaptchaField is free software distributed under the terms of the MIT license, (*10)

The Versions

28/03 2018

dev-master

9999999-dev https://github.com/atk14/RecaptchaField

A field for protecting forms against spam in ATK14 applications

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

url captcha field forms recaptcha atk14

27/03 2018

v0.1

0.1.0.0 https://github.com/atk14/RecaptchaField

ATK14 form field for protecting forms against spam

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

url field forms atk14