2014-01-17 48 views
2

我希望能够在启动保存之前验证创建相关模型的表单。我正在拯救一个拥有多个相关模型的组织。由于我有许多多对多关系,所以我必须先创建主模型,然后再从输入数据中分别创建每个相关模型。在创建初始组织后,我在组织资源库中调用'createRelated'方法。Laravel 4中相关模型验证的最佳实践?

public function createRelated($input) 
{ 
    $this->orgtypes()->attach(1, ['objectstate_id' => ORGANIZATION_STARTED]); 
    $this->addresses()->create(array_only($input, ['street1', 'zip', 'city', 'state_code'])); 
    for ($i=0; $i < 2; $i++) 
    { 
     if($input['phone_numbers_'.$i.'_number']) 
     { 
      $this->phoneNumbers()->create(['phone_number_type_id' => $input['phone_numbers_'.$i.'_phone_number_type_id'], 'number' => $input['phone_numbers_'.$i.'_number']]); 
     } 
    } 
    $this->procedures()->attach($input['procedures']);  
    $input['objectstate_id'] = USER_STARTED; 
    $input['password'] = $this->encryptPassword($input['password']); 
    $this->users()->create(array_only($input, ['first_name', 'last_name', 'email', 'password', 'objectstate_id'])); 
    $this->profileElements()->create(['type' => 'short_description', 'content' => $input['short_description']]); 
} 

有叫出每个输入并将其发送到其相应的模型似乎并没有我的权利,但是这是我听到的IRC为“正确”的方式。但是现在我来验证一下,即使输入可能违反验证规则,目前也没有验证这些模型的创建。我很惊讶没有命名约定(我已经看到)清楚地标识了相关的模型输入,因此它可以全部验证和创建,而无需像上面那样进行手动处理。在开始上述保存事务之前,如何验证相关输入数据的任何想法?

我对组织验证的样子:

<?php namespace Acme\Validators\Organizations; 

use Acme\Validators\ValidatorAbstract; 

class Validator extends ValidatorAbstract { 

    /** 
    * Validation rules 
    */ 
    protected $rules = array(
     'organization_name' => 'required' 
    ); 

    /** 
    * Custom Validation Messages 
    */ 
    protected $messages = array(
    ); 

} 

<?php namespace Acme\Validators; 

use Illuminate\Support\MessageBag as MessageBag; 


/* 
* This class defines abstract Validator methods 
*/ 

abstract class ValidatorAbstract implements ValidatorInterface { 
    protected $validator; 

    protected $input; 

    protected $errors; 

    /** 
    * @param array $input 
    * 
    */ 
    public function __construct($input = NULL, MessageBag $bag) 
    { 
     $this->input = $input ?: \Input::all(); 

     $this->validator = \Validator::make($this->input, $this->rules, $this->messages); 
     $this->errors = $bag; 
    } 

    /** 
    * Run validation on input. 
    * 
    * @return boolean 
    */ 
    public function passes() 
    { 
     if($this->validator->passes()) 
     { 
      return true; 
     } 
     $this->errors = $this->validator->messages(); 

     return false; 
    } 

    /** 
    * Get all errors stored. 
    * 
    * @return MessageBag 
    */ 
    public function getErrors() 
    { 
     return $this->errors; 
    } 

    /** 
    * Add new error. 
    * 
    * @return MessageBag 
    */ 
    public function addError($key, $message) 
    { 
     return $this->errors->add($key, $message); 
    } 

} 

我的抽象库类,我在炽热

<?php namespace Acme\Repositories; 

// use Eloquent; 
use LaravelBook\Ardent\Ardent; 

/* 
* This class defines Eloquent methods 
*/ 

abstract class EloquentRepositoryAbstract extends Ardent implements RepositoryInterface { 

    protected $guarded = []; 

    public $timestamps = false; 
    public $autoHydrateEntityFromInput = true; // Ardent hydrates on new entries' validation 
    public $forceEntityHydrationFromInput = true; // Ardent hydrates whenever validation is called 
… 

我控制器带来:

<?php 

use \Acme\Repositories\Organizations\OrganizationRepositoryInterface; 
use \Acme\Validators\Organizations\Validator; 
use \Acme\Validators\Users\EditValidator as UsersValidator; 

class OrganizationsController extends BaseController { 

    /** 
    * Organization Repository 
    * 
    * @var repository 
    * @var validator 
    * @var usersValidator 
    */ 
    protected $repository; 
    protected $validator; 
    protected $usersValidator; 

    public function __construct(OrganizationRepositoryInterface $repository, 
           Validator $validator, 
           UsersValidator $usersValidator) 
    { 
     $this->repository = $repository; 
     $this->validator = $validator; 
     $this->users_validator = $usersValidator; 
    } 

[...]香草雄辩的风格店铺方式:

public function store() 
    { 
     $input = Input::all(); 
     $opasses = $this->validator->passes(); 
     $ppasses = $this->repository->validateProcedure($input); 
     $upasses = $this->users_validator->passes(); 
     if($opasses && $ppasses['status'] && $upasses) 
     { 
      $new_organization = $this->repository->create(['organization_name' => $input['organization_name']]); 
      if($input['logo_url']) 
      { 
       $new_organization->processImage($input, Request::root());    
      } 
      if($new_organization->saveRelated($input, 'create')) 
      { 
       return Redirect::route('home') 
        ->with('message', 'Organization Created.');    
      } 
      else 
      { 
       return Redirect::route('organizations.create') 
        ->withInput() 
        ->with('message', 'There were errors in the creation of this Organization');    
      } 
     } 
     return Redirect::route('organizations.create') 
      ->withInput() 
      ->withErrors(array_merge($this->validator->getErrors()->toArray(), $this->users_validator->getErrors()->toArray(), $ppasses)) 
      ->with('message', 'There were validation errors.'); 

    } 

这一切似乎过于复杂和脆弱。

回答

0

我认为最棘手的决定是在调用验证方法之前放置验证规则的地方。这里有一个链接,将有助于:http://laravel.com/docs/validation#available-validation-rules

下面是我做了我的,但我仍然决定在哪里放置验证规则/错误消息。

Laravel 4内置的验证类有一个方法,使得验证真正的轻松:

Validator::make($input, $rules=array(), $errors=array()) 

(第三个参数是可选的)

在你的情况,你打电话给你创建的方法(假设这是前用来保存元素),你可以这样做:

$rules = array(
    "first_name" : "required", 
    "second_name" : "required", 
    and so on... 
); 

我给的链接,你会给你的可能的规则。最后,您还可以创建自己的自定义错误消息,如下所示:

$messages = array(
    "required" => "This :attribute field is required" 
    and so on... 
); 

::属性将填写自动验证失败的字段。

然后,验证您刚刚通过这些阵列和调用通行证()方法或失败()方法:

Validator::make($input, $rules, $messages)->passes() (如果通过返回true,false,如果失败) 它看起来更干净我认为做这样:

$validation = Validator::make($input, $rules, $messages); 
if ($validation->passes()) { 
     // save data 
} 

作为奖励,你可以从这个数组访问错误信息:

$errors = $validation->messages(); 

所以,我希望能回答你的问题。我认为最大的问题是在哪里放置这些验证规则,因为我认为还没有一个约定。如果您有任何问题,请分享您的见解和意见。谢谢。

+0

那么我现在有我认为一个不好的解决方案。我正在使用存储库模式和验证作为服务。 – user3061986

+0

是的,我认为这与你原来的帖子完全不同,因为你现在问的是应用程序架构而不是如何验证输入。 –