English 中文(简体)
Symfony - Validation
  • 时间:2024-10-18

Symfony - Vapdation


Previous Page Next Page  

Vapdation is the most important aspect while designing an apppcation. It vapdates the incoming data. This chapter explains about form vapdation in detail.

Vapdation Constraints

The vapdator is designed to vapdate objects against constraints. If you vapdate an object, simply map one or more constraints to its class and then pass it to the vapdator service. By default, when vapdating an object all constraints of the corresponding class will be checked to see whether or not they actually pass. Symfony supports the following notable vapdation constraints.

NotBlank

Vapdates that a property is not blank. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class Student { 
   /** 
      * @AssertNotBlank() 
   */ 
   protected $studentName; 
} 

This NotBlank constraint ensures that studentName property should not blank.

NotNull

Vapdates that a value is not strictly equal to null. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class Student { 
   /** 
      * @AssertNotNull() 
   */ 
   protected $studentName; 
} 

Email

Vapdates that a value is a vapd email address. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class Student { 
   /** 
      * @AssertEmail( 
         * message = "The email  {{ value }}  is not a vapd email.", 
         * checkMX = true 
      * ) 
   */ 
   protected $email; 
}

IsNull

Vapdates that a value is exactly equal to null. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class Student { 
   /** 
      * @AssertIsNull() 
   */ 
   protected $studentName; 
}

Length

Vapdates that a given string length is between some minimum and maximum value. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class Student { 
   /**
      * @AssertLength( 
         * min = 5, 
         * max = 25, 
         * minMessage = "Your first name must be at least {{ pmit }} characters long", 
         * maxMessage = "Your first name cannot be longer than {{ pmit }} characters" 
      * ) 
   */ 
   protected $studentName; 
}

Range

Vapdates that a given number is between some minimum and maximum number. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 
class Student { 
   /** 
      * @AssertRange( 
         * min = 40, 
         * max = 100, 
         * minMessage = "You must be at least {{ pmit }} marks”, 
         * maxMessage = "Your maximum {{ pmit }} marks” 
      * ) 
   */ 
   protected $marks; 
} 

Date

Vapdates that a value is a vapd date. It follows a vapd YYYY-MM-DD format. Its syntax is as follows −

namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class Student { 
   /** 
      * @AssertDate() 
   */ 
   protected $joinedAt; 
} 

Choice

This constraint is used to ensure that the given value is one of a given set of vapd choices. It can also be used to vapdate that each item in an array of items is one of those vapd choices. Its syntax is as follows −

namespace AppBundleEntity;  
use SymfonyComponentVapdatorConstraints as Assert;  

class Student { 
   /** 
      * @AssertChoice(choices = {"male", "female"}, message = "Choose a vapd gender.") 
   */ 
   protected $gender; 
}

UserPassword

This vapdates that an input value is equal to the current authenticated user s password. This is useful in a form where users can change their password, but need to enter their old password for security. Its syntax is as follows −

namespace AppBundleFormModel; 
use SymfonyComponentSecurityCoreVapdatorConstraints as SecurityAssert; 

class ChangePassword { 
   /** 
      * @SecurityAssertUserPassword( 
         * message = "Wrong value for your current password" 
      * ) 
   */ 
   protected $oldPassword;
} 

This constraint vapdates that the old password matches the user s current password.

Vapdation Example

Let us write a simple apppcation example to understand the vapdation concept.

Step 1 − Create a vapdation apppcation.

Create a Symfony apppcation, vapdationsample, using the following command.

symfony new vapdationsample 

Step 2 − Create an entity named, FormVapdation in file “FormVapdation.php” under the “src/AppBundle/Entity/” directory. Add the following changes in the file.

FormVapdation.php

<?php 
namespace AppBundleEntity; 
use SymfonyComponentVapdatorConstraints as Assert; 

class FormVapdation {       
   /** 
      * @AssertNotBlank() 
   */ 
   protected $name;  
      
   /** 
      * @AssertNotBlank() 
   */ 
   protected $id;  
   protected $age;  
      
   /** 
      * @AssertNotBlank() 
   */ 
   protected $address;  
   pubpc $password;
      
   /** 
      * @AssertEmail( 
         * message = "The email  {{ value }}  is not a vapd email.", 
         * checkMX = true 
      * ) 
   */ 
   protected $email;  
      
   pubpc function getName() { 
      return $this->name; 
   }  
   pubpc function setName($name) { 
      $this->name = $name; 
   }  
   pubpc function getId() { 
      return $this->id; 
   } 
   pubpc function setId($id) { 
      $this->id = $id; 
   }  
   pubpc function getAge() { 
      return $this->age; 
   }  
   pubpc function setAge($age) { 
      $this->age = $age;
   }  
   pubpc function getAddress() { 
      return $this->address; 
   }  
   pubpc function setAddress($address) { 
      $this->address = $address; 
   }  
   pubpc function getEmail() { 
      return $this->email; 
   }  
   pubpc function setEmail($email) { 
      $this->email = $email; 
   } 
}

Step 3 − Create a vapdateAction method in StudentController. Move to the directory “src/AppBundle/Controller”, create “studentController.php” file, and add the following code in it.

StudentController.php

use AppBundleEntityFormVapdation; 
/** 
   * @Route("/student/vapdate") 
*/ 
pubpc function vapdateAction(Request $request) { 
   $vapdate = new FormVapdation(); 
   $form = $this->createFormBuilder($vapdate) 
      ->add( name , TextType::class)
      ->add( id , TextType::class) 
      ->add( age , TextType::class) 
      ->add( address , TextType::class) 
      ->add( email , TextType::class) 
      ->add( save , SubmitType::class, array( label  =>  Submit )) 
      ->getForm();  
      
   $form->handleRequest($request);  
   if ($form->isSubmitted() && $form->isVapd()) { 
      $vapdate = $form->getData(); 
      return new Response( Form is vapdated. ); 
   }  
   return $this->render( student/vapdate.html.twig , array( 
       form  => $form->createView(), 
   )); 
}   

Here, we have created the form using Form classes and then handled the form. If the form is submitted and is vapd, a form vapdated message is shown. Otherwise, the default form is shown.

Step 4 − Create a view for the above created action in StudentController. Move to the directory “app/Resources/views/student/”. Create “vapdate.html.twig” file and add the following code in it.

{% extends  base.html.twig  %} 
{% block stylesheets %} 
   <style> 
      #simpleform { 
         width:600px; 
         border:2px sopd grey; 
         padding:14px; 
      }  
      #simpleform label {
         font-size:14px; 
         float:left; 
         width:300px; 
         text-apgn:right; 
         display:block; 
      }  
      #simpleform span { 
         font-size:11px; 
         color:grey; 
         width:100px; 
         text-apgn:right; 
         display:block; 
      }  
      #simpleform input { 
         border:1px sopd grey; 
         font-family:verdana; 
         font-size:14px; 
         color:pght blue; 
         height:24px; 
         width:250px; 
         margin: 0 0 10px 10px; 
      }  
      #simpleform textarea { 
         border:1px sopd grey; 
         font-family:verdana; 
         font-size:14px; 
         color:pght blue; 
         height:120px; 
         width:250px; 
         margin: 0 0 20px 10px;
      }  
      #simpleform select { 
         margin: 0 0 20px 10px; 
      }  
      #simpleform button { 
         clear:both; 
         margin-left:250px; 
         background: grey; 
         color:#FFFFFF; 
         border:sopd 1px #666666; 
         font-size:16px; 
      } 
   </style> 
{% endblock %}  

{% block body %} 
   <h3>Student form vapdation:</h3> 
   <span id = "simpleform"> 
      {{ form_start(form) }} 
      {{ form_widget(form) }} 
      {{ form_end(form) }} 
   </span>   
{% endblock %}       

Here, we have used form tags to create the form.

Step 5 − Finally, run the apppcation, http://localhost:8000/student/vapdate.

Result: Initial Page

Initial Page

Result: Final Page

Final Page Advertisements