I'm new in Zend Framework, and I'm having a problem.
usually when verifying data in forms we use javascript like bellow:
<html>
<head>
<script>
function verify(){
if( document.form.name.value=="") { alert("Error"); return false}
}
</script>
</head>
<body>
<form onSubmit="verify()" name="form">
<input name="name" />
<input type="submit" />
</form>
</body>
</html>
I'm using zend forms and i don't have a clue how to do it.
<?php
class Application_Form_Login extends Zend_Form {
public function init() {
$login = new Zend_Form_Element_Text('login');
$password = new Zend_Form_Element_Password('password');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel("Login");
$remember = new Zend_Form_Element_Checkbox('remember');
$this->addElements(array($login, $password,$remember ,$submit));
}
}
?>
Any help please.
thanks for any help.
You do it the same way as before, but here is how to tell Zend_Form to add the onsubmit event to your so that the HTML code generated contains the attribute in the <form> tag.
class Application_Form_Login extends Zend_Form {
public function init() {
$this->setAttrib('onsubmit', 'return verify()');
// rest of your form code to add elements
}
}
This adds an attribute onsubmit to the Zend_Form object, such that when the form is rendered your <form> tag has onsubmit="return verify()" in it.
Now you can just put the actual Javascript code to verify the form in the view script itself, or an external javascript file that you can reference in a <script> tag.
Related
I am facing problems with yii2 form get method. Here is my form:
<form class="search-form" method="get" action="<?php echo Yii::$app->urlManager->createAbsoluteUrl(['search/index']); ?>" id="search-form">
<div class="row search-box">
<div class="12u search-box-inner">
<input class="search-input" type="text" id="search-query" placeholder="Search" name="search_key" autocomplete="off" >
</div>
</div>
</form>
Here is my SearchController with actionIndex():
public function actionIndex()
{
$request = Yii::$app->request;
$search_key = $request->get('search_key');
return $this->render('index', ['search_key'=>$search_key]);
}
I want to submit to web/index?r=search/index&&search_key='something', but when I submit this form always returns web/index?searchkey='something'.
What need I do?
If you want use a parameter in your SearchController/Index
return $this->render('index', ['search_key'=>$search_key]);
You should declare in function declaration
public function actionIndex($search_key)
in this way you can use the value of $search_key passsed in render call
by your form submit action
<form class="search-form" method="get" action="
<?php echo Yii::$app->urlManager->createAbsoluteUrl(['search/index']); ?>"
id="search-form">
the resulting target is should be
web/index.php?r=search&id=search-form
and for use this get submit in your SearchController/Index
Your actionIndex function should be
public function actionIndex($id)
{
// $id contain the value you assigne in form action
// in you case you should obtain the value 'search-form'
.......
}
First of all, you need to configuring-web-servers correctly.
The url should not include 'web/index'.
Then change your form with ActiveForm
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
Your form content here.
<?php ActiveForm::end(); ?>
OR
Just change url.
<?php echo Url::to(['search/index']); ?>
I post data and redirect to another page with a userfunction and a simple form like the following example:
<?php
class user_test
{
public function getForm() {
$form = '
<form action="#" method="post">
<input type="text" name="test" value="test" />
<input type="submit" name="send" value="send" />
</form>
';
$postArray = t3lib_div::_POST();
if ( isset( $postArray['send'] ) ) {
header( 'Location: index.php?id=2' );
}
return $form;
}
}
this works fine if realURL is deactivated, but if I activate realURL this form leads me always to the homepage after submit :(.
The typoscript part looks as follows:
prefixLocalAnchors = all
simulateStaticDocuments = 0
baseURL = http://test.de
tx_realurl_enable = 1
realURL works for speaking URLs and other forms like powermail forms, but my own forms leads to homepage, hope you can help me :).
UPDATE
If I remove the hash from action attribute: 'action="#"' of this form it is working, is this solution passable?
Removing the action attribute from the form was working.
I am using parsley.js 2.0.2. for client side form validation.
Now I noticed on the Parsley Website that parsley 2.x has dynamic form validation.
I have a form on my page with parsley. It works correctly and does validate. Now on the same page I have a link that dynamically adds a form from an external file. Issue is now parsley.js won't validate the newly added form.
On the parsley website they have an example where one can use JavaScript to validate but I tried it and it does not work. Here is the snippet code of the example:
<script src="jquery.js"></script>
<script src="parsley.min.js"></script>
<form id="form">
...
</form>
<script type="text/javascript">
$('#form').parsley();
</script>
I am aware that the content in the DOM changed but is there a way that I can tell parsley to validate this newly added form or something that will trigger the validation process?
I will appreciate the help!
Thanks
Here is my form on the index.php page (This form does successfully validate):
<form action="server.php" method="post" name="main-form" id="myForm" data-parsley-validate>
<div>
<label for="njsform-name">Name</label>
<input name="name" type="text" id="njsform-name" placeholder="Mike" data-parsley-required="true" data-parsley-minlength="2">
</div>
<div>
<label for="njsform-email">Surname</label>
<input name="email" type="text" id="njsform-email" placeholder="Gates" data-parsley-required="true" parsley-minlength="2">
</div>
<div class="submitWrap">
<input class="submit" type="submit" value="Apply Now" />
</div>
Here is the link that gets the external content
<ul class="services-list">
<li><a class="s-option" href="views/form-short_term_loans.php">My Link</a></li>
</ul>
Here is the code I am using to dynamically change the content (does successfully retrieve external form and populates):
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('.services-list li a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-5)){
var toLoad = hash+'.html #form-section';
$('#form-section').load(toLoad)
}
});
$('.services-list li a').click(function(){
var toLoad = $(this).attr('href')+' #form-section';
$('#form-section').hide('fast',loadContent);
$('#load').remove();
$('#intro-section').append('<span id="load">Getting required form...</span>');
$('#load').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#form-section').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#form-section').show('normal',hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
The second form is just a duplicate but the form id is myForm2 and the name second-form
Add a call to
$('#xxxxxx').parsley();
After the load of the new form. With xxxxx the id of the new form inserted in the DOM
i want to add a dropdown menu to my layout .
i have a list of hotels . i want to change the hotel from a dropdown , i need to keep that drop down in my layout.phtml .
problem is hotel list is dynamic.
can i do this , is this possible in zend ,
here is my layout,phtml
i need to add a dropdown to <div class="floatright wid35 textaligncenter padtop5">
<html>
<head>
<?php echo $this->docType(); ?>
<?php echo $this->headTitle(); ?>
<?php echo $this->headScript(); ?>
<?php echo $this->headLink(); ?>
<?php echo $this->headStyle(); ?>
</head>
<body>
<?php echo $this->render('admin/header.phtml'); ?>
<div id="contentColumns" class="columns">
<div id="columnRight" class="column-right"></div>
<div id="columnLeft" class="column-right">
<div class="link-guide">
<div id="breadcrumbs" class="floatleft wid60">
<?php echo $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render(); ?>
</div>
<div class="floatright wid35 textaligncenter padtop5">
</div>
</div>
<div class="padding-box">
<?php echo $this->layout()->content; ?>
</div>
</div>
<div class="clear"></div>
</div>
<?php echo $this->render('admin/footer.phtml'); ?>
</body>
</html>
No. Layout files represent the static structure of a page like header and footer and everything you wrap around your dynamic content.
So this is not recommended. However you could solve that implementing the _init method of your main controller and extend any controller with that main controller:
class MainController extends Zend_Action_Controller{
function init(){
$this->view->foo = "Show everywhere!";
}
}
class IndexController extends MainController{
public function indexAction(){
$this->view->bar = "Show only on index/index";
}
}
Or you can use a plugin which would be the more elegant way
class MyPlugin extends Zend_Controller_Plugin_Abstract{
public function preDispatch(Zend_Controller_Request_Abstract $request){
$view = Zend_Controller_Action_HelperBroker::
getStaticHelper('viewRenderer')->view;
$view->foo = "bar";
}
}
and in your bootstrap process register that plugin
Zend_Controller_Front::getInstance()->registerPlugin(new MyPlugin);
You can create preDispatch function in your base controller class.
Then you get hotel list and send it to view:
$this->view->hotels = $hotels;
And in your layout you can parse it as you wish.
I see two issues here:
Using your hotel list in a dropdown suggests that you will have a form My_Form_Hotels extending Zend_Form which accepts the hotel list as a constructor parameter.
Where should you populate the hotel list, instantiate the form, and communicate all this to the view/layout for rendering?
Sounds like a front controller plugin would do the job. Since the layout resource functions as a plugin that runs late in postDispatch(), you need to run yours before that. dispatchLoopStartup() should be fine.
Something like this in library/My/Plugin/HotelForm.php:
class My_Plugin_HotelForm extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$hotelModel = new My_Model_Hotel();
$hotels = $hotelModel->getAllHotels();
$form = new My_Form_Hotels($hotels);
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$view = $viewRenderer->getView();
$view->hotelForm = $form;
}
}
Then in your layout, simply dump the form using $this->hotelForm.
To enable your plugin, add to application/configs/application.ini:
autoloaderNamespaces[] = 'My_"
resources.frontcontroller.plugins[] = "My_Plugin_HotelForm"
Can I use my normal (html) form in Zend Framework ? How can I do that & How can I call action in IndexController file?
of course you can ... just use
<form action="/index/action" methode="POST">
to access post arguments use
$this->getRequest()->getParam('argument')
thats no problem, put your form code inside the view script for the associated action. Maybe:
formAction()
{
// check if post request
if ($this->getRequest()->isPost()) {
// read global $_POST array
$data = $this->getRequest()->getPost();
}
}
the associated view ist than form.phtml
Yes, definitely.. You just have to remove the isValid call in your controller since it won't be performing any validation and also remove the post request check if it will not contain any form. It's like creating a common view with simple links in it.
Yes, I have a module called 'contact', and an action addcontactAction() in the ContactController.php.
So I can use :
/view/scripts/contacts/addcontact.phtml
<form action="" method="post" name="frm_addcontact" />
<input name="cn_fname" type="text" class="textbox" id="cn_fname"/>
<input type="submit" class="button" id="save" value="Save" />
</form>
when this form is submitted, it calls addcontactAction() in the controller.
$cn_fname = $_REQUEST['cn_fname'];
Just to know this is not a good practice to implement, but to solve such problem do the following:
in the view file when you define the form
<form action = "<?php echo $this->url(array('action'=>'ACTIONAME')); ?>" ...>
................
</form>
in the corresponding action name
if($this->_request->isPost()){
foreach ($_POST as $var => $value) {
echo "$var = $value<br>";
}