Validation of Zend_Captcha_Image when using in form - zend-framework

I have the following code in a form in zend framework application.
$captcha = new Zend_Form_Element_Captcha('captcha', array(
'label' => "",
'captcha' => 'image',
'captchaOptions' => array(
'captcha' => 'image',
'font'=> APPLICATION_PATH . '/../public_html/assets/fonts/akbar.ttf',
'imgDir'=> APPLICATION_PATH . '/../public_html/assets/captcha/',
'imgUrl'=> '/assets/captcha/',
'wordLen' => 1,
'fsize'=>20,
'height'=>60,
'width'=>200,
'gcFreq'=>50,
'expiration' => 300)
));
and the display of the form element is as expected.
When I try to validate the form using the following code it always returns error even if I enter the captcha correctly.
if($this->getRequest()->isPost()) {
if($this->view->form->isValid($_POST)) {
Any solution on how to validate it correctly will be of great help.
Thanks
Nizam

I'm going to guess that you're also doing $captcha->generate(); before the isPost() check. The problem with this is that when you submit the form, you're generating a new CAPTCHA before checking the POST data, so the check will always fail because it's validating it against the new CAPTCHA. The solution is simply to move the generate call further down.
I put up a blog post a while back with some code examples of this component, see here - http://tfountain.co.uk/blog/2009/1/6/zend-captcha-image-experiences but the post is a couple of years old now so some things may have changed.
If this doesn't help, please edit your question to include a bit more code so we can see what else might be causing the problem.

Check this post: Zend Framework: Captcha problem
Basically you have to remove the "viewhelper" from the element.
ex.:
$form->getElement('captcha')->removeDecorator("viewhelper");

Related

Unable to show a newly created PHP page from Yii2 (on XAMPPV3.2.2)

I am a newbie.
Installed XAMPP 3.2.2 on Windows 10.
Installed Yii 2.0.13 (advanced template) in XAMPP.
Started the Apache Server.
Yii2 Development Environment Initiated. Were able to open the page from browser and all the links are working at this point in time.
Navigated to the FrontEnd->Site->Layout and copied about.php to
testabout.php
In the "FrontEnd"->"Views"->"Layout"->Main.PHP, added a new Menubar Item as shown in the blocked letters (Italic Items are what was already in there).
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
['label' => 'Custom Link', 'url' => ['/site/testabout']],
];
A new link called "Custom Link" is added to the Landing Page with no issues.
When I click on the link, it shows the below error message
Not Found (#404)
Page not found.
The above error occurred while the Web server was processing your request.
Please contact us if you think this is a server error. Thank you.
Need your inputs to fix this issue.
For sure, i searched for a similar issue in this forum and either I found them not related to the steps I performed or I cannot understand their answers.
Some Updates I have done are as follows:
1) Created a .htaccess file in the frontend->Web Folder and Backend->Web Folder;
you need to create an action in site controller
1.create an action in
frontend->controllers->siteController action name = 'testabout'
example,
public function actionTestabout()
{
$return_value = 'Haii';
return $return_value; //this will print 'Haii' on screen..
}
but, you need to render a view file..(the proper way)
example,
make a new file named '
test_about.php
' and put it in
frontend->views->site
.
then add a something in
test_about.php
ex: echo 'haii';
then, modify your action like this,
public function actionTestabout()
{
return $this->render('test_about); //shows 'haii'
}
I hope this will help you

Laravel 4.1, call error message for specific validation point of one input / possible?

I have a question concerning the Laravel 4.1 validators.
$validator = Validator::make(
array('name' => 'Dayle'),
array('email' => 'required|min:5|unique:users')
);
Is it possible to call a specific validation error for the case when the entered email is not unique? Reading the docs I only saw that one is able to define the error message if the validation for 'email' fails. However, if someone enters an email address but this one is already in the database it would be awesome to show the user exactly that he passed "required" but did not pass "unique".
The third parameter of Validator::make() lets you pass in an array of messages. More on this here: http://laravel.com/docs/validation#custom-error-messages
As it says in the above link, you can specify field-and-rule-specific messages by using the dot syntax email.unique. In your case this would be:
$validator = Validator::make(
array('name' => 'Dayle'),
array('email' => 'required|min:5|unique:users'),
array('email.unique' => 'This email is already being used by another user.'),
);

Zend Routing / URL helper . Have a global parameter show up first in URL

Was curious if anyone knew the best way to implement the following: I have a parameter in my zend framework 1.12 app which effectively controls the 'scope' of things, and is a field in every table in my db to represent the scope of a row. It is a simple integer variable, and can be thought of as 'buildingID', so it controls which 'building' we are working with.
In a plugin, I have:
Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('building', DYNAMIC_INT);
which accomplishes what I need. When I build a URL with the URL view-helper I have my parameter, but it is always at the end of the parameter list. I know this is trivial from a programming perspective, but how would I achieve 'prepending' this global param to my url parameters?
site.com/admin/controller/action/param1/xyz/param2/xyz/building/2
to become
site.com/admin/controller/action/building/2/param1/xyz/param2/xyz ?
Open to any ideas. If you want me to overload the url view helper, can you provide some example code, because I had trouble setting up this class.
Thank you all!
You can use a custom route to accomplish this. Setup the route somewhere in your bootstrap file:
$route = new Zend_Controller_Router_Route(
':controller/:action/building/:building/*'
);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('building', $route);
And then, assuming that the following has been called at some point prior to using the url view helper...
Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('building', DYNAMIC_INT);
...you can specify the route as the second argument of the helper:
echo $this->url(array(
'controller' => 'admin',
'action' => 'controller',
'param1' => 'xyz',
'param2' => 'xyz',
), 'building');
// /admin/controller/building/1/param1/xyz/param2/xyz

Zend Routes translate URL's

1) I have a controller "calendar" and have action "showDate" which gets date via url. So, url is something like "calendar/show-date/date/2012-07-22"
2) I have a link to display all entries, "calendar/"
So, I want to create routes so my links look like "kalendar/2012-07-22" and "kalendar/".
Can anyone help me?
According to this post:
http://www.z-f.fr/forum/viewtopic.php?id=5138
The solution is to add '#locale' => $lang to the params.
$this->url(array('lang'=>'it','#locale'=>'it'))
It works very well for me.
I've been looking into translating the URL with Zend_Translate and I came across this sites' plugin that attempts to auto-translate URL segments (module/controller/action).
http://blog.helmich.cz/305-howto-simple-multilingual-routes-in-zend-framework/
The nice thing is that it's a modified custom router class that can function similar to Zend_Router so it's relatively familiar off the bat.
$pages = new MyApp_Controller_Router_Route(
':locale/:#controller/:#action/*',
array(
'controller' =>; 'index',
'action' => 'index',
'locale' => 'cs'
)
);
$router->addRoute('pages',$pages);
The thing you'll need is to have a language ID in your URL (called :locale in the above example) so your Zend_Translate can set the proper language.
www.example.com/en/calendar/2012-06-22/
www.example.com/fr/calendrier/2012-06-22/
www.example.com/de/kalender/2012-06-22/
www.example.com/it/calendario/2012-06-22/
I've only slightly played around with this concept but I recall that it had promise. You'll have to get more familiar with Zend_Translate: http://framework.zend.com/manual/en/zend.translate.html
I hope that helps!
Cheers!
You could re-route all calls of calendar to kalendar. There are two possibilites, either you do it with Zend (preferable) or you change your webserver configuration to rewrite calls to calendar with a HTTP 302 (ugly).
You should however consult the official Zend Documentation, which is pretty good
You have to setup custom routes, this is my way:
in folder application/configs/ create file named "routes.ini"
Put in file your route:
;index-homepage, parameter date isn't required
;"index" is key of your route
routes.index.route = "kalendar/:date"
routes.index.defaults.controller = calendar
routes.index.defaults.action = show
routes.index.defaults.date =
So in your bootstrap.php define that config file:
protected function _initRoute() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addDefaultRoutes();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router->addConfig($config, 'routes');
}
And that's it, you can call URL
www.website.com/kalendar
and
www.website.com/kalendar/2012-1-1
See answers in this question for details:
Simple rewrites in Zend Framework

Using Zend Routes? to customized url

This is getting me confused.
How do I customize /state/state_name to be replaced by /state/state_name.php?
Initially state_name is a variable and I don't want each of the state to be an action in my StateController. Instead, I would like to take that variable and process it in a specific action loadAction() to deal with the contents.
This is because the url /state/state_name.php is already SEO optimized and I want it to stay in that form using Zend Framework.
Thanks for any help. Any suggestions would be gladly welcomed!
Maybe something like:
$route = new Zend_Controller_Router_Route_Regex(
'state/([^/]+)\.php',
array(
'controller' => 'state',
'action' => 'load',
),
array(
'state_name' => 1,
),
'state/%s.php'
);
But why is is so important to keep the .php suffix? If anything, it unnecessarily exposes the underlying server-side technology employed to construct the page. What if you later change the entire site to Ruby On Rails or to Django? Why tie yourself to PHP?
If you are really sweet for a suffix, then I'd imagine that .htm or .html is better. But even that leads you down a path with a potentially bad smell. What if you want to use this same controller to be the endpoint of an AJAX request that returns JSON or XML data?
Personally, I'd bail on the suffix.