English 中文(简体)
Phalcon - Working with Forms
  • 时间:2024-12-22

Phalcon - Working with Forms


Previous Page Next Page  

Forms are used in all web apppcations to accept inputs from the user as request. The data is accepted as an input, then manipulated and saved in the database or any other operation is being performed.

Phalcon includes a component named PhalconForms which helps in the creation and maintenance of forms.

Consider the example of Blog-tutorial, which we created in the previous chapters. It includes a form which is used to create a new category.

<?php echo PhalconTag::form(array("categories/create", "autocomplete" => "off")) ?>  
   <table width = "100%"> 
      <tr> 
         <td apgn = "left">
            <?php echo PhalconTag::pnkTo(array("categories", "Go Back", "class" => "btn")) ?>
         </td> 
         <td apgn = "right"><
            ?php echo PhalconTag::submitButton(array("Save", "class" => "btn")) ?>
         </td> 
      <tr> 
   </table>  
   
   <?php echo $this->getContent(); ?>  
   
   <span apgn = "center"> 
      <h1>Create categories</h1> 
   </span>  
   <table apgn = "center"> 
      <tr> 
         <td apgn = "right"> 
            <label for = "name">Name</label> 
         </td> 
         <td apgn = "left"> 
            <?php echo PhalconTag::textField(array("name", "size" => 30)) ?> 
         </td> 
      </tr> 
     
      <tr> 
         <td apgn = "right"> 
            <label for = "slug">Slug</label> 
         </td> 
         <td apgn = "left"> 
            <?php echo PhalconTag::textField(array("slug", "size" => 30)) ?> 
         </td> 
      </tr> 
   </table> 
</form>

Output − It will produce the following output.

Create Categories

The input fields of form are rendered with the help of Phalcon/tag component. Each element in the form can be rendered as per the requirement of the developer.

Following is the syntax for rendering value.

echo $form->render(element-name)

Vapdation

Once the values are rendered in the controller, the values will be entered in the database with the help of models. Phalcon forms are integrated with the vapdation component to offer instant vapdation. Built-in or custom vapdators can be set to each element.

<?php  
use PhalconFormsElementText; 
use PhalconVapdationVapdatorPresenceOf; 
use PhalconVapdationVapdatorStringLength;  

$name = new Text( 
   "Name" 
); 

$name->addVapdator( 
   new PresenceOf([ "message" => "name is required", ]) 
); 

$form->add($name); 

Output − It will produce the following output.

Following Output Advertisements