Custom validation fail messages


#1

Is it possible to have custom made error messages for validation fails?


#2

Hey !

Yup, it’s even in the documentation :smiley:

http://indicative.adonisjs.com/#indicative-basics-custom-messages


#3

well, i must be stupid cause this still gives me the standard messages:

        const data = request.only(['username', 'email', 'password', 'password_confirmation'])

        const rules = {
            username: 'required',
            email: 'required|email|unique:users',
            password: 'required|confirmed'
        }

        const messages = {
            'username.required' : 'Username is required to continue',
            'email.required'    : 'Email is required for further communication'
        }

        const validation = await validateAll(data, rules, messages)

        if (validation.fails()) {
            session
                .withErrors(validation.messages())
                .flashExcept(['password'])

            return response.redirect('back')
        }

#4

i see the new messages in debug but i guess this wont show them:
{{ elIf(’$self’, getErrorFor(‘username’), hasErrorFor(‘username’)) }}


#5

Which version of validator are u using?


#6

4.0 and i think im using two different validations without knowing how and why :slight_smile:


#7

i get confused when i read docs on: http://indicative.adonisjs.com/ and on: http://dev.adonisjs.com/docs/4.0/validator


#8

Can u share the version of @adonisjs/validator from the package.json file?


#9
"@adonisjs/validator": "^4.0.2",

#10

Just update it to the latest version and it will work fine


#11

so if i have this in my controller and i have updated to 4.0.3

   const data = request.only(['username', 'email', 'password', 'password_confirmation'])

        const rules = {
            username: 'required',
            email: 'required|email|unique:users',
            password: 'required|confirmed'
        }

        const messages = {
            'username.required' : 'Username is required to continue',
            'email.required'    : 'Email is required for further communication'
        }

        const validation = await validateAll(data, rules, messages)

        if (validation.fails()) {
            session
                .withErrors(validation.messages())
                .flashExcept(['password'])

            return response.redirect('back')
        }

and this in my register.edge:

  <input type="text" name="username" class="form-control" id="username" value="{{ old('username', '') }}" />
                  {{ elIf('<span>$self</span>', getErrorFor('username'), hasErrorFor('username')) }}

why do i still get: required validation failed on username in stead of Username is required to continue ?


#12

Really strange, but now it is working.

Guess the update to 4.0.3 did the trick even though the browser didn’t take it at first :slight_smile:

Thank you.


#13

I have also the same problem in my blog Holi Wishes bur the help of google now I am able to know the all.