Retrieving the identifier from a value within a Symfony controller

One thing I am looking to achieve is to retrieve an Id based on a folder number. Both the Id and folder number (which is unique) are located in the same controller.

I input the folder number from a text box and then need to direct the user to a /Id page.

My question is how should I approach this?

Do I create a method getIdFromFolderNumber() within my controller and then invoke this function in my JavaScript function? Or should I handle everything within a JavaScript function (I know I'll need a JS function as I plan to use AJAX for redirection and input value retrieval)?

I'm not looking for actual code here, just guidance on the approach I should take to understand how to do it. I've recently started work on a major project and, being a junior developer, I feel a bit overwhelmed.

Your assistance is greatly appreciated.

Answer №1

To achieve the desired outcome, there is no need for JavaScript. It simply involves setting up a basic form and redirection process.

Upon submitting your form to obtain the number:

$number = $form->get('number')->getData();

You can then use this number as criteria for your doctrine request:

$id = $yourEntityRepository->findBy(['number' => $number)])->getId();

With the ID in hand, you are now ready for the redirection:

return $this->redirectToRoute('entity_show', ['id' => $id]);

Was this the solution you were seeking?

Note: The variables $number and $id do not need to be stored; they were used here for clarity. You can directly substitute them with the appropriate requests where necessary.

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

Is it necessary to always pause before I click?

Currently, I am in the process of writing tests for my website using WebdriverIO with Mocha and Chai. However, I encountered an issue where my element is not rendered before attempting to interact with it. it('select application', function(done) ...

How can I determine the quantity of pairs in an array using JavaScript?

My current approach is not providing me with the desired result. I am facing an issue where pairs of 1 are being counted multiple times, which is incorrect. What I actually need is to count only the "perfect" pairs, such as the pair between 5 and 2 in this ...

Adding vibrant colors to the expansion panel within Material UI to enhance its visual appeal

How can I customize the color of the expansion panel in material ui when it is open? Is there a way to override this in material ui? Feel free to check out the code example at this link ...

Encountering a 404 error while using the Post method with Axios in a ReactJS

I am experiencing an issue with axios.js. I am attempting to use the POST method, but it is resulting in a 404 error and I am unsure how to resolve it. The error message is as follows: > POST http://localhost:3000/api/match 404 (Not Found) > createE ...

React Server side error: Router.use() expects middleware function instead of a string

Currently, I am in the process of preparing my server to handle ajax calls. As part of my learning journey with React, I have been exploring server-side functionalities. While following various tutorials, I configured the server.js to connect with a postgr ...

What is the best way to extract data from a JavaScript object received from multer?

Currently, I am facing an issue while trying to utilize multer for handling the upload of a CSV file in Express. The goal is to parse the uploaded file line by line. Although I can successfully retrieve the file as an object stored in req.body, I encounter ...

Tips for retaining form data after refreshing the page

Hey there, I want to create a library system using PHP and HTML, but I'm facing an issue with the search functionality. When I search for a book, the results are displayed correctly. However, to view more information about a specific book, I have to ...

Using AJAX in a loop presents a challenge: What is the best way to initiate an ajax request for every item in an array?

Hey there! I'm a new member of the StackOverflow community and I could really use some assistance. My current challenge involves performing an inverse geocode to retrieve addresses from coordinates. I have a functional URL that works with ajax, but I ...

Can one jQuery script be used for multiple ajax 'like' buttons?

Hey there! I'm working on an Ajax 'like' button that utilizes jQuery. This button will be utilized multiple times on a single page. I'm looking for a way to streamline the process and avoid including the jQuery script multiple times. Is ...

Is it possible in Angular.js to limit the visibility of a service to only one module or to a specific group of modules?

When working with Angular.js, the services declared on a module are typically accessible to all other modules. However, is there a way to restrict the visibility of a service to only one specific module or a selected group of modules? I have some service ...

encountering an issue "Error in p5practice.js:97 - Uncaught TypeError: Unable to access property 'y' of undefined"

While working on the paddle section of my code and resolving other errors, I encountered a new issue. After fixing the previous errors, I received the following error message: "p5practice.js:97 Uncaught TypeError: Cannot read property 'y' of unde ...

The dynamic form functionality is experiencing issues when incorporating ng-container and ng-template

I'm currently working on a dynamic form that fetches form fields from an API. I've attempted to use ng-container & ng-template to reuse the formgroup multiple times, but it's not functioning as anticipated. Interestingly, when I revert b ...

Is the sudden disconnection from Chrome after a WebSocket handshake related to a domain mismatch or is it possibly a bug in Chrome?

I created my own WebSocket server using Python, but I encountered an issue where Chrome 4.0.249.78 dev (36714) always disconnects after the handshake process. Wanting to rule out any issues with my code, I tested it using the WebSocket server from , only t ...

Quasar Troubles with Touch Swipe Gestures

I'm facing two issues, the first being that the directives are not functioning as expected. For example, I've implemented a swipe only to the right: <div class="q-pa-md row justify-center"> <q-card v-touch-swipe. ...

CSS Attribute Selector Wildcard Capture Tool

Suppose I have the following HTML structure: <div class="tocolor-red"> tocolor </div> <div class="tocolor-blue"> tocolor </div> <div class="tocolor-green"> tocolor </div> <div class="tocolor-yellow"> tocolor ...

Issues arise when the Slick functionality fails to load following an ajax request

I've come across a situation similar to the one on this post. I'm having trouble getting my slick carousel to work after a successful ajax call. Despite trying all the solutions provided, it's still not functioning as expected. The code for ...

PHP: A guide on validating textboxes with jQuery AJAX

How can I use jQuery AJAX to validate a textbox on focusout? The validation process includes: Sending the value to a PHP page for server-side validation Displaying an alert message based on the validation result I have only impl ...

Issue with manipulating currency conversion data

Currently, I am embarking on a project to develop a currency conversion application resembling the one found on Google's platform. The main hurdle I am facing lies in restructuring the data obtained from fixer.io to achieve a similar conversion method ...

The Strong Password checker fails to identify the presence of a forward slash

Here is a regex I have for validating a strong password: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d!$\/%@]{6,20}$/ Criteria: Alphanumeric with special characters $/%@ At least 1 number At least 1 lowercase letter At least ...

Shuffle the dots on the sphere and assign each one a unique identifier

I am currently working on creating a spherical design using three.js. My goal is to have clickable dots and meshes embedded within this sphere. To achieve this, I believe that assigning names to each dot on the sphere will be essential. I have two specific ...