The SEVP Response Center answer above that F-1 students cannot attend a flight school is incomplete.
Full-time, accredited Professional Pilot Programs at some schools are indeed eligible for F-1 visa issuance.
Some of these programs will accept "transfer credits" or equivalent, which may include previously obtained certificates/ratings (e.g., a PPL, at their discretion). There are probably some accreditation rules which state that a minimum of x% of the required training time must be completed as part of the program.
So is it legal?
Depends. If you were accepted & enrolled into one of the approved, full-time Professional Pilot programs mentioned above, then it's perfectly legal. You may be issued an F-1 visa and may be eligible for the F-1 CPT and OPT work permits.
If you intend to just take a some CFI courses at a flight school without pursuing an approved Professional Pilot course, then it's not legal. You must attend under an M-1 visa, and may only be eligible for limited OPT.
My 2 cents.