Closed jampack closed 4 years ago
Hi. For now i don't think that we can do something for 5.4 other than fixing issues. Laravel 5.5 comes to our life and brings new changes to the validation classes by introducing custom validation rules. I guess a new updated version will follow. Can you please be more specific and define exactly what functionality you require?
see there are some fields that will be added on the page dynamically on run time by user, let say the names recent movies watched by a user so one field will be present by the name 'movie_name' but the user can mention more then one movies by adding more fields that will make the field like 'movie_name[array]' now laravel can validate this array of fields but the package doesnt seem to do that for the fields added on run time, more over if a field in laravel is defined as
'movie_name.* => "min:3|max:45"'
the package doesn't even validate the predefined field that is already present on the page by default
This functionality was added earlier by the original creator but it does not seem to work on laravel 5.4 with dev-master of this package
@akkhan20 I haven't tried wildcard validations yet. It will be great if you will write unit test for this case.
Same issues: #229 #221
the only way i have figured for now is to add a certain number of fields by default like this:
$rules = [ 'name' => 'required' ]; for ($i = 0; $i < 5; $i++) { $rules['emails.' . $i] = 'required|email'; } return $rules;
but then if you add a field on frontend the validator needs to be reinitialized to discover the new field or alternatively this example can be followed to do it more efficiently
doing like:
foreach($this->request->get('emails') as $key => $email) { $rules['emails.'.$key] = 'required|email'; } return $rules;
doesn't generate the JS rule at all because its dependent on formRequest to be sent from client first, i believe this has to be achieved using remote method somehow
In some previous post was mentioned something like:
'files.*.name' => 'required_with:files.*.file'
So, is not this rule working then?
ex.: https://laravel.com/docs/5.4/validation#validating-arrays
@marcelogarbin Array validation only works for input fields already present in the source at load time.
Array validation will not validate fields that added dynamically on the client.
@magnosis Ok, would you have any method of validating dynamic fields?
@marcelogarbin I have yet to find something more elegant than @akkhan20's method.
This works in my context, because the max size of array values is deterministic and small. It does not scale well at all. Hoping this feature will be added soon.
I'm using the Jquery Repeater package and apparently this code (297#issuecomment-490558632) seems to work with array validation.
Any news about this array validation?
Hey guys, i got a situation that seems like this issue of dynamic fields. I have a form, lets say its for adding a rental property, well, when the user selects the property type, i render several new fields depending on the property type, i issued an ajax request to the server and returned new rules to be applied to the validation, son i substituted the call of jsValidation::formRequest(FOrmRequest::class) with {!! JsValidation::make($newRules, $newMessages, [], "#idOfForm") !! } this substitution is made using jquery.
The bad news is that the new rules are not applied. WHat could i do to manage these???
Any help is appreciated.
THanks in advance
@rush4u can you provide a small example to replicate what you're doing?
@rush4u can you provide a small example to replicate what you're doing?
if have an action that respond to an ajax request that returns this view:
public function updateFormRequest(Request $request, TipoInmuebleRepository $tipoInmuebleRepository, ModelsHelper $modelsHelper)
{
$tipoInmuebleSelected = $tipoInmuebleRepository->find($request->id);
$rules = [
"nombre" => "required",
"descripcion" => "required",
"tipo_inmueble_id" => "required",
"operacion_id" => "required",
"new_property_direccion.direccion" => "required",
"new_property_direccion.numero" => "required|numeric",
"new_property_direccion.zip" => "required|exists:location_postal_codes,postal_code"
];
$messages = [
"tipo_inmueble_id.required" => "El tipo de inmueble1 es obligatorio",
"new_property_direccion.zip.exists" => "Ese código postal1 es incorrecto.",
"new_property_direccion.zip.required" => "El código postal1 es obligatorio.",
"new_property_direccion.numero.numeric" => "El número de123 la casa debe ser numérico",
"new_property_direccion.numero.required" => "El número de l123a casa es obligatorio",
"new_property_direccion.direccion.required" => "La direcció123n de la casa es obligatoria",
];
return view("frontend.panel-usuario.inmuebles.includes._form_request_update", compact("tipoInmuebleSelected", "rules", "messages"));
}
on the js side i call the action controller like this:
$.ajax($elem.data("update-inmueble-form-request-url"), {
data: { id: id },
type: "post",
beforeSend: function () {
$("#ajax-call-veil").addClass("show");
}
})
.done(function (result) {
$("#ajax-call-veil").removeClass("show");
$formRequestContainer.children().remove();
$formRequestContainer.append(result);
});
and this is the part of the view that gets substituted by the ajax call:
@push("actionJs")
<div id="inmueble-add-form-request-container">
{!! JsValidator::formRequest('App\Http\Requests\InmuebleRequest', "#addInmuebleForm") !!}
</div>
@endpush
the div of id = inmueble-add-form-request-container
the select element that triggers the ajax event is this part of blade snippet combined with laravel collective:
<div class="form-group @include(" common._has_error", ["fieldName"=> "tipo_inmueble_id"])">
{!! Form::label("tipo_inmueble_id", "Tipo de Inmueble") !!}
{!! Form::select("tipo_inmueble_id", $tiposInmueble, old("tipo_inmueble_id"), ["placeholder"=>"Seleccione el tipo de inmueble", "data-update-operaciones-field-url"=> route("public.panel_usuario.update_operaciones"), "data-update-inmueble-fields-url"=> route("public.panel_usuario.update_fields"), "data-update-inmueble-form-request-url"=> route("public.panel_usuario.update_form_request") , "id"=>"tipoInmuebleSelect", "class" => "form-control"]) !!}
@include("common._errors", ["fieldName" => "tipo_inmueble_id"])
</div>
Hope you get the big picture I guess i need to call validator update rules or something but dont know how can i do it.
Well, thanks in advance
Well, given that this package laravel js validation is made of jquery validate, i search inside the stackoverflow questions about the jquery plugin itself, and this is what i did:
<script>
$(function() {
$("#addInmuebleForm").removeData('validator');
});
</script>
{!! JsValidator::make($rules, $messages, [], "#addInmuebleForm") !!}
I unset the validator instance with jquery method removeData and then call my facade JsValidator with the new rules and messages.
Hope this helps some people.
Thanks for providing a solution @rush4u
As far as I'm aware jquery/jquery-validation will validate fields which are dynamically added. Some of what is discussed in this issue is a duplicate of https://github.com/proengsoft/laravel-jsvalidation/issues/297
I'm going to close this now and focus on providing a fix the above issue.
In laravel 5.4 the package is not validating dynamic fields, is there a work around or the package doesnt support that?