robclancy / validation
该软件包最新版本(dev-master)没有可用的许可证信息。
为PHP 5.4构建的验证软件包,用于框架的简单验证。
dev-master
2013-03-29 15:42 UTC
Requires
- php: >=5.3.0
- robclancy/validation-rules: dev-master
This package is auto-updated.
Last update: 2024-09-22 04:17:23 UTC
README
此版本尚未准备好使用。
以下是一个输出,用于解释问题以便反馈。
首先,我们有一个别名,Validatable
将别名 RobClancy\Validation\Illuminate
。
要使用此软件包进行模型验证,您可以这样做...
// Note most of this would be in a base class class Post extends Eloquent { // We import the validator methods use Validatable; // Define the input and rules to validate, basic post validation public function defineInput() { $this->input('user_id')->required()->integer(); $this->input('post')->required(); } // Then we have our save method public function save() { if ( ! $this->validate($this->attributes)) { return false; // User can now call $this->getErrors() for a list of errors } // Doing this means we can push Input::all() into the model and the validator will filter out what we need $this->attributes = $this->getValidatedInput(); return parent::save(); } }
因此,在控制器中,可以这样做...
class PostController extends Controller { ... public function postIndex() { $post = new Post(Input::all()); if ( ! $post->save()) { return Redirect::back()->withErrors($post->getErrors(), $post->getInput()); } return Redirect::to('success/page'); } }``` Then I had a use case for logging in, didn't want to validate input before sending to authentication on a model so can do this instead... ```php class LoginController extends Controller { use Validatable; // getLogin method etc here // Define the input to validate against public function defineInput() { $this->input('email')->required()->email(); $this->input('password')->required(); } // And once again use the new methods here public function postIndex() { if ( ! $this->validate()) { return Redirect::back()->withErrors($this->getErrors()); } // run authentication } }
在控制器示例中,您还可以跳过 defineInput 方法,而是这样做...
class LoginController extends Controller { use Validatable; ... public function postIndex() { $validate = $this->validate(function($add) { $add->input('email')->required()->email(); $add->input('password')->required(); }); if ( ! $validate) { return Redirect::back()->withErrors($this->getErrors()); } // authenticate } }