FormBuilder Interface Silex File Input

Having recently subscribed, I have come to seek assistance on a platform where I have previously found many solutions to my issues.

I have completed a course on Openclassrooms (Transitioning to a Professional PHP Architecture) which has equipped me with the necessary skills to work with Silex, Twig, and Bootstrap. As I delve into creating a website using Silex's documentation, there is a specific challenge I am facing with the FormBuilderInterface.

class ActivityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', 'text')
            ->add('public', 'choice', array(
                'choices' => array(
                    'children' => 'children',
                    'adults' => 'adults',
                    'family' => 'family',
                    ),
                ))
            ->add('withHorses', 'choice', array(
                'expanded' => 'true',
                'label' => 'With horses',
                ))
            ->add('content', 'textarea')
            ->add('image', 'file', array(
                'required' => 'false',
                'data_class' => null,
                ));
    }

    public function getName()
    {
        return 'activity';
    }
}

The data_class set to null is crucial to avoid an exception.

Below is the code snippet showcasing the form display:

{{ form_start(activityForm, { 'attr': {'class': 'form-horizontal'} }) }}
    <div class="form-group">
        {{ form_label(activityForm.title, null, { 'label_attr':  {
            'class': 'col-sm-4 control-label'
        }}) }}
        <div class="col-sm-6">
            {{ form_errors(activityForm.title) }}
            {{ form_widget(activityForm.title, { 'attr':  {
                'class': 'form-control'
            }}) }}
        </div>
    </div>
    <!-- Add more form field blocks as needed -->
{{ form_end(activityForm) }}

The file input appears in the form but check here for its current appearance.

Upon inspecting the DOM, an anomaly was observed here. The div housing the file input lacks the "form-group" class and attributes specified in the view (class: form-control and accept: images/*) are not being applied.

Despite extensive research and efforts, I have failed to resolve this issue related to Silex. Any guidance on positioning the file input between the text area and submit button, aligning the label and input horizontally, and incorporating view-specific classes and attributes would be greatly appreciated.

If anyone can shed light on the usage of data_class, particularly the necessity of setting it to null to prevent exceptions when dealing with file inputs through that interface, it would be highly valuable information.

Thank you in advance!

Answer №1

Have you considered trying out BraincraftedBootstrap? Here's a helpful link with information:

It covers all the bases and actually gets the job done :)

Answer №2

Just a quick update!

I realized that the bug I was encountering was actually located behind the chair and the desk! The form I shared earlier was correct all along; it was simply my mistake in mixing up the files and referring to the wrong one. And for anyone planning to use my code, please note that 'withHorses' is intended to be a checkbox.

Therefore, make sure to adjust:

add('withHorses', 'choice', array(
                'expanded' => 'true',
                'label' => 'With horses',
                ))

to:

add('withHorses', 'checkbox', array(
                'label' => 'With horses',
                ))

More information available here

Thegeekrv, when using the FormBuilderInterface, I find that I already get a stylish view that aligns with Bootstrap's aesthetics, so I may not necessarily need BraincraftedBootstrap. Nonetheless, to enhance the appearance of file inputs, I have opted to utilize bootstrap-filestyle. You can see how it looks like here.

Thank you very much for your assistance and corrections!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP - Modify the final row within a loop

I'm currently working on creating a music playlist based on files in a specific folder. The only problem I'm facing is that the last row in the foreach loop is echoing an extra comma. Here is a snippet of the code: echo '<script type="te ...

Received a 'PDOException' error stating 'SQLSTATE[HY000] [2002] Connection timed out' while attempting to establish a connection with Google Cloud SQL

Hello everyone, I have been attempting to establish a connection with Google Cloud SQL from an external website. I have authorized the server's IP in Google Cloud under Instance -> Access Control -> Authorization The instance has been assigned an ...

Trouble with parsing JSON in PHP

I've been encountering issues with a PHP script I recently created. The problem lies in my inability to retrieve data from a JSON file named demo.json using PHP. Below is the content of the JSON file: { "checkouts":[ { "billing_address":{ ...

Filtering doctrine queries by related entities within the same table

I am currently implementing API platform into my Symfony project and facing an issue. Within my project, I have 3 tables: account, user, and account_user. An account can be linked to a parent account. Now, when making a GET request to /accounts, I need t ...

Enhance your Woocommerce shopping experience by showcasing additional attributes directly on your cart page

I am using Woocommerce along with Woocommerce Bookings, and I am looking to enhance the cart by adding more information once a user has added an item there. Currently, I have the starting date of the booking and its duration displayed with labels. <dl ...

Can I use newline escapes in a MySQL query?

While browsing through a different stackoverflow question, I came across the following code... $query=" SELECT account.id, client.client_id\n" . " FROM account, client\n" . " WHERE account.id = 19"; I'm wondering if those newline e ...

Generating a JSON object that includes arrays and defining key-value pairs

My PHP is producing a JSON output with three arrays: eventIDs, TipsTB, and TipsTW. When I pass this JSON to my HTML file, the array values are displayed with keys 0 and 1. How can I assign unique keys like tip1 and tip2? I am creating the array and encodi ...

PHP array utilized in a dynamic dropdown menu

I am working on creating a PHP array for a select list that has dynamic options populated using JavaScript. My goal is to collect all the options selected and display them on the next page. I was wondering if there is a better way to achieve this task. C ...

Encountering a problematic scenario after transitioning from PHP 5 to PHP 7, where a previously functional dynamic

I am in the process of repairing a website that I own which is currently not functioning. After careful examination, I have identified the issue in the following code: <?php global $options; foreach ( $options as $value ) { if ( isset( $value[' ...

Obtain the final timestamp in the array

I am facing an issue with my array. How can I retrieve the latest time from the array? Below is a sample of my code: Array ( [0] => 2015-08-04 01:00:00 [1] => 2015-08-03 16:00:00 [2] => 2015-08-03 10:00:00 ) This is just a glimpse of ...

Automated client connection negotiation for memcached's latest feature

My attempts to utilize memcached to store PHP session data have hit a snag. PHP keeps generating an error message indicating it can't establish a connection: Warning: session_start(): open(memcached:11211/sess_hitr4obt9ofmmsvk9kfl8euqt6, O_RDWR) f ...

The functionality of $GLOBALS['TSFE']->set_no_cache() seems to be ineffective starting from TYPO3 version 6.2.17 and newer versions

I previously implemented the 'set_no_cache' function globally in the initialize action of my extension. $GLOBALS['TSFE']->set_no_cache(); However, it seems that this method is not effective starting from typo3 version 6.2.17 onward ...

phpUsing a foreach loop to iterate through an associative array and

I have a database table in Microsoft Access with three fields: partno, description, and quantity. However, when I try to retrieve the data and display it in an HTML table, I encounter an error message. An issue occurred while trying to access the ' ...

Looping endlessly, causing the page to freeze and preventing it from loading. Time to take

I am in the process of setting up a Bitcoin payment gateway for transactions worth 0.25 BTC with just 1 confirmation required. I have designed a form (form.html) that generates a unique random address ($_POST['address']). Upon submission, I want ...

Substitute terms in web address

Can you assist me with replacing words in URLs/links for WordPress? For example, if there is a mention of "red" in my article, I want it to be replaced by "orange," but only in the URLs/links and not anywhere else in the article. Here are the rules: - Rep ...

I must output certain values from the nested arrays, so I attempted to use the foreach syntax, but for some reason, I am not executing it correctly

Having some trouble printing values from nested arrays using a foreach syntax. Here is the code snippet: <?php echo "<strong><h1>EXERCISES</h1></strong>"; /*THree friends (John Doe , Jane Foo , and Elvis Peanutbut ...

Incorporate External Data Source into Magento Admin Products Grid

I'm working on creating a split screen view with one grid displaying products from a remote repository, and another grid showing products from a local repository. To simplify things, all I really need help with is incorporating the remote source into ...

Displaying PHP content using JavaScript classes

I have a popup feature implemented in JavaScript and all the necessary scripts added to my HTML page. I am attempting to load a PHP page in the popup when the submit button of my form is clicked. The popup is functioning correctly for buttons like the one ...

(struggling beginner) Struggling to execute PHP on a website built with the Laravel framework

I have a PHP site that I developed. My client now wants to add some functionality, but whenever I try to set it up on my machine or host it on Hostinger, I keep encountering the following error: Warning: require(/home/u364558673/public_html/../bootstrap/a ...

In PHP, implement a function to insert a delimiter into a string retrieved from a

Merchant Id|Merchant|Website|Transaction In Period|Voids In Period|Gross Sales Amount|Total Commision in Period 9766|Mountains Plus Outdoor Gear|www.MPGear.com|1|0|88.91|8.89 12447|Meritline.com|www.meritline.com|5|0|52.86|3.71 16213|East Coas ...