Validation::add

public add (mixed $field, Phalcon\Validation\ValidatorInterface $validator) Adds a validator to a field

Validation

extends abstract class Phalcon\Di\Injectable implements Phalcon\Events\EventsAwareInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\ValidationInterface Source on GitHub Allows to validate data using custom or built-in validators Methods public setValidators (mixed $validators) ... public __construct ([array $validators]) Phalcon\Validation constructor public Phalcon\Validation\Message\Group validate ([array | object $data], [object $entity]) Validate a set of data according to a set of rul

Validation

Phalcon\Validation is an independent validation component that validates an arbitrary set of data. This component can be used to implement validation rules on data objects that do not belong to a model or collection. The following example shows its basic usage: use Phalcon\Validation; use Phalcon\Validation\Validator\Email; use Phalcon\Validation\Validator\PresenceOf; $validation = new Validation(); $validation->add( "name", new PresenceOf( [ "message" => "Th

Validating Models

Validating Data Integrity Phalcon\Mvc\Model provides several events to validate data and implement business rules. The special “validation” event allows us to call built-in validators over the record. Phalcon exposes a few built-in validators that can be used at this stage of validation. The following example shows how to use it: namespace Store\Toys; use Phalcon\Mvc\Model; use Phalcon\Validation; use Phalcon\Validation\Validator\Uniqueness; use Phalcon\Validation\Validator\InclusionIn; class

Using Views

Views represent the user interface of your application. Views are often HTML files with embedded PHP code that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application. Phalcon\Mvc\View and Phalcon\Mvc\View\Simple are responsible for the managing the view layer of your MVC application. Integrating Views with Controllers Phalcon automatically passes the execution to th

Using PHP Built-in webserver

As of PHP 5.4.0, you can use PHP’s on built-in web server for development. To start the server type: php -S localhost:8000 -t /public If you want to rewrite the URIs to the index.php file use the following router file (.htrouter.php): if (!file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])) { $_GET['_url'] = $_SERVER['REQUEST_URI']; } return false; and then start the server from the base project directory with: php -S localhost:8000 -t /public .htrouter.php Then point your browser to h

Using Controllers

Actions are methods on a controller that handle requests. By default all public methods on a controller map to actions and are accessible by a URL. Actions are responsible for interpreting the request and creating the response. Usually responses are in the form of a rendered view, but there are other ways to create responses as well. For instance, when you access a URL like this: http://localhost/blog/posts/show/2015/the-post-title Phalcon by default will decompose each part like this: Phalcon

Unit testing

Writing proper tests can assist in writing better software. If you set up proper test cases you can eliminate most functional bugs and better maintain your software. Integrating PHPunit with phalcon If you don’t already have phpunit installed, you can do it by using the following composer command: composer require phpunit/phpunit or by manually adding it to composer.json: { "require-dev": { "phpunit/phpunit": "~4.5" } } Once phpunit is installed create a directory called ‘test

Understanding How Phalcon Applications Work

If you’ve been following the tutorial or have generated the code using Phalcon Devtools, you may recognize the following bootstrap file: use Phalcon\Mvc\Application; // Register autoloaders // ... // Register services // ... // Handle the request $application = new Application($di); try { $response = $application->handle(); $response->send(); } catch (\Exception $e) { echo "Exception: ", $e->getMessage(); } The core of all the work of the controller occurs when handle

Tutorial 7: Creating a Simple REST API

In this tutorial, we will explain how to create a simple application that provides a RESTful API using the different HTTP methods: GET to retrieve and search data POST to add data PUT to update data DELETE to delete data Defining the API The API consists of the following methods: Method URL Action GET /api/robots Retrieves all robots GET /api/robots/search/Astro Searches for robots with ‘Astro’ in their name GET /api/robots/2 Retrieves robots based on primary key POST /api/robots Adds a new r