yii2 validate form element from model without using activeform - forms

I want to use the simple form tag & element like
<form name="formname" action="" method="post">
<input type="text" name="title" value="" />
</form>
I want to validate all fields on client side & server side using yii model.
Model validations can easily apply with activeform but i don't want to use activeform.
Any easy way to validate the form fields on client & server both sides?

Use beginForm() method. And try something like below.
use yii\helpers\Html;
<?php $form = Html::beginForm()([
'method' => 'post',
'name' => 'formname',
]); ?>
<?= Html::textarea->textarea(['rows' => 6, 'name'=>'title'])->label(false) ?>
<div class="form-group">
<?= Html::submitButton('POST', ['class' => 'btn btn-primary']) ?>
</div>
<?php Html::endForm() ?>
and then in your model
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// $model->addRule(['fieldname'], 'string', ['max' => 50]);
}

Related

Yii2 form checkbox template

I set the form parameters:
<?
$form = ActiveForm::begin([
'id' => 'activeForm',
'action' => 'javascript://',
]);
$checkboxTemplate = '<div class="checkbox">{labelTitle}{beginLabel}{input}<span class="slider round"></span>{endLabel}{error}{hint}</div>';
echo $form->field($aclForm, tbl_RbacActions::IS_DEVELOPMENT)
->checkbox([
'labelOptions' => ['class' => 'switch'],
'template' => $checkboxTemplate
]);
?>
As a result, it still turns a standard form with standart classes:
<form id="Index-form" class="row col-12 no-gutters" action="javascript://" method="post">
<input type="hidden" name="_csrf-frontend" value="Lo7lVHTJ9wcN5rdfjK-b7AgW8L4OHEaqI9IsVofZPOl3yKkFBJqAQz-i-y7H3-mdMUmY-H1RcMRBhkU01-F2oA==">
<div class="form-group field-aclform-is_development required">
<input type="hidden" name="AclForm[is_development]" value="0">
<label class="switch">
<input type="checkbox" id="aclform-is_development" name="AclForm[is_development]" value="1" template="{input}{beginLabel}{labelTitle}{endLabel}{error}"> Is Development</label>
<div class="help-block"></div>
</div>
</form>
Why is it adding the template as the input attribute?
You haven't provided the HTML template that you are trying to follow or integrate here in the checkbox, but regarding the template being added as the attribute of the input field is quite logical as you are passing it as the option of the checkbox rather than the field option.
You need to provide the template as an array to the third parameter option of the field see below
<?php
$form = ActiveForm::begin(
[
'id' => 'activeForm',
'action' => 'javascript://'
]
);
$checkboxTemplate = '<div class="checkbox">{labelTitle}{beginLabel}{input}<span class="slider round"></span>{endLabel}{error}{hint}</div>';
echo $form->field(
$aclForm,
tbl_RbacActions::IS_DEVELOPMENT,
[
'template' => $checkboxTemplate
]
)->checkbox(
[
'labelOptions' => ['class' => 'switch']
]
);

Selected an option in edit view cakephp 3.0

I am generating list within a form like the following code. But for updating view how can select the particular option according to the user. Suppose for this user's role is Customer. Then how can select this(Customer) option in CakePHP 3.
<?= $this->Form->create($user) ?>
<?php
echo $this->Form->input('fullname');
echo $this->Form->input('contact_no');
?>
<div class="form-group">
<?php echo $this->Form->select(
'role',
['Customer', 'Staff', 'User'],
['title' => 'Select user type']
); ?>
</div>
<?= $this->Form->end() ?>
Here is what I did : (According that I have 2 tables : Users which belongsTo Roles)
In my UsersController, i added this following line :
$this->set('roles', $this->Users->Roles->find('list')->order('name'));
Then, you'll need to edit your view from
<div class="form-group">
<?php echo $this->Form->select(
'role',
['Customer', 'Staff', 'User'],
['title' => 'Select user type']
); ?>
</div>
to
<div class="form-group">
<?= $this->Form->select(
'role_id',
$roles,
['id' => 'role-id', 'required' => true]
);
?>
</div>

Zend Form Rendering and Decorators (Use correctly with bootstrap)

The Bootstrap Example Code
http://getbootstrap.com/css/#forms
Copying a simple email input element from getbootstrap.com suggests we format the HTML in the following way:
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input id="exampleInputEmail1" class="form-control" type="email" placeholder="Email">
</div>
Above we have a <label> tag that closes straight after it's text content, "Email address".
I would now like to create the same form group using Zend Framework.
module/MyApp/src/MyModule/Form/MyForm.php
namespace MyModule\Form;
use Zend\Form\Form;
class MyModuleForm extends Form {
public function __construct($name = null)
{
$this->add(array(
'name' => 'email_address',
'type' => 'Email',
'options' => array(
'label' => 'Email address',
),
'attributes' => array(
'class' => 'form-control',
'placeholder' => 'Email'
)
));
The Zend Framework generated code
<div class="form-group">
<label>
<span>Email address</span>
<input class="form-control" type="email" placeholder="Email" id="exampleInputEmail1">
</label>
</div>
In the above HTML you can see that Zend has not closed the <label> tag. Instead the <label> ecapsulates its children.
How do I change the way the Zend rendering works?
I assume you are using ZF2 FormRow view helper to render your form element in your view script, e.g. $this->formRow($this->form->get('email_address'));
To render it differently you need to use the following view helpers
FormLabel
FormText
FormElementErrors
If for example you wanted to render as a definition list you would use something like
<dl class="zend_form">
<dt><?php echo $this->formLabel($this->form->get('email_address')); ?></dt>
<dd><?php echo $this->formText($this->form->get('email_address')); ?>
<?php echo $this->formElementErrors($this->form->get('email_address')); ?></dd>
</dl>
I hope this points you in the right direction

select menu default values not rendering in symfony

I am using Symfony 1.4, and am using embedded forms to place multiple similar forms into one form for a configuration page. I am having success showing the form, but the default values of the sfWidgetFormChoice widgets are not being rendered, i.e. the selected="selected" attribute is gone from the HTML.
Incidentally, the default values show up if I don't use embedded forms. The problem with avoiding embedded forms is that each form has identical inputs and therefore overwrites itself.
The action code is as such, some code omitted for brevity:
$serviceFormArray = array();
$this->fullForm = new ConfigForm();
foreach($this->serviceArray as $net => $service)
{
$this->partialForm = new ConfigForm();
foreach($service as $typeId => $val)
{
$typeObj = Doctrine::getTable('Type')->find($typeId);
$typeField = new sfWidgetFormChoice(array(
'default' => $val,
'choices' => array('1' => 'on', '0' => 'off'),
'label' => $typeObj->name)
);
$typeField->setDefault($val);
$serviceFormArray[$typeObj->name] = $typeField;
}
$netObj = Doctrine::getTable('Network')->find($net);
$this->partialForm->setWidgets($serviceFormArray);
$this->fullForm->embedForm($netObj->name,$this->partialForm);
}
and the template looks like this, some code omitted for brevity:
<div class="sectionBox">
<?php echo $fullForm->renderFormTag('/configure/submitconfig') ?>
<?php foreach ($fullForm->getVisibleFields() as $part => $field): ?>
<div class="settingsField">
<?php echo $field->renderLabel() ?>
<?php echo $field->render() ?>
<input type="hidden" name="plug" value="<?php echo $plugName; ?>"/>
</div>
<?php endforeach; ?>
<div id="submitConfig"><input type="submit" value="Save"/></div>
</form>
</div>
Try setting default value via $form->setDefault($name, $default).
$this->partialForm->setDefault($typeObj->name, $val);

How can I change the template of a form in sfDoctrineGuardPlugin?

How can I change the template of a form in sfDoctrineGuardPlugin?
That is, I need to change the HTML (class, id) of the input elements (username, password) of a login form provided by sfDoctrineGuardPlugin.
I've changed apps/app_name/modules/sfGuardAuth/templates/singinSuccess.php, but it then just echoes $form (I need to change contents of that part - $form):
<form action="<?php echo url_for('#sf_guard_signin') ?>" method="post">
<table>
<?php echo $form ?>
</table>
<input type="submit" class="go_button" value="ir" />
<?php echo __('Forgot your password?') ?>
</form>
(It really should be something like changing a _form.php => I can't find this, though :S)
Thank you all for any answers provided =)
In the sign in form class (forget it's name), there will be something like:
'username' => new sfWidgetFormInput(array(), array())
Modify that to:
'username' => new sfWidgetFormInput(array(), array('id' => 'jibbly', 'class' => 'wibbly'))