Rasa formvalidation

Rasa form" FormValidationAction" is not checking for setting slot value . it is setting simultaneously 2 entities into slot .

code ###################

def validate_material(

        self,

        slot_value: Any,

        dispatcher: CollectingDispatcher,

        tracker: Tracker,

        domain:  Dict[Text, Any],

) -> Dict[Text, Any]:

    """Validate material value."""

    try:

        material_no= re.findall(r'\d+',slot_value,re.IGNORECASE)[0]

        print(material_no)

    except Exception as E:

        print(E)

    if len(material_no) ==13:

        return {"material":material_no}

    else:

        dispatcher.utter_message(response="utter_wrong_material")

        return {"material":None}

def validate_plant(

    self,

    slot_value: Any,

    dispatcher: CollectingDispatcher,

    tracker: Tracker,

    domain: Dict[Text, Any],

) -> Dict[Text, Any]:

    """Validate `plant` value."""

    entity=re.search(r'(\d{4,5})', slot_value)

    if len(slot_value)<7:

        # validation succeeded, set the value of the "erp" slot to value

        return {"plant": entity[0]}

    else:

        print('Uttering wrong plant')

        dispatcher.utter_message(response="utter_wrong_plant")

        # validation failed, set this slot to None,

        # user will be asked for the slot again

        return {"plant": None}

@naveenjr can you share the rasa --version?

Rasa Version : 2.8.1 Minimum Compatible Version: 2.8.0 Rasa SDK Version : 2.8.1 Rasa X Version : 0.39.3 Python Version : 3.8.12 Operating System : Windows-10-10.0.19043-SP0

@naveenjr please visit this repo for your reference: https://github.com/RasaHQ/rasa-2.x-form-examples/tree/main/05-validation I would recommend going through every mentioned forms example, it will help you. I hope this will solve your issue.

thanks for prompt replay. i have find issue

in domin file

########### earlier

data_form:

required_slots:
  material:
  - entity: material
    type: from_entity
  plant:
  - entity: plant
    type: from_entity

########### now data_form:

required_slots:

  material:

  - type: from_text

  plant:

  - type: from_text

#############

which is giving better result .But what is actual difference ? why form validation is falling earlier case ? @nik202

@naveenjr great naveen, please close this thread as a solution. The reason can be anything, I can not comment without seeing the custom action only, that’s why I recommend seeing the repo.

custom action file for your reference

################code

class Resource_Form(FormValidationAction):

def name(self) -> Text:

    return "resource_form"

def run(self, dispatcher: CollectingDispatcher,

    tracker: Tracker,

    domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

    #Calling the DB

    #calling an API

    # do anything

    #all caluculations are done

    camera = tracker.get_slot('material')

    ram = tracker.get_slot('plant')

    battery = tracker.get_slot('work_center')

    dispatcher.utter_message(text='Here are your search results')

    return []

def slot_mappings(self):

    # type: () -> Dict[Text: Union[Dict, List[Dict]]]

    """A dictionary to map required slots to

        - an extracted entity

        - intent: value pairs

        - a whole message

        or a list of them, where a first match will be picked"""

    return {"material": self.from_entity(entity="material",

                                        intent="material_number",not_intent="plant_name"),

            "plant": self.from_entity(entity="plant",

                                        intent="plant_name")}

def validate_material(

    self,

    value: Text,

    dispatcher: CollectingDispatcher,

    tracker: Tracker,

    domain: Dict[Text, Any],

) -> Dict[Text, Any]:

    """Validate num_people value."""

    #4 GB RAM

    # 10 GB RAM --> integers/number from this -- 10

    #

    material_v = re.findall(r'[0-9]+[A-Za-z]',value)[0]

    #Query the DB and check the max value, that way it can be dynamic

    if len(material_v) > 10:

        return {"material":material_v}

    else:

        dispatcher.utter_message(template="utter_wrong_ram")

        return {"material":None}

def validate_plant(

    self,

    slot_value: Text,

    dispatcher: CollectingDispatcher,

    tracker: Tracker,

    domain: Dict[Text, Any],

) -> Dict[Text, Any]:

    """Validate `plant` value."""

    print(f"slot value given = {slot_value}")

    entity=re.findall(r'(\d{4,6})', slot_value)

    print(f"len slot value given = {len(slot_value)}")

    if len(slot_value)<7:

        # validation succeeded, set the value of the "erp" slot to value

        return {"plant": entity[0]}

    else:

        print('Uttering wrong plant')

        dispatcher.utter_message(response="utter_wrong_plant")

        # validation failed, set this slot to None,

        # user will be asked for the slot again

        return {"plant": None}