Phalcon Forms

It handles creation and maintenance of forms in the web application. Now, as we have designed a basic web-app before we would be adding signup form.

Designing Signup form

Change the index.phtml viewfile, add the signup page link as a controller.

app/views/index/index.phtml

<?php  

echo "<h1>Hello!</h1>";  

echo PHP_EOL;  

echo PHP_EOL;  

echo $this->tag->linkTo(  

    "signup",  

    "Sign Up Here!"  

);

    OUTPUT:

    Phalcon Forms 1

    Now, we write the signup controller

    app/controllers/SignupController.php

     <?php  
    
    use Phalcon\Mvc\Controller;  
    
    class SignupController extends Controller  
    
    {  
    
        public function indexAction()   
    
        {  
    
         }  
    
    }  
    
    ?> 

      Initializing Form

      In this form, we provide the form definition i.e. structure of form is provided.

      app/views/signup/index.phtml

       <h2>Sign up using this form</h2>  
      
      <?php echo $this->tag->form("signup/register"); ?>  
      
      <p>  
      
      <label for="name">Name</label>  
      
      <?php echo $this->tag->textField("name"); ?>  
      
      </p>  
      
      <p>  
      
      <label for="email">E-Mail</label>  
      
      <?php echo $this->tag->textField("email"); ?>  
      
      </p>  
      
      <p>  
      
      <?php echo $this->tag->submitButton("Register"); ?>  
      
      </p>  
      
      </form> 

        OUTPUT

        Phalcon Forms 2

        Validating Form

        Phalcon forms are integrated with the validation component to offer instant validation.

        <?php  
        
        use Phalcon\Forms\Element\Text;  
        
        use Phalcon\Validation\Validator\PresenceOf;  
        
        use Phalcon\Validation\Validator\StringLength;  
        
        $name = new Text(  
        
            'name'  
        
        );  
        
        $name->addValidator(  
        
            new PresenceOf(  
        
                [  
        
                    'message' => 'The name is required',  
        
                ]  
        
            )  
        
        );  
        
        $email = new Text(  
        
        'email'  
        
        );  
        
        $name->addValidator(  
        
            new PresenceOf(  
        
                [  
        
                    'message' =>'The email is required',  
        
                ]  
        
            )  
        
        );  
        
        $form->add($name);  
        
        $form->add($email);  
        
        ?>

        Form Elements

        Phalcon provides a set of built-in elements to use in your forms. All these elements are located inside Phalcon\Forms\Element.

        NameDescription
        Phalcon\Forms\Element\TextGenerate INPUT[type=text] elements
        Phalcon\Forms\Element\PasswordGenerate INPUT[type=password] elements
        Phalcon\Forms\Element\SelectGenerate SELECT tag (combo lists) elements based on choices
        Phalcon\Forms\Element\CheckGenerate INPUT[type=check] elements
        Phalcon\Forms\Element\TextAreaGenerate TEXTAREA elements
        Phalcon\Forms\Element\HiddenGenerate INPUT[type=hidden] elements
        Phalcon\Forms\Element\FileGenerate INPUT[type=file] elements
        Phalcon\Forms\Element\DateGenerate INPUT[type=date] elements
        Phalcon\Forms\Element\NumericGenerate INPUT[type=number] elements
        Phalcon\Forms\Element\SubmitGenerate INPUT[type=submit] elements

        Comments

        Leave a Reply

        Your email address will not be published. Required fields are marked *