There are three things to note with respect to this question:
- Validity
- Licitness
- Hindrances
With regards to validity, a Sacrament is valid or not depending on the intention and form of the minister confecting the Sacrament. If the minister intends by his actions to do what the Church intends by that action, and if he adheres to the correct form of the Sacrament, as defined by the Church, then the Sacrament is valid, regardless of anything else.
With regards to licitness, a Sacrament may only be performed licitly as long as the minister of the Sacrament has be so authorized by the Church to do so, and within the strictures and guidelines that the Church has established for each Sacrament. A Sacrament may be performed licitly, but not validly, and a Sacrament may be performed validly, but not licitly. For example, a Black Priest (a validly ordained priest who has become Satanic) may validly consecrate the Eucharist, but does so illicitly. Likewise, a priest in good standing with the Church may perform the Eucharistic Sacrament licitly, but if he does not believe in the real presence (for example), then the Sacrament is not completed validly.
The third thing to consider are hindrances to the Sacramental Graces. A Sacrament may be completed validly and licitly, and still not convey the graces that the Sacrament intends. Hindrances to these graces lie with the person who is receiving the Sacrament, and may consist in deficient knowledge or understanding of what the Sacrament is and does, or deficient intention in the reception of the Sacrament.
So, a person may be Confirmed both licitly and validly, but of the confirmand either does not understand the Sacrament, or does not want to receive it (for example), but is being pressured to do so, then the graces that the Sacrament intends to convey are hindered.
Likewise, for example, with Penance, if the one confessing is not properly disposed (i.e., not really sorry). The Sacrament, ministered by the Priest, may be completed licitly and validly, but the graces of the Sacrament can be hindered by the one receiving it.
Baptism is ministered by any validly baptized person, cleric or lay, and the same rules apply.
Marriage is ministered by the couple, not the priest, which is why the intention of either the Bride or Groom may render the Sacrament invalid, because they may not be intending to accomplish by the marriage what the Church intends by it.
I hope this helps.