Basic Hidden field in yii

Basic Hidden field in yii

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Hidden Fields in Yii Without Model Modifications

You often need to pass data between form submissions, perhaps for CSRF tokens, session identifiers, or simple static values like a status code that shouldn't be touched by the user. In standard PHP, this is trivial: you just write an <input> tag with a type="hidden" attribute and set your value. If you are working in Yii, specifically within a view file, you might wonder how to replicate that exact behavior without digging into models or rewriting controller logic.

The answer lies in the Blade template engine (for Laravel-based projects) or the raw HTML helpers available in older Yii versions, but the most robust modern approach usually involves leveraging the @csrf directive and understanding how views render input fields. However, if your requirement is strictly to output a static hidden field with a specific value like "a", you generally do not need to modify your models or controllers. You can inject this directly into your view using standard HTML syntax or helper methods depending on the specific framework version you are targeting.

If you are working in a Laravel environment, which is the most common context for modern Yii-like development questions today, you have two primary ways to achieve this. The first is writing raw HTML directly within your Blade template. Since Blade allows you to write standard PHP tags inside curly braces {}, you can simply echo out an input string:

<input type="hidden" name="field_name" value="a">

This works perfectly for static values. Because the value is hardcoded as a string literal, no database query or model attribute binding occurs. This keeps your models clean and prevents accidental data contamination if you later decide to bind that field to a specific input property in your form request.

Alternatively, you can use Blade's syntax to make it slightly more dynamic if the value needs to come from a variable:

<input type="hidden" name="field_name" value="{{ $staticValue }}">

However, be cautious with escaping. If your static value contains special characters or you are pulling it from an external source, ensure it is properly escaped to prevent XSS vulnerabilities. In Blade, {{ }} escapes output by default, which is safer, whereas {!! !!} outputs raw HTML. Since your example has a simple value "a", standard escaping is sufficient and recommended.

Why You Should Avoid Modifying Models

The user in the source material mentioned avoiding model modifications. This is a sound architectural decision. In MVC frameworks like Yii or Laravel, models represent data entities and their business logic. They are not responsible for rendering specific HTML input types or managing static form fields that exist purely for transport purposes.

If you were to add field_name as a property to your Model class, the framework might attempt to validate it, sanitize it, or store it in the database on the next save operation. Even if you set up fillable rules to ignore it, you are adding unnecessary complexity to your entity. By keeping the hidden field in the view layer (the controller's responsibility for rendering), you maintain a clear separation of concerns. The controller handles the form structure, and the model handles data persistence.

Integration with CSRF Protection

A common pitfall when adding hidden fields is forgetting about Cross-Site Request Forgery (CSRF) tokens. In modern Yii/Laravel stacks, frameworks automatically generate a token and require you to include it in every POST request. If you are adding a custom hidden field for error checking or static data, you must ensure the CSRF token is also present.

In Laravel, this is handled by the @csrf directive, which places a hidden input containing the token before your custom fields:

@csrf
<input type="hidden" name="field_name" value="a">

If you are using an older Yii version that does not have directives, you must manually generate the token in your controller and pass it to the view, then echo it into the hidden field. Failing to do so will result in 419 (Page Expired) or validation errors when submitting your form, regardless of the correctness of your custom hidden field.

Final Considerations on Value Types

Your source material specified a static value of a. If you intend for this to be treated as an integer by your backend logic, ensure you do not include quotes around the value in the HTML if the framework expects raw integers, though typically strings are safer unless strict type hinting is enforced on the input side. Always verify how your controller or service layer parses the $_POST array. Most frameworks will coerce string "a" to integer 1 if you cast it, but relying on implicit coercion can lead to subtle bugs later in the application lifecycle.

Our network