I didn’t find the answer in the examples provided in ember-cp-validation dummy app as well in their presentation.
I have to validate a language in the following model:
# models/shop-language.js
import DS from 'ember-data';
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
language: {
description: 'Language',
validators: [validator('ds-error'), validator('presence', true)]
},
shop: {
description: 'Shop',
validators: [validator('ds-error'), validator('presence', true)]
},
});
export default DS.Model.extend(Validations, {
shop: DS.belongsTo('shop', { async: true }),
language: DS.belongsTo('language', { async: true }),
modifiedBy: DS.attr('string')
});
On which property should I use v-get
in the template if I load 2 models from my route:
#routes/shop-languages.js
model() {
return RSVP.hash({
shopLanguages: this.store.query('shop-language', { shop_id: this.get('currentShop.shop').get('id')}),
languages: this.store.findAll('language')
});
},
Here is the template:
#templates/shop-languages.hbs
...
{{#each model.shopLanguages as |shopLang|}}
<li class="list-group-item">
{{shopLang.language.tag}}
<button type="button" class="btn btn-danger btn-sm" disabled={{isDeleteButtonDisabled}} {{action "deleteLanguage" shopLang}}>
<span class="oi oi-trash"></span>
</button>
</li>
{{/each}}
<form {{action "saveLanguage" aLanguage on="submit"}} class="form-inline">
{{#if showLanguageError}}
<div class="alert alert-danger">
{{v-get aLanguage 'id' 'message'}}
</div>
{{/if}}
<div class="col-sm-2">
{{#power-select
class="form-control"
placeholder=placeholderText
selected=aLanguage
options=model.languages
searchField="tag"
noMatchesMessage=(t 'components.language.labels.not.found')
onchange=(action (mut aLanguage))
focus-out=(action (mut showLanguageError) true)
as |language|
}}
{{language.tag}}
{{/power-select}}
</div>
<div class="form-check sm-2">
<button type="button" type="submit" class="btn btn-success btn-sm">
<span class="oi oi-plus"></span>
{{t 'buttons.add'}}
</button
...
Here is how I tried to save a newly added language in the route:
# routes/shop-languages.js
actions: {
...
async saveLanguage(aLanguage) {
...
try {
await shopLanguage.save();
controller.set('aLanguage', '');
route.get('flashMessages').success(route.get('i18n').t('flash.language.added'));
route.refresh();
} catch(response) {
let error = response.errors[0];
console.log('+++++ in CATCH block error: ' + error);
if (error.status !== "422") {
throw response;
}
}
It prints the error to the console but displays nothing in the template … The response I get from Rails backend if language is not selected:
{"message":"Couldn't find Language"}
Should parse in some way this message to be able to display it ?
And the error
object is as follows:
{status: "404", title: "The backend responded with an error", detail: "[object Object]"}
Thank you.