From ec557205f4a01f3e26ddc4f3be65f070902a9296 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Tue, 16 Feb 2021 15:15:37 +0530 Subject: [PATCH] fix: Add validations dynamically for pre-chat form (#1779) --- .../api/v1/widget/conversations_controller.rb | 2 +- .../widget/components/PreChat/Form.vue | 36 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/controllers/api/v1/widget/conversations_controller.rb b/app/controllers/api/v1/widget/conversations_controller.rb index 2c8cede47..cc68cb888 100644 --- a/app/controllers/api/v1/widget/conversations_controller.rb +++ b/app/controllers/api/v1/widget/conversations_controller.rb @@ -7,7 +7,7 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController def create ActiveRecord::Base.transaction do - update_contact(contact_email) if @contact.email.blank? + update_contact(contact_email) if @contact.email.blank? && contact_email.present? @conversation = create_conversation conversation.messages.create(message_params) end diff --git a/app/javascript/widget/components/PreChat/Form.vue b/app/javascript/widget/components/PreChat/Form.vue index 8b1543958..8871bc7da 100644 --- a/app/javascript/widget/components/PreChat/Form.vue +++ b/app/javascript/widget/components/PreChat/Form.vue @@ -71,18 +71,30 @@ export default { default: () => ({}), }, }, - validations: { - fullName: { - required, - }, - emailAddress: { - required, - email, - }, - message: { - required, - minLength: minLength(10), - }, + validations() { + const identityValidations = { + fullName: { + required, + }, + emailAddress: { + required, + email, + }, + }; + + const messageValidation = { + message: { + required, + minLength: minLength(10), + }, + }; + if (this.options.requireEmail) { + return { + ...identityValidations, + ...messageValidation, + }; + } + return messageValidation; }, data() { return {