validationMessages within createOptionsFrom not working as expected

Hi All,
->createOptionForm([
TextInput::make('name')
->label('Job Title')
->maxLength(255)
->required()
->validationMessages([
'required' => 'A job title is required.',
'max' => 'Job title cannot exceed 255 characters.'
]),
TextInput::make('acronym')
->label('Acronym')
->nullable()
])
->createOptionForm([
TextInput::make('name')
->label('Job Title')
->maxLength(255)
->required()
->validationMessages([
'required' => 'A job title is required.',
'max' => 'Job title cannot exceed 255 characters.'
]),
TextInput::make('acronym')
->label('Acronym')
->nullable()
])
this produces the screenshot with orange ! icon, I'd expect it to show with red text like the other screenshot!? Any ideas? Cheers, Bot
No description
No description
Solution:
@awcodes, We have managed to work this one out: ```php TextInput::make('name') ->label('Job Title')...
Jump to solution
6 Replies
awcodes
awcodes6d ago
This is the default browser validation. If you do not want to use it then drop required() and maxLength() and apply them with rules()
TheRealTeeHill
@awcodes I'm chiming in as Bot's colleague... can you point to an example/resource of this for required so we can get an idea of implementation please?
Solution
bottbott
bottbott6d ago
@awcodes, We have managed to work this one out:
TextInput::make('name')
->label('Job Title')
->rules(Rule::requiredIf(true)),
TextInput::make('name')
->label('Job Title')
->rules(Rule::requiredIf(true)),
awcodes
awcodes6d ago
when you use ->required() it also adds the 'required' attribute to the input, which will trigger browser validation. It's useful to prevent unnecesary requests to the server. ->rules() allows you to pass in a laravel validation rule set that will only get run when a request is made.
bottbott
bottbottOP6d ago
appreciate it, understood!
bottbott
bottbottOP6d ago
you can also do this like this: ->rules(['required', 'max:255'])
->createOptionForm([
TextInput::make('name')
->label('Employment Status')
->rules(['required', 'max:5'])
->validationMessages([
'required' => 'Please select an employment status.',
'max' => 'This cannot exceed.'
]),
])
->createOptionForm([
TextInput::make('name')
->label('Employment Status')
->rules(['required', 'max:5'])
->validationMessages([
'required' => 'Please select an employment status.',
'max' => 'This cannot exceed.'
]),
])

Did you find this page helpful?