In Silverstripe < 3 you can do smth like this in a form action
Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
and then in Template you can check with <% if Success %> if the Form is submitted.
in >3.1 you'll get
Fatal error: Call to undefined method Director::redirect()
somehow one shold use SS_HTTPRequest but i do not get it how to use this guy.
I want to show a simple success message after form submission.
Assuming you are handling the form submission in a Controller to redirect you could use
$this->redirect( Director::baseURL() . $this->URLSegment . "/?success=1" );
Related
With Zend Framework 1.10 I'm having a list with articles and an search field.
When typing something in the searchfield and hit the search button it generates me the following url:
https://example.org/products?category=12&no=
On the result of the searchpage you'll find the products matching to the search field.
If there are more than 10 products with the search attribute, there is a next button. For this next button im using the following code to generate the url, which will sadly not extend the already passed arguments (category and no).
<a href="<?php echo $this->url(array('page' => $this->next)); ?>">
<?php echo $this->translate('NextPage'); ?> >
It will redirect me on https://example.org/products
How to add the already passed GET argument from the form?
You can store the GET parameters from the search in the session so when you are requesting with the next url, you can retrieve the parameters previously entered in the search form.
I have a custom redirects in place for my devise sign up forms, I have two forms, one for an individual and another for a company. I have added this in the create action of the registrations controller:
if resource.company_form
redirect_to new_user_registration_path(company: true)
else
redirect_to new_user_registration_path
end
In doing this though I have lost all the devise error messages, as in the don't display any validation errors, so I need to send the error messages along with the redirect, don't I? However, I am not sure how.
So far I have tried printing the error messages to the console:
ap(resource.errors.full_messages)
[
[0] "Email can't be blank",
[1] "Password can't be blank",
[2] "Company name can't be blank"
]
Whereas this:
ap(resource.errors)
#messages={:email=>["can't be blank"], :password=>["can't be blank"], :company_name=>["can't be blank"]}
How would I get the error messages to be displayed above the form again?
The magic of devise error messages is made with respond_with method.
Hence you can change the redirect_to for a respond_with block
respond_with(resource) do |format|
if resource.company_form
format.html { render 'new', locals: { is_organisation: true } }
else
format.html { render 'new' }
end
end
and in your view
<% params[:organisation] ||= is_organisation -%>
Not sure if this will help, but you can customize this even further with your messages. First things first it's a good idea to actually ensure this works first. Then customize it further.
Add your toastr gem to your gemfile.
gem 'toastr-rails', '~> 1.0'
In your application.js you will need to add //= require toastr
In your stylesheet.scss you will need to import toastr #import "toastr";
Then run bundle in your terminal
In your views/devise/registrations/ folder and views/devise/password/ folder, the pages inside are your devise views that show the error messages. There you will find the error messages by devise. <%= devise_error_messages! %>
So what you wanna do, is customize these messages.
Now go to views/shared and create a new file and name it _devisemes.html.erb
<% unless resource.errors.empty? %>
<script type="text/javascript">
<% resource.errors.full_messages.each do |value| %>
toastr.error('<%= value %>')
<% end %>
</script>
<% end %>
After you have saved this file. Just go to the following files and find
<%= devise_error_messages! %>
Replace it with <%= render 'shared/devisemes' %> in the following files below:
views/devise/registrations/edit.html.erb
views/devise/registrations/new.html.erb
views/devise/password/new.html.erb
views/devise/password/edit.html.erb
Now logout and go to create an account and without an email, or password and test it. You will notice all the error messages from devise showing up with toastr.
I have a form with a couple search options, like a checkbox array and radio button. By using the form validation library I have the form repopulating after a submit, like so:
echo form_checkbox('check_track[]', '1', set_checkbox('check_track[]', '1', TRUE));
echo form_dropdown('select_year', $options, set_value('select_year', '2013'), $attribs);
I also save all the form options (by storing the post) into session userdata. Is it possible to repopulate all the fields from the session data if $_SERVER['REQUEST_METHOD'] !== 'POST' but keep repopulating based on form validation otherwise?
The easier would probably be to separate the form generation from the value generation. In the snippet you provide, the value is read directly from the submitted form.
I would advise you, in you controller or your model to generate a data structure, each field corresponding to one of the form field.
For each, the value would either be the default, either the one stored in the session if it matches you condition ie: valid data and not after a POST if I understood you well.
I ended up just faking that a POST had happened before the form validation stuff ran to get repopulation to work:
if(!isset($_POST['something']) && $this->session->userdata('something'))
{
$_POST = $this->session->all_userdata();
}
$this->form_validation->set_rules('something', 'stuff', 'required');
.
.
.
What is the easiest way to categorise (warning, success, error) flash messages in Zend Framework using the FlashMessenger helper? I also want a single method to check for messages where the controller may not necessarily have forward the request on. At the moment, I believe this is done via FlashMessenger::getCurrentMessage()?
In you're controller you can do this :
$this->_helper->FlashMessenger(
array('error' => 'There was a problem with your form submission.')
);
$this->_helper->FlashMessenger(
array('notice' => 'Notice you forgot to input smth.')
);
In you're view you can echo the notice like this :
<?php echo $this->flashMessenger('notice'); ?>
And the error like this :
<?php echo $this->flashMessenger('error'); ?>
Edit:
Check this link :
... Calling the regular getMessages() method here won't work. This only returns messages which were stored in the appropriate ZendSession namespace when the FlashMessenger was instantiated. Since any messages added this request were not in the ZendSession namespace at that time (because the FlashMessenger was instantiated in order to add the messages) they won't be returned by getMessages().
For just this use-case, the FlashMessenger also provides a getCurrentMessages() method (and a related family of current methods) which returns those messages set on the current request.
Okay, thanks for everyone's input I have however implemented a different approach.
I already had a parent controller that extends Zend_Controller_Action where I've placed common logic across the application, so in the postDispatch() method I merged the getCurrentMessages and getMessages into a view variable.
public function postDispatch()
{
$messages = array_merge(
$this->_helper->flashMessenger->getCurrentMessages(),
$this->_helper->flashMessenger->getMessages()
);
$this->view->messages = count($messages) > 0 ? $messages[0] : array();
}
I set the message via a controller action like;
$this->_helper->flashMessenger(array('error'=>'This is an error'));
And in my layout file, I use a conditional on the $messages variable;
<?php if(count($this->messages) > 0) : ?>
//.. my HTML e.g. key($this->messages) returns 'error'
// current($this->messages) returns 'This is an error'
<?php endif; ?>
This works for me as the messages is categorised and can be obtained from the current request in addition to the next redirect.
Two ideas.
1. PHPPlaneta
Check out the source code of PHPlaneta by Robert Basic:
https://github.com/robertbasic/phpplaneta
He uses the standard FlashMessenger action helper:
$this->_helper->flashMessenger()->addMessage(array('fm-bad' => 'Error occurred')
Then defines a view helper called FlashMessenger so that he can access the messages. In his layout or view script, he simply calls:
<?php echo $this->flashMessenger(); ?>
The view helper uses the key (ex: 'fm-bad') to set up CSS styling for the output message.
2. PriorityMessenger
Check out the Priority Messenger view helper from Sean P. O. MacCath-Moran:
http://emanaton.com/code/php/zendprioritymessenger
The thing I like about this is that this whole business of saving messages for display on the next page load strikes me as something that should be completely within the view. So in your action, before your redirect, you populate the view helper with your messages and your priorities. Then, in your layout or view script, you output those messages with their priorities via the same view helper.
I'm trying to get the standard error messages to show up in zend_form but they don't.
I have this:
if ($form->isValid($formData)) {
// do stuff
} else {
$form->populate($formData);
$this->view->form = $form;
}
When I post an invalid form, the form does show up in the view like it's supposed to, but from the tutorials it seems like error messages should show up by default?
What am I missing?
Thank you for your help!
The error messages are applied using Decorator Pattern. There are some Zend Form Element Decorators present in the form by default.
I guess you have overwritten the default decorators, using e.g. setDecorators().