Praise be to Allah.
It is not valid to change the intention from naafil to obligatory, because what is required in an obligatory prayer is for the intention to be formed with the opening takbeer, or shortly before it.
Al-Nawawi (may Allaah have mercy on him) said in al-Majmoo’ (4/183-184):
Al-Maawardi said: Changing the intention from one prayer to another falls into different categories:
1 – Changing the intention from one obligatory prayer to another: neither of them counts.
2 – Changing the intention from one regular naafil prayer to another, such as Witr to the Sunnah of Fajr: neither of them counts.
3 – Changing from a naafil prayer to a fard prayer: neither of them counts.
… . etc. end quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked about a man who started to pray, then he remembered that he had not prayed ‘Isha’, so he changed his intention to ‘Isha’. Is that valid?
That is not valid, because the intention for a specific act of worship must be formed from the beginning, before starting it. If he formed the intention partway through, that implies that the part of it that he did before forming the new intention is devoid of the intention to offer the prayer that he changed to, and the Prophet (peace and blessings of Allaah be upon him) said: “Actions are but by intentions, and every man will have but that which he intended.” So the questioner has to repeat the ‘Isha’ prayer. End quote.
Majmoo’ Fataawa Ibn ‘Uthaymeen, 12/443
See also the answer to question no. 39689
With regard to the ruling on the prayer of those who prayed behind him, it is valid in sha Allaah, because although the prayer of the imam was not valid as an obligatory prayer, it was still a naafil prayer, because he changed his intention out of ignorance and thinking that doing so was permissible. So it is like one who starts to offer an obligatory prayer before the time for it has begun, thinking that the time has begun, so it counts as a naafil prayer. In the answer to question no. 21764 we have stated that an obligatory prayer offered behind an imam who is praying a naafil prayer is valid.
Even if we assume that the imam’s prayer was invalid because of that, that does not mean that the prayer of those who prayed behind him is also invalid.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked: Does the prayer of the congregation become invalid if the prayer of the imam becomes invalid?
He replied: The prayer of the congregation does not become invalid if the prayer of the imam becomes invalid, because the prayer of the people praying behind him is valid and the basic principle is that it remains valid and it cannot be regarded as invalid unless there is sound evidence to that effect. The prayer of the imam may become invalid on the grounds of sound evidence, but the person praying is following the command Allaah and his prayer cannot be deemed invalid except with evidence to that effect. The basic principle is: whoever starts to do an act of worship as prescribed, we cannot rule it to be invalid without evidence.
An exception to that is cases where the imam’s position applies to the congregation as well, such as the sutrah (screen). The sutrah of the imam is also a sutrah for those who are behind him. If a woman passes between the imam and his sutrah, then the prayer of the imam and the prayer of those who are praying behind him are both invalidated, because this sutrah is shared by all of them. Hence we do not tell the people praying behind the imam to use a sutrah, rather if they use a sutrah then this is regarded as going to extremes and as an innovation. End quote.
Majmoo’ Fataawa Ibn ‘Uthaymeen, 12/450
And Allaah knows best.