Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] New Validation Rules: even and odd #55284

Closed

Conversation

devajmeireles
Copy link
Contributor

Context

In terms of front-end, we have several different ways to print odd/even numbers, a very simple one would be this:

<select name="number_1" id="number_1">
    @for ($i = 0; $i <= 20; $i++)
        @if ($i % 2 === 0)
            <option value="{{ $i }}">{{ $i }}</option>
        @endif
    @endfor
</select>

However, there is no default validation rule to check whether the number sent in the request is odd or even — allowing, in a somewhat unusual example, the HTML to be edited in the browser to change the values of a select element in a form.

Solution

With this PR, we added the even and odd rules to Laravel:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function update(Request $request)
    {
        $request->validate([
            'number_1' => ['even'],
            'number_2' => ['odd'],
        ]);

        return redirect()->back()->withInput();
    }
}

Plus

In addition, we extended these new rules to the Numeric class, added in #54425:

use Illuminate\Validation\Rule;

$request->validate([
    'number_1' => Rule::numeric()->even(),
    'number_2' => Rule::numeric()->odd(),
]);

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants