How to disable button when text input is empty in yii2? - php

I want to disable button when text input is empty in yii2?
can you help me?
this is my code
thanks
<div class="pemesanan-search thumbnail padding-baru col-sm-6 input">
<?php $form = ActiveForm::begin([
'action' => ['menubayar'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'NO_REKENING') ?>
<?= $form->field($model, 'NO_KARTU') ?>
<?= $form->field($model, 'NAMA') ?>
<div class="form-group">
<center><?= Html::submitButton('Cek Kartu Kredit', ['class' => 'btn btn-default']) ?></center>
</div>
<?php ActiveForm::end(); ?>
</div>

First disable the submit button by default, by using disabled attribute
in button's option.
After that you have to register a JS code using Yii2 RegisterJS.
Then trigger event using jQuery to remove disabled attribute after the input value is changed.
$( "# [id of the input that needs value]" ).change(function() {
$('input[type="submit"]').removeAttr('disabled');
});
By using above code, if you change the value of the input whith this id, it means that its not empty anymore and the disabled attribute will be removed.
Or also you can check if $("# [id of the input that needs value]").val(); is not empty remove the disabled attribute again with this code:
$('input[type="submit"]').removeAttr('disabled');
P.S: You may also define rules in its model and make that attribute as Required in the model rule, and let Yii handles the validation. You can learn more about that by reading This document.

Related

Yii2 - radioList choosing bindend to two different fields

I have two radioList bindend to two different fields:
<div class="form-group row">
<div class="col-lg-5">
<?= $form->field($model, 'isGdo')->radioList([0 => 'No', 1 => 'Yes'])->label("Are you a distributor ?"); ?>
</div>
<div class="col-lg-5">
<?= $form->field($model, 'isProducer')->radioList([0 => 'No', 1 => 'Yes'])->label("Are you a producer?"); ?>
</div>
</div>
Now my aim is to refactor this and create a radioList with 3 options in which I can set both these properties.
I know that I can switching to a single field with 3 values, but there are a lot of checks in the program, so keeping these two fields separate could be better for me.
Create A radio 3 radio buttons for showing with same name.
isGdo and isProducer
kept as hidden input field .
Write jquery code on above radio button and set that value on input box on basis of selection of radio button.
echo $form->field($model, 'isGdo')->hiddenInput(['value'=>''])->label(false);
echo $form->field($model,'isProducer')->hiddenInput(['value'=>''])->label(false);
$form->field($model, 'publicattribute')->radioList([1 => 'yes', 0 => 'No'])->label('');
<script>
$(document).ready(function(){
$("input[type='button']").click(function(){
var radioValue = $("input[name='']:checked").val();
if(radioValue){
alert("Your are a - " + radioValue);
**insert that value on your condition in the hidden input field**
}
});
});
</script>

How to add contextual classes in Yii 2 ActiveField / ActiveForm?

In Bootstrap 3 we can add a contextual class to a form-group container which will color that form field container in a specific color, ie. has-error will render it reddish:
<div class="form-group has-error">
<label for="field">Erroneous label</label>
<input type="text" class="form-control" placeholder="Erroneous input" id="field" />
<p class="help-block">Erroneous help-block</p>
</div>
Label, the input's text color and border and final p.help-block will all become red.
In Yii 2 we can use ActiveForm and ActiveField to do the same in one-liner:
<?= $form->field($model, 'field')
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint') ?>
And it generates roughly the same markup as above, in a form-group container.
I have gone through the docs and didn't find a way to add a has-error class to the form-group container.
$hintOptions
$inputOptions
$labelOptions
Do not work for this scenario.
Yii automatically adds has-error class in case of validation errors.
If you want to add any CSS-class to ActiveField container then you can use options property. For example:
<?= $form->field($model, 'field', [
'options' => [
'class' => 'form-group has-error',
],
])
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint');
?>

Laravel : How to send variable from one page to another in blade

I'm building a laravel application where from The first page I want to send a variable or say value to second page in a form ..How do I do it?
Let's say In my first page I have several buttons so when a user click on any one button, he will be redirected to another page where he has to submit a form.
In first page while he select a button, he automatically select a course_id which will also submit inside the second page form. But How do i send the course_id from the first page button?
<li> A </li>
<li> <a href="{{route('registration')}}" >B</a> </li>
When a user click on the button second page will appear ..Where I'm gonna submit a form where course_id will come from the first page means the <a> tag
Here is my form demo:
{!! Form::open(array('route' => 'postRegistration','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<div class="form-group">
<label class="sr-only" for="form-first-name">Name</label>
<input type="text" name="name" placeholder="Name..." class="form-first-name form-control" id="form-first-name">
</div>
<div class="form-group">
<label class="sr-only" for="form-email">Email</label>
<input type="text" name="email" placeholder="Email..." class="form-email form-control" id="form-email">
</div>
<div class="form-group">
<label class="sr-only" for="form-last-name">Address</label>
<textarea rows="4" style="height:100px;" name="address" placeholder="Address..." class="form-last-name form-control" id="form-last-name"></textarea>
</div>
<button type="submit" class="btn">Submit</button>
{!! Form::close() !!}
In my database I have to submit the above field as well as the course_id from the database.
How do I do it in Laravel?
Any suggestion or solution please?
You can send the variable as route parameter. To do this change your <a> tags like this.
<li> A </li>
<li> B </li>
Then you need to allow your registration route to take parameter. Suppose your route is like this
Route::get('/registration', [
'as' => 'registration', 'uses' => 'SomeController#method'
]);
Change it to
Route::get('/registration/{course_id}', [
'as' => 'registration', 'uses' => 'SomeController#method'
]);
Now you have to add this parameter into your SomeController#method
public method($course_id){ ... }
Now pass this $course_id to your form view. To submit this variable with your other fields you can add this as hidden input field.
<div class="form-group">
<input type="hidden" name="course_id" value="{{ $course_id }}">
</div>
So you want to create page with multiple course links and each link redirects to registration page with the course id to be used in the registration form.
Your a href link should look like this
{!! link_to_route('registration',
$title = 'Course 1', $parameters = ['1'], $attributes =
['class'=>'btn btn-success']) !!}
Routes file web.php
Route::get('/registration/{course_id}','
CourseRegistration#showForm')->name('registration');
CourseController CourseController
public function showForm($course_id){
return view('registration')->with('courseid',$course_id);
}
Now you can access the course id with $courseid in view. If you want to pass it in form create a hidden or input tag with the data.
According to me, you're looking for a type of web app in which when a user submits a form the page opens up with the registered user details.
This can be done in Laravel like this:
routes.php
// For submitting form on this route
Route::post('users/register', 'UsersController#create')->name('users.register');
// For showing user's profile via user's id
Route::get('users/{id}/profile', 'UsersController#show')->name('users.profile');
UsersController.php
class UsersController {
public function create() {
$inputs = request()->all();
// Use Eloquent to save user info to DB and fetch insert id from DB...
return redirect()->route('users.profile', array('id' => $insert_id));
// Will open registered user profile page
}
public function show($id) {
$user = User::find($id);
if(!$user) {
abort('404');
}
return view('users/profile', compact('user'));
// At the users/profile.php page you can use $user variable to populate user's info
}
}
Usage of route method with passing data with route in Blade
{{ route('users.profile', array('id' => $user->id)) }}
For more help, see this >>
Hope this helps you!

laravel write to mysql database on button click

I am currently able to write data to mysql when one of my pages loads. Here is the code for this in my controller.
public function manuelSignUP()
{
DB :: table("users") -> insertGetId
(
array("firstname" => "John", "lastname"=> "John",
"passwordHash" => "password", "userslevelID" => 2)
);
DB :: table("userlevel") -> insertGetID
(
array("userlevelID" => $userlevelID, "name" => $name)
);
return view("pages.manualsignup");
}
So I would like to call this function through my blade file on a button click, but I have been struggling to do so. Here is my blade file with the button.
<!DOCTYPE html>
<html>
<head> </head>
<body> This should </body>
<br><br><br><br>
<form method="post">
<button type="button"> submit </button>
</form>
</html>
Based on google searching I know that I can use ajax to help me with this problem, but I believe there is way to do it by just using html post methods. I am trying to do it the second way, without ajax.
If you're not using ajax you need to specify where your form should go with the action attribute, it's not sufficient to do it with the method alone.
view
<form action="{{ route('signup') }}" method="post">
<button type="submit"> submit </button>
</form>
routes.php
Route::post('/signup', [
'as' => 'signup',
'uses' => 'YourController#manuelSignUP',
]);
Also, normally you should use a form to input data into, not hard code it in.
I'd recommend you to use RESTful controllers and test it without using ajax first. Start from learning RESTful controllers and Routes:
https://laravel.com/docs/5.1/controllers#restful-resource-controllers
https://laravel.com/docs/5.1/routing
All you need is to create RESTful controller and use store method to store your your data in DB. For example:
{!! Form::model($data, array('action' => 'MyController#store') !!}
{!! Form::text('name', null, array('required', 'class'=>'form-control', 'placeholder'=>'Name')) !!}
{!! Form::submit('Create and store in DB', array('class'=>'btn btn-success')) !!}
When you'll test everything, just use AJAX if you don't want to reload page every time you store the data in DB.

How to Customizing/add a class to the ActiveForm items with Material Bootstrap design classes on Yii2

I'm trying to add a class for a TextField like this one:
<div class="form-group-lg">
<?= $form->field($model, 'email'); ?>
</div>
Output:
And that would be something like this?(the label will be inside the TextField).i mean, can we use it like below codes?
Example:
I need something like this:
<div class="form-group label-floating">
<label class="control-label" for="focusedInput1">Write something to make the label float</label>
<input class="form-control" id="focusedInput1" type="text">
</div>
http://fezvrasta.github.io/bootstrap-material-design/bootstrap-elements.html
Forms section-first one.
So, the question is,
How can i add a class to this item with above codes on Yii2 ActiveForm?
You need to use fieldConfig Propertie in ActiveForm.
Like as
<?php $form = ActiveForm::begin([
'id' => 'active-form',
'fieldConfig' => [
'template' => "<div class=\"form-group label-floating\">{label}{input}{error}</div>",
],
]); ?>
For more info read Docs

Categories