So, I've been working on a website for a reggae backing band and on the 'booking' page I have a form. After submitting the form I first (ofcourse) check if all required fields are filled in, if something else went wrong or if it succeeded.
But, I have a couple of language files (nl, de, fr and en). On the HTML page I first check if an error was set inside the PHP and if there is an error, I want to output it in the right language.
So in case of an error, the HTML would have to look like this
{{ lang.booking.{{ form_error }} }}
because I am sending the error type in the PHP as well, but this doesn't seem to be possible (obviously).
Can someone help me out or tell me how to get around this problem?
Thanks in advance!
You can use the attribute function to access a dynamic attribute of a variable.
{{ attribute(lang.booking, form_error) }}
You can use translation on constraint messages as you can learn in this article: http://symfony.com/doc/current/validation/translations.html
Related
upon inspecting my console I get the error like below:
Uncaught SyntaxError: Invalid or unexpected token.
I've traced back this line to my script in the footer portion of my folder and the line goes
$('<?php echo $active_accordion_id ?>').addClass('show');
My noob question is this, why is this an error? This prints out what is needed for me to toggle the active accordion upon visiting the respective pages, according to the ID passed in each respective controller function. Since it is working as intended I am confused as to why it is an error.
Should I be bothered to change it since it is working as intended but it does bother me when I go into inspect mode. I would think that the best practice is to deal with this even if there might be no ramification from it (I hope not!). Shed some light if it would.
Thank you for reading my question!
I think it's not good to call and use PHP functions directly in the script.
You can create a hidden input tag for your element what you want to add some class in your view
your view:
<input type="hidden" id="element_id" value="<?php echo $active_accordion_id; ?>">
your javascript code
var id = $("#element_id").val();
$('#'+id).addClass('show');
I noticed why the error appears. All pages that loads the script doesn't necessarily have the accordion ID defined, so the error shows on those pages alone in console. So all I did is to assign null values for the active_accordion_id on those pages to not have that error appear.
What I wanna ask is I dont know where it comes and I am 1200% sure that there are no debug enabled somewhere or outputs something in my application that on the left top of my window always it outputs my primary key field names and sometimes ids after a form post or server side operation. What I use is cakephp >2 framework.
Anybody helps?
Thanks
I think in your respective controller function you are echoing something when post event
is fired
ex.
if ($this->request->is('post'))
{
echo $id;
}
Please Check your controller function.
Happening in every controller actions? Why not do a text search using your IDE, look up for suspicious "echo" "debug", "var_dump" or "print_r" in your app folder?
Worst case is something in your view has wrong closing tags that may interfere your page rendering.
Also view source on the offending pages to see whether it's appearing before your DOCTYPE or after. Before means the echoing is before view get rendered, after means it could be during the view rendering.
Hope it helps
I have a form at the bottom of a long page, if a user fills out the form but it doesn't validate the page is reloaded in the typical codeigniter fashion:
$this->load->view('template',$data);
however because the form is way down at the bottom of the page I need the page to load down there like you do with HTML anchors. Does anyone know how to do this in codeigniter?
I can't use the codeigniter
redirect();
function because it loses the object and the validation errors are gone. Other frameworks I've used like Yii you can call the redirect function like:
$this->redirect();
which solves the problem because you keep the object. I've tried using:
$this->index()
within the controller which works fine as a redirect but the validation errors are in another method which is where the current page is loaded from:
$this->item($labs)
but when I use this it get stuck in a loop
Any ideas? I've seen this question a lot on the net but no clear answers. I'm researching using codeigniter "flash data" but think it's a bit overkill.
cheers.
I can't personally vouch for this, but according to this thread if you append the anchor to the form's action, it will work.
CodeIgniter helper:
<?php echo form_open('controller/function#anchor'); ?>
Or vanilla HTML:
<form method='post' action='controller/function#anchor'>
If you were open to using Javascript, you could easily detect a $validation_failed variable and appropriately scroll. Or, even better, use AJAX.
Another option is to put the form near the top of the page?
Ok, as far as I understood your problem, it isn't much related to the back end(codeigniter). You want the form at the bottom of the page to be 'what-users-sees-on-page-load' (since you mention anchors).
Now, what you can do is, you can set delimiters for your validation error messages using:
echo validation_errors('<div id="bottom_form_error">', '</div>');
Using jQuery ScrollTo, do:
$( function() { $('#bottom_form_error').ScrollTo(); } );
And, the user will be scrolled to the errors at the bottom of the page. Don't forget to include jQuery too.
Anchor hash fragment click is different - it is scrolling at ∞ speed.
I hope that is what you wanted.
P.S. I am ignoring what you said below this line:
Does anyone know how to do this in codeigniter?
as I felt it is not really relevant to the question.
I have following PHP code:
if($_SESSION['msg']['login-err'])
{
echo '<p class="error">'.$_SESSION['msg']['login-err'].'</p>';
unset($_SESSION['msg']['login-err']);
}
Everything working fine. 'P' with error is displayed correctly inside element.
My question is how to specify where my error should be displayed?
E.G. Not as the first element inside the form, but
as the second for example
just before <input name="user" />
??
Any suggestion much appreciated.
Since you're (somewhat naively) testing for errors and displaying errors at the same time, you should just move the code lower in the page so that the test/output happen where you want it to display.
Ideally you should decouple your display code from your logic. Store your errors for later display in an $errors variable which is passed to your template to be rendered.
Rather than placing the echo where it exist now, why not change your code to something like this:
$error;
if($_SESSION['msg']['login-err'])
{
$error = $_SESSION['msg']['login-err'];
unset($_SESSION['msg']['login-err']);
}
Then, right above your input tag, do something like this...
<?php echo $error;?>
It's not the best way to do error handling...but it should do what you are asking.
The "where" depends on you frontend design.
In a login form I would recommend to display errors at the top or bottom of the whole form. In a contact/registration form it makes more sense to show error beside the field ( on the top or left ).
Quite different thing is this combination of logic and presentations. They should be separated, because each piece of code should have only one reason to change. Your piece has two reasons: design or logic.
You would gain a lot if you did some extended studies in MVC and OOP in general.
I have a form with multiple fields. Some file and input.
I was working on it for some time.
Everything was working as it should be until a few hours back suddenly the form is not submitting to the right.
I have no idea what went wrong.
Action to submit the form is the same as the view it generated.
After i submit the form browser does not show anything default template address stays the same as the form submits to same view. But i do have redirect statement if the data is saved correctly..
As the form is submitted browser goes blank, not even the default template is shown ... and to add to my pain no errors ... Things are looking worse as they are..
So would really appreciate any pointers..
Thank You.
A quick thing to check for is if there are any whitespace characters at the end of your models or controllers (actually, any .php file) after the '?>' That can cause the behavior you describe.
<?php
class YourController extends AppController {
/*** your code here ***/
}
?>(whitespace chars here)
Something that I do that helps with this problem is to remove the '?>' on my models and controllers. The php interpreter will consider the EOF as the closing tag.
<?php
class YourController extends AppController {
/*** your code here ***/
}
// END
#webbiedave has good advice too, cake has great debugging, although you may need to add the following to your layout template depending on which version of cake you are using...
<?php echo $this->element('sql_dump'); ?>
I put it right at the end of my default template
Please post the code for the controller method that handles the form view.
Also post the <form action=... code.
If it isn't too long, also post the whole of the form html instead of (2.)
With the code, we can help you debug the problem.
To ensure that you can see any errors generated by PHP, open app/config/core.php in your editor and search for debug. Set the debug level to 2 - Configure::write('debug',2);