Validation required when in Adonisjs doesn't work? - javascript

I'm trying to create a rule for this type of validator.
However, it doesn't work.
Am I doing it wrong or did I not understand how it works.
Post
{
batidaUti: "1",
dataEntrada: "2022-01-23 15:42:33",
lote: 10189636,
nome: "NOME",
quantidade: 50,
siaId: "9324",
tipoEstoque: "PA"
}
Validador
nome: schema.string.optional([
rules.requiredWhen('quantidade', '=', 50)
])
No errors are shown and the form is saved in the database without any validation.

Related

sendgrid mail sending error upon placing multiple receiver

I'm using const sgMail = require('#sendgrid/mail'); and utilizing sendgrid version 7.6.2.
When I'm adding two email addresses in array and passing that into send() or sendMultiple() it's throwing me error like this.
status: 'failed',
> Error: Error: String expected for `email`
here's the section where I'm putting the multiple emails,
mailsTo = {
email: ["demo#email.com", "demo1#email.com"],
name: "demo",
type: 'to'
}
here if I pass one email as in form of string the mail is getting triggered. Can anyone please suggest what am I doing wrong.
Thanks in advance
According to the documentation the addresses are to be passed as an array of EmailData (string or object with email and name).
Please try the following:
mailsTo = [
{
email: "demo#email.com",
name: "demo"
},
{
email: "demo1#email.com",
name: "demo"
}
]
Assuming mailsTo is being pass as the to parameter for

how to create extra field when capturing checkout in commerce js

i am using commerce js on my website and i want to create an extra field for getting phone number from user but commerce js does'nt have any documentation about extra_field. can any one tell me how to add it
i also created extra field in commerce js dashboard
here is my order data object
const orderData = {
line_items: checkoutToken.line_items,
customer: {
firstname: shippingData.firstName,
lastname: shippingData.lastName,
email: shippingData.email,
},
shipping: {
name: "International",
street: shippingData.address1,
town_city: shippingData.city,
county_state: shippingData.shippingSubdivision,
postal_zip_code: shippingData.zip,
country: shippingData.shippingCountry,
},
fulfillment: { shipping_method: shippingData.shippingOption },
payment: {
gateway: "stripe",
stripe: {
payment_method_id: paymentMethod.id,
},
},
extra_fields: {
contact: shippingData.contact,//its not working
}
};
When capturing the order, provide the extra_fields object with each extra field ID as the key and the value you want to use.
extra_fields: { [extraFieldId]: 'your custom field value' }
from https://commercejs.com/docs/api/?shell#capture-order

Yup how to validate mandatory fields on null object

I have a problem regarding the validation of mandatory fields which are properties of a (possibly) null object.
Here is an example schema :
object().shape({
catalog: {
brand: string().required()
}
})
If I try to validate the following object against this schema, I get the expected error : brand is required. From what I understood, there is a default value created for undefined object which mimics the shape of the object in the schema. This is the behavior I expected and what I want.
{ catalog: undefined }
// "catalog.brand is a required field"
But in my case, I don't receive an object with undefined, but with null. And I can't figure out how to manage the same result with a null value.
{ catalog: null }
// No error on catalog.brand
Manually casting null to undefined is out of the question as there is a good reason why I receive null.
Here is a codesandbox which reproduces my use case :
https://codesandbox.io/s/yup-playground-tbfcj
I would really appreciate a bit of help on this, thanks :)
The solution is to include both nullable() and required() to the shape.
Example
const requiredOption = Yup.object()
.shape({
value: Yup.string(),
label: Yup.string(),
})
.nullable()
.required('This field is required.');
Try adding strict() on your object schema
object().shape({
catalog: {
brand: string().required()
}
}).strict();
Here's a less mindbending way to do it than the other answer. This allows you to keep the required directive on the subfield whether or not the parent schema/field containing it is empty at validation time.
yup.object().shape({
field: yup.object().shape({
subfield: string().required(),
}).default(undefined),
});
// Or...
yup.object().shape({
field: yup.object().shape({
subfield: string().required(),
}).default(null).nullable(),
});
One difference between the two is that when you validate data over each of these schemas and field is missing, if you also pass in stripUnknown, in the first case the field will be totally removed from the validated result, but in the second case the field will still show up but with value null.
Adding the .default(null) method along with .nullable() worked for me.
const parentValidator = Yup.object({
personData: personDataValidator,
data: Yup.object({
workshift: Yup.array()
.of(Yup.string().matches(/x|-/g, "Local de trabalho inválido"))
.required("Turno de trabalho obrigatório")
.typeError("Turno de trabalho inválido"),
workplace: Yup.string()
.required("Local de trabalho obrigatório")
.typeError("Local de trabalho inválido"),
email: Yup.string()
.required("Email obrigatório")
.typeError("Email inválido"),
}),
})
.default(null)
.nullable();

Angular 4 check multiple checkboxes

I have a form with User roles displayed as multiple checkboxes:
<div *ngFor="let role of roles">
<label for="role_{{role.id}}">
<input type="checkbox" ngModel name="roles" id="role_{{role.id}}" value="{{role.id}}"> {{role.name}}
</label>
</div>
the roles object loaded from server looks like this which have all the roles that displayed on the form:
{id: 1, name: "HQ", description: "A Employee User", created_at: "2017-10-07 10:43:17",…}
1
:
{id: 2, name: "admin", description: "A Manager User", created_at: "2017-10-07 10:43:17",…}
2
:
{id: 3, name: "caretaker", description: "", created_at: null, updated_at: null}
now i want to set multiple check boxes using form.setValue, my user object loaded from server looks like this:
"roles" in the user object are the roles that are assigned to the user and needs to be checked on the form
{
"id":13,
"name":"Wasif Khalil",
"email":"wk#wasiff.com",
"created_at":"2017-10-07 10:43:17",
"updated_at":"2017-10-09 07:45:34",
"api_token":"LKVCGPGnXZ3LyiCnyiTAg8XTpck6xWlVkeoMBgtoYZWoAOy4b5epNqMz7KG7",
"roles":[
{"id":2,"name":"admin","description":"A Manager User","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17","pivot":{"user_id":"13","role_id":"2","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17"}
},
{"id":1,"name":"HQ","description":"A Employee User","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17","pivot":{"user_id":"13","role_id":"1","created_at":null,"updated_at":null}
}
]
}
after loading user object form server im setting values like this:
this.form.setValue({
name: user.name,
email: user.email,
password:"",
confirm_password:"",
roles: [1] //here im not sure how to set roles
});
can someone help me check the checkboxes with the loaded user roles object.
Thanks in advance
EDIT:
Sorry for not explaining it well, i have edited my question to explain the question again:
the roles on user object are the roles that are assigned to user
and the roles object is the list of all roles to display in form, look at the image below:
You don't have to use reactive forms to make it done.
HTML
<input ...[checked]="check(user.roles,role.id)" ...>
Typescript:
check(value1, value2){
return (value1.filter(item => item.id == value2)).length
}
DEMO

How to auto bind collection from HTML form using node.js express framework?

I am looking for a way to bind a collection on the server side, for example:
single binding
<input type="text" name="person[name]" />
binds to
person:{
name: 'Name from html form'
}
If I am using express I can have access to this object at:
app.post('/person', function(req, res){
console.log(JSON.stringify(req.body.person, null, 2));
});
collection binding
But now I am looking for a way to have multiple phone numbers, for example, I want this JSON to arrive:
person:{
name: 'Name from html form',
phones: [
{ number: '12345678' },
{ number: '87654321' }
]
}
So what is the syntax for binding an input to a collection field???
I tried <input type="text" name="person[phone][number]" /> with no success, and have no idea how to Google for it (I already tried, without success...). Is this a feature from express/connect? If not, what is the best way to achieve it? I know this feature is present on some Java frameworks, so this might exist here too.
Thanks to #camus I found out the way to do it:
br
input(type="text", name="person[phones][0][type]")
input(type="text", name="person[phones][0][number]")
br
input(type="text", name="person[phones][1][type]")
input(type="text", name="person[phones][1][number]")
Gives the following at server side:
person: {
phones:
[{
type: "1",
number: "23453131"
},
{
type: "2",
number: "51254534"
}]
}

Categories