2014-03-19 64 views
1

我曾經使用Yii框架。我想用Phalcon製作項目。我無法在Phalcon上找到驗證場景。在Phalcon上正確實施它的最佳方式是什麼?Phalcon驗證場景

在此先感謝。

回答

4

任何數據驗證:

<?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

如果您正在使用的模型工作:

<?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

型號也有活動,所以您可以添加任何邏輯你需要在這些功能:

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

2

我想用表格CRUD因爲他們是非常動態的,可重複使用。 您可以使用選項在表單中實現該功能。

您可以傳遞額外的選項來形成和行爲像一個場景。

您可以點擊這裏

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

表格構造在你的控制器,你可以通過$選項

<?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); 

    } 
} 

而且你的形式看起來應該像什麼這樣

<?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 
     } 

    } 
} 

現在您可以添加多個場景並根據場景採取行動。