create-request
生成 Laravel FormRequest 类以进行验证。用于创建或更新数据(例如,“为学生注册添加验证”)。
📖 中文导读:生成 Laravel FormRequest 类以进行验证。用于创建或更新数据(例如,“为学生注册添加验证”)。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。
name: create-request description: Generates a Laravel FormRequest class for validation. Use when creating or updating data (e.g., "Add validation for student registration"). allowed-tools: Write, Read
Create Form Request
This skill generates a FormRequest class, which is the mandatory location for all validation logic in this project.
Instructions
Identify Parameters:
- Module: (e.g.,
Identity,Academic). - Context: What action is being validated? (e.g.,
RegisterStudent,UpdateProfile). - Path:
app/Modules/{Module}/Http/Requests/{Module}/{ClassName}.php.
- Module: (e.g.,
Generate Code:
- Namespace:
App\Modules\{Module}\Http\Requests\{Module} - Method
authorize(): Returntrue(authorization is handled by Policies/Middleware, not here, unless simple ownership check). - Method
rules(): Return array of validation rules. - Strict Typing: Add
string,int,arraytype hints to the returned rules.
- Namespace:
Naming Convention:
- Do not prefix with Actor (e.g.,
StudentLoginRequest❌). - Use Domain Context (e.g.,
LoginRequest✅ insideIdentitymodule).
- Do not prefix with Actor (e.g.,
Template
<?php
namespace App\Modules\Identity\Http\Requests\Identity;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class RegisterStudentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true; // Auth handled by Middleware/Policy
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique('users', 'email'),
],
'campus_id' => ['required', 'integer', 'exists:campuses,id'],
'password' => ['required', 'confirmed', 'min:8'],
];
}
}
Best Practices
- Complex Rules: Use
Rule::builder (e.g.,Rule::unique(),Rule::in()). - Custom Messages: Implement
messages()only if standard messages are insufficient. - Attributes: Implement
attributes()to provide friendly names for fields.