I tried extending the Laravel 4.2 validation to check if all form data is valid (not just some fields) according to some business rules.
我尝试扩展Laravel 4.2验证,以根据一些业务规则检查所有表单数据是否有效(不仅仅是某些字段)。
I could not extend the custom validator in any normal way (either too hacky or not DRY enough) so I decided to add the business rules check after validator succeeds, and then add my own error messages and redirect.
我无法以任何正常的方式扩展自定义验证器(太hacky或不够DRY)所以我决定在验证器成功后添加业务规则检查,然后添加我自己的错误消息并重定向。
It is still ugly and it does not work but for now I would be happy if someone tells me how to add my message to the session. If you feel like telling me how I should use Laravel's custom validators I've included some more code so you can be more specific..:
它仍然是丑陋的,它不起作用但是现在如果有人告诉我如何将我的消息添加到会话中,我会很高兴。如果您想告诉我如何使用Laravel的自定义验证器,我已经包含了一些代码,以便您可以更具体......:
$validator = Validator::make(Input::all(), $this->validationRules, $this->validationMessages);
if ($validator->fails()) {
if (0 == $id) {
return Redirect::route('x.create')->withErrors($validator)->withInput();
} else {
return Redirect::route('x.edit', $id)->withErrors($validator)->withInput();
}
} else {
//TESTs
if ($this->AlreadyExists($id)) {
$messages = new MessageBag();
if ($request->getSession()->get('errors')){
$messages = $request->getSession()->get('errors')->getBag('default');
}
$messages->add('form', 'same data already exists');
if (0 == $id) {
return Redirect::route('x.create')->withErrors($messages)->withInput();
} else {
return Redirect::route('x.edit', $id)->withErrors($messages)->withInput();
}
}
}
//all is ok. Save/update entity
...
The code for setting the session is first 5 lines after check for AlreadyExists. I got it from some forum but it doesn't seem to work ok (I get some "non-object" exceptions if I include the code so it seems it corrupts the session object)
检查AlreadyExists后,设置会话的代码是前5行。我从一些论坛得到它但它似乎没有工作正常(如果我包含代码,我会得到一些“非对象”异常,因此它似乎破坏了会话对象)
I don't think I have time to upgrade to L5.
我认为没有时间升级到L5。
2 个解决方案
#1
I solved this by creating a new error message bag and adding it to redirect:
我通过创建一个新的错误消息包并将其添加到重定向来解决这个问题:
$mb = new Illuminate\Support\MessageBag();
$mb->add("form", "same data already exists");
...
return Redirect::route('x.create')->withErrors($mb)->withInput();
Seems I needed to add full namespace for bag, but most of all I needed rest :)
似乎我需要为包添加完整的命名空间,但最重要的是我需要休息:)
The validation logic is still bad but no time to tinker with that now.
验证逻辑仍然很糟糕,但现在没有时间修补它。
#2
for anyone who uses form request I suggest to take a look at this implementations. formatErrors in laravel 5.1 documentations mentioned for a way to manipulate the way errors display. I just simply jump in it and added some codes to extend errors.
对于使用表单请求的任何人,我建议看看这个实现。 laravel 5.1文档中的formatErrors提到了一种操纵错误显示方式的方法。我只是简单地跳进去并添加了一些代码来扩展错误。
use Illuminate\Contracts\Validation\Validator;
class ProfileChangePasswordRequest extends Request
{
public function authorize()
{
if (auth()->check())
return true;
return false;
}
public function rules()
{
return [
'password' => "required|confirmed|min:6"
];
}
protected function formatErrors(Validator $validator)
{
if ($this->isCurrentPasswordValidate()) {
return $validator->errors()->all();
} else {
$validator->errors()->add('current_password', 'current password is wrong');
return parent::formatErrors($validator);
}
}
public function isCurrentPasswordValidate()
{
return auth()->validate(['email' => auth()->user()->email,
'password' => $this->current_password]);
}
}
#1
I solved this by creating a new error message bag and adding it to redirect:
我通过创建一个新的错误消息包并将其添加到重定向来解决这个问题:
$mb = new Illuminate\Support\MessageBag();
$mb->add("form", "same data already exists");
...
return Redirect::route('x.create')->withErrors($mb)->withInput();
Seems I needed to add full namespace for bag, but most of all I needed rest :)
似乎我需要为包添加完整的命名空间,但最重要的是我需要休息:)
The validation logic is still bad but no time to tinker with that now.
验证逻辑仍然很糟糕,但现在没有时间修补它。
#2
for anyone who uses form request I suggest to take a look at this implementations. formatErrors in laravel 5.1 documentations mentioned for a way to manipulate the way errors display. I just simply jump in it and added some codes to extend errors.
对于使用表单请求的任何人,我建议看看这个实现。 laravel 5.1文档中的formatErrors提到了一种操纵错误显示方式的方法。我只是简单地跳进去并添加了一些代码来扩展错误。
use Illuminate\Contracts\Validation\Validator;
class ProfileChangePasswordRequest extends Request
{
public function authorize()
{
if (auth()->check())
return true;
return false;
}
public function rules()
{
return [
'password' => "required|confirmed|min:6"
];
}
protected function formatErrors(Validator $validator)
{
if ($this->isCurrentPasswordValidate()) {
return $validator->errors()->all();
} else {
$validator->errors()->add('current_password', 'current password is wrong');
return parent::formatErrors($validator);
}
}
public function isCurrentPasswordValidate()
{
return auth()->validate(['email' => auth()->user()->email,
'password' => $this->current_password]);
}
}