mardi 23 octobre 2018

ember-cp-validations form error message not showing

I am trying to validate input in a form using ember-cp-validations. The actual validation itself seems to be working, however I cannot get the error message to show on the template. I am fairly new to ember in general, and I am assuming that I am not correctly using the model in the v-get helper.

Here is my model:

const Validations = buildValidations({
    name: validator('presence', true),
})

export default DS.Model.extend(Validations, {
    name: DS.attr('string'),
});

Here is the action I am running in my controller when the form is submitted:

//tried to get character model here to make validation work
let character = this.get('model');

//create new character record to store
let newCharacter = this.store.createRecord('character', {
    name: name,
});

//validating here
newCharacter.validate()
    .then( ({validations}) => {
        console.log(validations)
        if(validations.get('isValid')) {
            console.log('form is valid')
            //record will be saved here
        }else{
            console.log('form is invalid')
        }
    })

In this action when the name field is empty it does say that the form in invalid as expected.

Lastly, I attempt to show the error here:


  
    <div class="error">
      
    </div>
  

The div is created here when you lose focus on the field, but the message never shows. From what I understand, the arguments for the helper here should be the model, the field you are validating, and then the message that is to be displayed. The fact that it knows the form is invalid, but the message never shows leads me to believe that I am misunderstanding something and not actually accessing the character model correctly in the v-get helper.

After reading around, and watching some tutorials on ember-cp-validations I still seem to be stuck. Any help would be appreciated.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire