One of the conditions of prayer being valid is avoidance of najaasah on one’s body and garment and in the place where one is praying. If a person prays when there is some najaasah on his clothes or body, or carries a child who has some najaasah on him, or carries a bottle in which there is some najaasah, his prayer is rendered invalid according to the majority of scholars, but his wudoo’ is not rendered invalid.
Ibn Qudaamah (may Allah have mercy on him) said in al-Mughni (1/403): If the worshipper carries a sealed bottle in which there is some najaasah, his prayer is not valid, because he is carrying an impurity that could not be overlooked, so it is similar to the case if the najaasah was on his body or clothes.
See: al-Mawsoo‘ah al-Fiqhiyyah, 40/99; al-Majmoo‘, 3/157; Kashshaaf al-Qinaa‘, 1/289
Nullification of the prayer only occurs if the worshipper carries the child knowing that there is some najaasah on him. If he did not know, or he knew that there was some najaasah on him, but when he picked him up he had forgotten about it, then his prayer is still valid.
An-Nawawi (may Allah have mercy on him) said in al-Majmoo‘, 3/163:
With regard to scholarly opinions on one who prays with some najaasah on him that he forgot about or was unaware of, the more correct view according to our madhhab is that he has to repeat the prayer. This is the view of Abu Qilaabah and Ahmad. However the majority of scholars say that he does not have to repeat it. Ibn al-Mundhir narrated that from Ibn ‘Umar, Ibn al-Musayyab, Tawoos, ‘Ata’, Saalim ibn ‘Abdullah, Mujaahid, ash-Sha‘bi, an-Nakha‘i, az-Zuhri, Yahya al-Ansaari, al-Awzaa‘i, Ishaaq and Abu Thawr. Ibn al-Mundhir said: This is also my opinion. It is also the view of Rabee‘ah and Maalik. The evidence for it is strong and it is the favoured view. End quote.
Al-Mirdaawi said in al-Insaaf, 1/486
The words “If he realised (after finishing the prayer) that it was present whilst he was praying, but he was unaware of it or forgot it, then there are two opinions.
One opinion says that his prayer is valid. This is the correct view according to the majority of later scholars and was the view favoured by the author [i.e., Ibn Qudaamah]… and Shaykh Taqiy ad-Deen [i.e., Ibn Taymiyah]
The second opinion says that his prayer is not valid and he has to repeat it. And this is what we think is the correct view. End quote.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
If (a person) prays with some impurity on his body, i.e., some najaasah got on him and he did not wash it off, or there is some impurity on his garment or in the place where he is praying, then his prayer is not valid according to the majority of scholars. But if he was not aware of this najaasah, or he was aware of it but then he forgot to wash it off until he had completed his prayer, then his prayer is valid and he does not have to repeat it. The evidence for that is that the Prophet (blessings and peace of Allah be upon him) led his Companions in prayer one day and took his shoes off (whilst praying), and the people took their shoes off. When the Prophet (blessings and peace of Allah be upon him) had finished the prayer, he asked them why they had taken their shoes off and they said: We saw you take off your shoes so we took off our shoes. He said: “Jibreel came to me and told me that there was some impurity on them.” If the prayer was to be deemed invalid because of the najaasah when he was unaware of it, then the Prophet (blessings and peace of Allah be upon him) would have started the prayer all over again.
So avoiding impurities on the body and clothes and in the place where one prays is one of the conditions of prayer being valid. But if a person did not avoid impurity because he was unaware of it or because he forgot, then his prayer is still valid whether he knew about it before he prayed then forgot to wash it off, or he did not find out about it until after he had prayed.
If you were to say: What is the difference between this and one who prays without wudoo’ because he forgot or was unaware (of the ruling), as the one who prays without wudoo’ because he forgot or was unaware (of the ruling) was instructed to repeat it, but the one who prays with some impurity because he forgot or was unaware (of it) is not instructed to repeat it?
Our answer is: The difference between them is that wudoo’ or ghusl come under the heading of doing something that is enjoined, whereas avoiding impurity (najaasah) comes under the heading of refraining from something that is prohibited. Failing to do something that is enjoined cannot be excused on the grounds of ignorance or forgetting, unlike doing something that is prohibited.
End quote from Majmoo‘ al-Fataawa, 12/390
And Allah knows best.