为什么我的验证规则在Laravel Controller中不起作用? [英] Why is my validation rule not working in my Laravel Controller?

查看:89
本文介绍了为什么我的验证规则在Laravel Controller中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个检查日期格式是否正确的测试.查看文档似乎很简单.

I am trying to write a test that checks that a date is formatted correctly. Looking at the docs it seems pretty straight-forward.

这是我的考验.我正在向控制器提交无效的日期格式:

Here is my test. I am submitting an invalid date format to my controller:

MyTest.php

 /** @test */
public function a_date_must_be_formatted_correctly()
{
    $foo = factory(Foo::class)->raw([
        'date' => date('d/m/Y'),
    ]);

    $response = $this->postJson('api/v1/foo', $foo)
        ->assertStatus(422)
        ->assertJsonValidationErrors('date');
}

这是我的控制器方法:

public function store(Request $request)
{
    $attributes = $request->validate([
        'date' => 'required|date_format:Y-m-d',
    ]);
    ...
}

我每次都通过考试.

我还尝试将格式包装在引号中:'date'=>'required | date_format:"Y-m-d"',

I have also tried wrapping the format in quotes: 'date' => 'required|date_format:"Y-m-d"',

我希望得到一个 422 的回覆,说我的 date 是无效的.我在做什么错了?

I'm expecting to get a 422 back saying my date is invalid. What am I doing wrong?

推荐答案

好吧,我最终编写了一个自定义规则.我认为这不是正确的解决方案,尽管考虑到Laravel具有方便的内置功能.我觉得我应该利用它.就我而言,我正在处理停电日期.

Well, I ended up writing a custom rule. I don't think this is the correct solution though given that Laravel has the handy built-in stuff. I feel like I should be leveraging that instead. In my case I am working with Blackout date(s).

她就是我最终的目标.如果有办法使内置方法起作用,请告诉我,以便我更新答案.

Hers is what I ended up with. If there is a way to make the built-in methods work, please let me know so I can update the answer.

MyTest.php

/** @test */
public function a_blackout_date_must_be_formatted_correctly()
{
    $blackout = factory(Blackout::class)->raw([
        'date' => date('d/m/Y'),
    ]);

    $response = $this->postJson('api/v1/blackouts', $blackout)
        ->assertStatus(422)
        ->assertJsonValidationErrors('date');
}

MyController.php

public function store(Request $request)
{
    $attributes = $request->validate([
        'date' => ['required', new DateFormatRule],
        ...
    ]);

    $blackout = new Blackout($attributes);

    ...

}

DateFormatRule.php

public function passes($attribute, $value)
{
    // The format we're looking for.
    $format = 'Y-m-d';

    // Create a new DateTime object.
    $date = DateTime::createFromFormat($format, $value);

    // Verify that the date provided is formatted correctly.
    return $date && $date->format($format) === $value;
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return 'Invalid date format.';
}

在我的测试中,我得到了预期的 422 响应.我可以通过将预期错误的名称从 date 更改为 date123 来验证一切是否正常,然后我会得到一个错误-说它正在期待 date 不是 date123 .

In my test I am getting a 422 response as expected. I can verify things are working by changing the name of the expected error from date to date123 and I'll get an error -- saying It was expecting date not date123.

希望这对某人有帮助!

这篇关于为什么我的验证规则在Laravel Controller中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆