0Pricing
Flask Academy · درس

التحقق اليدوي من الحقول المطلوبة

ارفض الإدخال الفارغ أو غير السليم مبكرًا.

التحقق اليدوي من الحقول المطلوبة درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Validate at All

Never trust raw input. Validation catches missing or junk data before it reaches your database or breaks your logic. 🛡️

Read the Field First

Pull each value with .get so an absent field gives None instead of crashing the view before you can check it.

email = request.form.get("email")

Check for Presence

A required field must actually be there. Test for None or an empty string before doing anything else with the value.

if not email:
    ...

Strip Whitespace

Users paste spaces. Call .strip so a field of only blanks counts as empty rather than sneaking past your presence check.

email = request.form.get("email", "").strip()

Collect Errors in a List

Gather every problem into an errors list as you go. That lets you report all issues at once instead of one at a time.

errors = []
if not email:
    errors.append("Email is required")

Validate a Length

Rules go beyond presence. Reject a password that is too short by checking its length and adding a clear message.

if len(password) < 8:
    errors.append("Too short")

A Rough Format Check

For something like an email, a simple substring test catches obvious mistakes. Keep it basic here and lean on libraries later.

if "@" not in email:
    errors.append("Invalid email")

Decide on the Result

After every rule runs, an empty errors list means the input passed. Otherwise you have a tidy bundle of what went wrong.

if errors:
    return render_template("form.html", errors=errors)

Return a 400 for APIs

When the client is an API, send the errors as JSON with a 400 status so the caller knows the request was bad.

return jsonify(errors=errors), 400

Proceed When Clean

Only past the checks should you save anything. A guard clause keeps the happy path below it clean and easy to read.

if not errors:
    save_user(email, password)

The Full Pattern

Read, strip, run rules, collect errors, then branch. This guard pattern scales to any number of fields without nesting chaos.

errors = []
if not email:
    errors.append("Email required")
if errors:
    return jsonify(errors=errors), 400

Quick Check

Pick the cleanest way to require a non blank field.

Recap: Manual Validation

You read fields safely, stripped whitespace, collected errors, ran length and format rules, and only saved clean input. 🎉

الأسئلة الشائعة

هل درس «التحقق اليدوي من الحقول المطلوبة» مجاني؟

نعم — نص درس «التحقق اليدوي من الحقول المطلوبة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «التحقق اليدوي من الحقول المطلوبة»؟

ارفض الإدخال الفارغ أو غير السليم مبكرًا. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «التحقق اليدوي من الحقول المطلوبة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. بناء نموذج HTML يرسل POST
  2. قراءة معلمات الاستعلام وتعيين قيمها الافتراضية
  3. التحقق اليدوي من الحقول المطلوبة
  4. إعادة التوجيه بعد POST (نمط PRG)
← العودة إلى Flask Academy