简体   繁体   中英

Laravel only add alpha_num if certain conditoin is meet

How can I only add alpha_num to the validation if $this->language() returns en ?

public function rules()
{
    return [
        'title' => ['required', 'alpha_num'],
    ];
}

public function language()
{
    // For brevity I only return "en", it could be other languages
    return 'en';
}
use Illuminate\Validation\Rule;

public function rules()
{
    return [
        'title' => ['required', Rule::when($this->language() === 'en', ['alpha_num'])],
    ]; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM