Laravel validation rule based on other fields value - laravel

I have this radio button
<input class="form-check-input" type="radio" name="discount" value="Yes">
<input class="form-check-input" type="radio" name="discount" value="No" checked>
and this hidden field
<input type="hidden" name="discount_valid" value="true">
by default this hidden field is true
Now when i'm trying to validate if discount_valid is true then it should submit the form this code is working but i want to add another condition, if discount is No then it should submit the form irrespective of whether discount value is true or false. If discount is Yes and discount_valid is false then form should not submit.
$validator = Validator::make($request->all(), [
'discount_valid'=>'in:true',
]);

By "submit the form", I have to assume you mean "processing the form request".
I've constructed a validation rule from your specifications:
if discount_valid is true then it should submit the form
if discount is No then it should submit the form irrespective of whether discount value is true or false
If discount is Yes and discount_valid is false then form should not submit.
$validator = Validator::make($request->all(), [
'discount_valid'=>'exclude_if:discount,No|in:true',
'discount'=>'in:No'
]);

Related

Post form in a button

In the admin dashboard you can make a post inactive if its active and vice versa. When you click on the button: 'make inactive', you send this form
<form method="POST" action="/admin/posts/inactivate/{{$post->id}}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<input type="number" id="active" name="active" value="1" hidden readonly>
<button type="submit">Make inactive</button>
</form>
I dd'd and made sure the correct 'active' value is given but when I click the button to update the value in the database it just does nothing(redirects back to the dashboard with no change). I have this in my controller
public function makeInactive(Post $post){
$attribute = request()->validate([
'active' => 'required',
]);
$post->update($attribute);
return redirect()->back();
}
I have the same sort code for updating user data and it works just fine. Am I missing something?
it works for me :) but I made the "active" column in the database as integer what about yours ?

Validation on checkbox where one one checkbox must be checked in laravel

I have the following Checkboxes now a want put validation in checkbox that one checkbox must be checked . But i dont't know how to do that.
CheckBox
<div class="form-group clearfix">
<label for="" class="col-sm-2 col-form-label">Arch (es) </label>
<div class="col-sm-10">
<label class="control-label" for="inputError" style="color: red"><i
id="arch_upper_error"></i></label>
<div class="demo-checkbox">
<input id="md_checkbox_1" name="arch_upper" value="41" class="chk-col-black"
type="checkbox">
<label for="md_checkbox_1">Upper</label>
<input id="md_checkbox_2" name="arch_lower" value="41" class="chk-col-black"
type="checkbox">
<label for="md_checkbox_2">Lower</label>
</div>
</div>
</div>
I tried this in laravel validation but i know its wrong because it required for both but i want at least one checkbox is checked.
public function rules()
{
return [
'arch_lower' => 'required',
'agarch_upper' => 'required',
,
];
}
I think you could use Laravel's required-without method:
The field under validation must be present and not empty only when any
of the other specified fields are not present.
Implementation would look something like this:
'arch_upper' => 'required_without: arch_lower',
If, by any chance, you have more checkboxes, you could use required-without-all:
The field under validation must be present and not empty only when all
of the other specified fields are not present.
Implementation:
'arch_upper' => 'required_without_all: arch_lower,another_checkbox',
Note: Code is not tested, if you encounter any errors, let me know.
You can read more on Laravel's official documentantion.

Custom validation message for my checkbox

I have below terms and condition checkbox created for a form
<input type="checkbox" name="checkbox" id="checkbox_id" class="checkbox required-entry" value="value">
I am getting "this is required field" as validation message but I want to change it to "Please check our terms and condition". Please suggest.
['validate-firstname', 'First name is required field.', function(v) {
return !Validation.get('IsEmpty').test(v);
}],
modify or add one rule in validation js like above code.

CFWheels: Form Helper Radio Button

I am using CFWheels for form validation. I have presenseOf() validation checks in both objects models. I have a form with a textbox and a set of radio buttons.
However If I submit the form empty, the validation for supervisor works but validation for the user checklist does not work. It gives the error;
"uchecklist" is not defined in the params variable.
On further observation, I notice that when the form is submitted, params struct has the "supervisor[name]" object but its empty, however it doesn't even have the "uchecklist[cstatus]" object. Moreover only when I select one of the radio buttons then the "uchecklist[cstatus]" object is submitted with that radio button's value.
I need to validate if at least one of the radio button is select, I guest this functionality is different from the empty text box validation.
Can someone show me how a radio button is validated using CFWheels form helpers.
Controller
public function t_validate()
{
title = "Home";
supervisor = model("supervisors");
uchecklist = model("user_checklist");
}
public function t_validate_complete()
{
title = "Home";
supervisor = model("supervisors").new(params.supervisor);
supervisor.save();
uchecklist = model("user_checklist").new(params.uchecklist);
uchecklist.save();
renderPage(action="t_validate");
}
View
<cfoutput>
<cfdump var="#params#">
#errorMessagesFor("supervisor")#
#startFormTag(action="t_validate_complete")#
<div>
<label for="">Supervisor:</label>
<input name="supervisor[name]" value="" />
</div>
<fieldset>
<input type="radio" name="uchecklist[cstatus]" value="1" />
<label for="profile-eyeColorId-2">Blue</label><br />
<input type="radio" name="uchecklist[cstatus]" value="2" />
<label for="profile-eyeColorId-1">Brown</label><br />
<input type="radio" name="uchecklist[cstatus]" value="3" />
<label for="profile-eyeColorId-3">Hazel</label><br />
</fieldset>
<div>
<input type="submit" value="Save Changes" />
</div>
#endFormTag()#
</cfoutput>
An unchecked radio button will submit no data to the server. This isn't a unique problem to ColdFusion or CFWheels.
To fix, provide a default value for the struct at the beginning of your controller action:
public function t_validate_complete()
{
// Provides an empty struct for the model to consume if none of the radio buttons are checked.
param name="params.uchecklist" type="struct" default="#StructNew()#";
title = "Home";
supervisor = model("supervisors").new(params.supervisor);
supervisor.save();
uchecklist = model("user_checklist").new(params.uchecklist);
uchecklist.save();
renderPage(action="t_validate");
}

Ajax POST - get a checkbox value for validation

I'd like to validate a form with ajax where there is a checkbox field.
I tried in this way but I can't get the value of checkbox field.
MY FORM
<form class="Form" action="?">
<input type="text" name="type" id="type" />
<input type="text" name="action" id="action" />
<input type="checkbox" name="chk" id="chk" value="1">
<input type="submit" value="INSERT" />
</form>
MY AJAX
$(".Form").submit(function( event ) {
event.preventDefault();
$.post("control.php", {
type: $("#type").val(),
action: $("#action").val(),
chk: $("#chk").val()
},
function(data){
$("#msg").html(data);
}
});
CONTROL.PHP
// CHECKBOX VALUE
if($_POST["chk"] == 1){
echo "THE VALUE IS 1";
exit;
}
How I could Do this? Thanks
EDIT
I tried pass to control page the chk with no success
$.post("control.php", {
type: $("#type").val(),
action: $("#action").val(),
chk: $("#chk").prop('checked')
},
and
$.post("control.php", {
type: $("#type").val(),
action: $("#action").val(),
$("#chk").prop('checked')
},
How can I define the name of my checkbox field in ajax post? thanks
This returns true if the checkbox is checked:
$("#chk").prop('checked')
And in PHP check it like this:
if(isset($_POST["chk"])) // true
For check boxes, php passing value if only check box selected. so you can check is that check box received to PHP page. if its received user has selected it or if user not selected, no check box name passing to PHP page.
so you can check
if(isset($_POST["chk"]))
...
if this returns true, that means user selected check box and if not user not selected...
One solution is you send a zero value with the same name in a hidden input
<input type="hidden" name="chk" value="0">
<input type="checkbox" name="chk" id="chk" value="1">
and if chk is checked the hidden input chk will be overriden in the $_POST

Resources