Question

I used to use Yii framework. I would like to make project using Phalcon. I could not find validation scenario on Phalcon. What is the best way to correctly implement it on Phalcon?

Thanks in advance.

Was it helpful?

Solution

Any data validation:

<?php

use Phalcon\Validation\Validator\PresenceOf,
    Phalcon\Validation\Validator\Email;

$validation = new Phalcon\Validation();

$validation->add('name', new PresenceOf(array(
    'message' => 'The name is required'
)));

$validation->add('email', new PresenceOf(array(
    'message' => 'The e-mail is required'
)));

$validation->add('email', new Email(array(
    'message' => 'The e-mail is not valid'
)));

$messages = $validation->validate($_POST);
if (count($messages)) {
    foreach ($messages as $message) {
        echo $message, '<br>';
    }
}

http://docs.phalconphp.com/en/1.2.6/reference/validation.html

If you are working with models:

<?php

use Phalcon\Mvc\Model\Validator\InclusionIn,
    Phalcon\Mvc\Model\Validator\Uniqueness;

class Robots extends \Phalcon\Mvc\Model
{

    public function validation()
    {

        $this->validate(new InclusionIn(
            array(
                "field"  => "type",
                "domain" => array("Mechanical", "Virtual")
            )
        ));

        $this->validate(new Uniqueness(
            array(
                "field"   => "name",
                "message" => "The robot name must be unique"
            )
        ));

        return $this->validationHasFailed() != true;
    }

}

http://docs.phalconphp.com/en/1.2.6/reference/models.html#validating-data-integrity

models also have events, so you can add any logic you need in these functions:

http://docs.phalconphp.com/en/1.2.6/reference/models.html#events-and-events-manager

OTHER TIPS

I would like to use forms for CRUD as they are very dynamic and reusable. You can achieve that in forms using options.

You can pass additional options to form and act like a scenario.

You can check Form constructor here

https://docs.phalconphp.com/en/latest/api/Phalcon_Forms_Form.html

In your controller you can pass $options

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function insertAction()
    {
        $options = array();
        $options['scenario'] = 'insert';
        $myForm = new MyForm(null, $options);  

        if($this->request->hasPost('insert')) {

            // this will be our model
            $profile = new Profile(); 

            // we will bind model to form to copy all valid data and check validations of forms
            if($myForm->isValid($_POST, $profile)) {
                $profile->save();
            }
            else {
                echo "<pre/>";print_r($myForm->getMessages());exit();
            }
        } 
    }

    public function updateAction()
    {
        $options = array();
        $options['scenario'] = 'update';
        $myForm = new MyForm(null, $options);

    }
}

And your form should look like something this

<?php   

// elements
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;

// validators
use Phalcon\Validation\Validator\PresenceOf;    

class MyForm extends Form {


    public function initialize($entity = null, $options = null) {    

        $name = new Text('first_name');
        $this->add($name);
        if($options['scenario'] == 'insert') {
            // at the insertion time name is required
            $name->addValidator(new PresenceOf(array('message' => 'Name is required.')));
        }
        else {
            // at the update time name is not required
            // as well you can add more additional validations
        }

    }
}

now you can add multiple scenarios and act based on scenarios.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top