I have written form tag of YII2 specific as
<?php $form = ActiveForm::begin(['id' => 'builder/saveform','options' => ['method' => 'post']]) ?>
but when i run this, my external javascript is catching an error showing
Error: Syntax error, unrecognized expression: #builder/saveform
What is the error
To change default action add it as the first argument in this format ['<controller>/<action>']
<?php $form = ActiveForm::begin(['action' => ['builder/saveform'],'options' => ['method' => 'post']]) ?>
Example from the Yii2 guide
Also, keep in mind that the method defaults to post, so specifying that is unnecessary.
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'action' => Yii::$app->urlManager->createUrl(['site/signup'])]);
You can also use this snippet for custom form action and other attributes. createUrl method will not affect your url pattern.
for the sake now i have written the following code
<?php $form = ActiveForm::begin(['action' => 'index.php?r=builder/saveform','options' => ['method' => 'post']]) ?>
it is working but is it the proper way to write???
I was able to do it by adding the action into the options like so
<?php $form = ActiveForm::begin(['id' => 'contact-form', 'options' => ['method' => 'post', 'action' => 'site/add']]); ?>
For Yii2
$form = ActiveForm::begin([
'id' => 'login-form',
'options'=>['autocomplete'=>'off','method' => 'post', ],
'action' => '/frontend/web/user-management/auth/login',
'validateOnBlur'=>false,
'fieldConfig' => [
'template'=>"{input}\n{error}",
],
]);
Since there is still no accepted answer, the exact answer to your question is in the Error:
"unrecognized expression: '#builder/saveform' ". You have your 'id' HTML tag of the Form being assigned a value of 'builder/saveform', which contains "/" - an illegal character in terms of HTML4 Specification. I suppose Yii has a "validation" in place resulting in your error.
Now, I believe you are trying to specify the relative URL for the Form submission. For that, please refer to Michael St Clair's answer.
Related
I'm trying to make a simple search form (it will grow more complex soon so I'm using an ActiveForm here instead of simply passing GET parameters to the action method).
controller:
public function actionIndex()
{
$search_form = new UserSearchForm();
$search_form->load(Yii::$app->request->get(), $formName = '');
return $this->render('index', [
'search_form' => $search_form
]);
}
view:
<?php $form = ActiveForm::begin(['id' => 'search-form', 'method' => 'get']); ?>
<?= $form->field($search_form, 'q')->textInput(['name' => 'q']) ?>
<?= Html::submitButton('Search') ?>
<?php ActiveForm::end(); ?>
I'm using $formName = '' in controller and 'name' => 'q' in view to make the query string cleaner (simple q instead of UserSearchForm[q]).
Everything looks fine until first submit. I see a hidden q field in the form and after second submit the URL looks like /user?q=value1&q=value2, each submit adds another q to hidden fields. Is there a good way to get rid of those hidden fields? Or maybe the whole approach is wrong? I guess I'll need hidden fields there anyway (sorting, pagination etc).
You should simply set form action (if empty it will be the current url) :
<?php $form = ActiveForm::begin([
'id' => 'search-form',
'method' => 'get',
'action' => ['controller/index']
]); ?>
If you use for filtering the same action and controller as for displaying results, then
$form = ActiveForm::begin([
'id' => 'filter-form',
'method' => 'get',
'action' => Url::toRoute(\Yii::$app->request->getPathInfo())
]);
I use Yii 1.1.14
I use form to capture an email, not using Yii ajaxValidation/clientValidation
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'form-newsletter',
'action' => null,
'focus' => array($model, 'email'),
'htmlOptions' => array(
'role' => 'form',
),
)); ?>
I got this at js console
Uncaught TypeError: undefined is not a function
Which is refer to this
jQuery('#form-newsletter').yiiactiveform({'validateOnSubmit':true,'validateOnChange':true,'errorCssClass':'has-error has-feedback','successCssClass':'has-success has-feedback','inputContainer':'div.form-group','attributes':[{'id':'NewsletterForm_email','inputID':'NewsletterForm_email','errorID':'NewsletterForm_email_em_','model':'NewsletterForm','name':'email','enableAjaxValidation':true,'clientValidation':function(value, messages, attribute) {
I also check that jquery.yiiactiveform.js is present.
The question is, i dont want to use the default ajaxValidation/clientValidation, why is it loaded? and why it tells that yiiactiveform is undefined?
Thank you.
Check if you are including a newer version of jQuery than the one Yii includes (2.x for example) for the validation.
If you use other custom javascripts then must use in your layout code for example:
Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/bootstrap.min.js');
I can't figure out to generate Url from everywhere i want to, in zend 2
I get action and controller so i try this:
$this->url('myControllerName', array('action' => 'myActionName'));
But this return an object, i just want the full URL string of this route
Somebody can help me to find the proper way?
EDIT : according to Stoyan, maybe i made a mistake on my route. here is the part of my module.config
'router' => array (
'routes' => array (
'indexqvm' => array (
'type' => 'segment',
'options' => array (
'route' => '/Indexqvm[/:action][/:id_event]',
'constraints' => array (
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id_event' => '[0-9]+'
),
'defaults' => array (
'controller' => 'Qvm\Controller\Indexqvm',
'action' => 'index'
)
)
),
And my call :
echo $this->url('indexqvm', array('action' => 'list-index'));
the error :
Catchable fatal error: Object of class Zend\Mvc\Controller\Plugin\Url could not be converted to string
Use the echo before calling $this->url(...) (see bellow) and this will display the whole URL.
<?php echo $this->url('route-name', $urlParams, $urlOptions); ?>
Note that the first paramter of url() is the name of the route as specified in your [module]/config/module.config.php file.
See this for more information about ZF2's URL view helper.
EDIT in response to the question edit:
The above section is related to using the URL view helper.
If you want a URL in the controller then you need the URL controller plugin.
<?php $url = $this->url()->fromRoute('route-name', $params, $options); ?>
This is the reference to the ZF2 manual for this controller plugin.
Hope this helps :)
Stoyan
You can use this in the .phtml file
echo $this->url('HelloWorld/default', array('controller'=>'Index', 'action'=>'registration'));
Where HelloWorld/default is the routing and the remaining is the controller and its action and also you can send the others parameter adding just in array as key and value pair.
Controller :
Yii::import("xupload.models.XUploadForm"); //enciora
$photos = new XUploadForm;
$this->render('create', array(
'model' => $model,
'photos' => $photos
));
create: <?php echo
$this->renderPartial('_form',
array(
'model'=>$model,
'photos' => $photos
)); ?>
_form: <?php
$this->widget( 'xupload.XUpload', array(
'url' => Yii::app()->createUrl( "/encionmentDetail/upload"),
//our XUploadForm
'model' => $photos,
//We set this for the widget to be able to target our own form
'htmlOptions' => array('id'=>'encionment-detail-form'),
'attribute' => 'file',
'multiple' => true,
//Note that we are using a custom view for our widget
//Thats becase the default widget includes the 'form'
//which we don't want here
'formView' => 'application.views.encionmentDetail._form',
)
);
?>
ERROR: Undefined variable: model or Undefined variable: photos . this are the errors coming
while creating. if one model is passed then it shows properly. Please help
Well, the problem is with this line 'formView' => 'application.views.encionmentDetail._form'
. if i remove this line then no error. what should i do ?
This could well be due to the fact that your _form and your inner form for
'formView' => 'application.views.encionmentDetail._form',
are the same. Use a different one in the form view. Its kind of getting recursive.
in controller file :-
<?php
Yii::import("xupload.models.XUploadForm");
$photos = new XUploadForm;
$this->render('create', array(
'photos' => $photos,
));
?>
in create a file passing a bot model in render file :-
<?php echo $this->renderPartial('_form', array('model'=>$model,'photo'=>$photo))
Important note : must same a active from id and extensition html option id
snow_walker absolutely right, but I want to give details.Well, maybe it will help someone.
Such case occurs when xupload widget view contains <form>, so when it renders it becomes nested in CActiveForm.
One of the way to fix it:
Place CActiveForm widget in create.php
Copy everything from standard xupload form view (…\protected\extensions\xupload\views\form.php) to your model _form.php (…\protected\views\somemodel_form.php)
Delete from _form.php
<?php if ($this->showForm) echo CHtml::beginForm($this -> url, 'post', $this -> htmlOptions);?>
and
<?php if ($this->showForm) echo CHtml::endForm();?>
The other thing is that you cannot change the attribute's value in the widget configuration. It has to be 'file' in the other way the Internal Server Error (500) occurs.
How do i use the inputDefaults to add a common class to all the input elements in my form. also pls give a brief description of the inputDefaults.
isn't it:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'class' => 'someclass'
)
);
`
You should read the cookbook. theres a good example: http://book.cakephp.org/view/1639/options-inputDefaults
When you create a form you add inputdefaults key in options:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'div' => array('class' => 'someclass')
)
);
After browsing the source file i didn't find anything either. So the only way is to use it explicitly for every call to the input function.