-
Below is the code <?php
namespace App\Data;
use DateTime;
use Spatie\LaravelData\Attributes\Validation\Rule;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
#[TypeScript]
class MemberData extends Data {
public ?int $id;
#[Rule('required|alpha|max:50')]
public string $first_name;
#[Rule('required|alpha|max:50')]
public string $middle_name;
#[Rule('required|alpha|max:50')]
public string $last_name;
#[Rule('required|in:M,F')]
public string $gender;
#[Rule('nullable|date_format:Y-m-d|before:today')]
public ?DateTime $birthdate;
#[Rule('nullable')]
public ?string $address;
#[Rule('nullable|digits:10')]
public ?string $mobile;
/**
* Get the error messages for the defined validation rules.
* @return array
*/
public static function messages(): array
{
return [
'gender' => 'Please select a valid gender.'
];
}
public static function attributes(): array
{
return [
'first_name' => 'First Name',
'gender' => 'Gender',
];
}
} The value for gender should be M or F. If null or any other character then above message should be displayed. But it displays the default error as shown in the image below. I can see the attributes are working perfect. |
Beta Was this translation helpful? Give feedback.
Answered by
aasiph
Jan 1, 2024
Replies: 1 comment
-
Never mind adding dot * worked.. /**
* Get the error messages for the defined validation rules.
* @return array
*/
public static function messages(): array
{
return [
'gender.*' => 'Please select a valid gender.',
'first_name.required' => 'Just for required',
'first_name.*' => 'General error message',
];
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
aasiph
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never mind adding dot * worked..