I am using symfony2. Actually i have created form using form type. but when i render the form, following error occurred
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\Component\Form\FormView
Here is my code
Controller
$profile = new profile();
$myForm = $this->createForm(new ProfileFormType(), $profile);
return $this->render('ProfileBundle:Profle:profile.html.twig', array(
'form' => $myForm,
'vendor' => $vendor,
'productId' => $productId
));
profile.twig.html
<form action="{{ path('profile_new') }}" method="POST" {{ form_enctype(form) }} id="frmProfile">
<div class="box-body col-sm-6">
<div class="form-group">
<label class="control-label">First Name: </label>
{{ form_widget(form.firstName, { 'attr': { 'placeholder': 'Title', 'class': 'form-control'}}) }}
<div class="serverError">{{ form_errors(form.firstName) }}</div>
</div>
</div>
<div class="box-body col-sm-6">
<div class="form-group">
<label class="control-label">Last Name: </label>
{{ form_widget(form.lastName, { 'attr': { 'placeholder': 'Title', 'class': 'form-control'}}) }}
<div class="serverError">{{ form_errors(form.lastName) }}</div>
</div>
</div>
<div class="box-body col-sm-6">
<div class="form-group">
<label class="control-label">User Name: </label>
{{ form_widget(form.username, { 'attr': { 'placeholder': 'Title', 'class': 'form-control'}}) }}
<div class="serverError">{{ form_errors(form.username) }}</div>
</div>
</div>
</form>
What am i doing wrong?
Replace 'form' => $myForm with 'form' => $myForm->createView(). Function createView() will create view of form object.
return $this->render('ProfileBundle:Profle:profile.html.twig', array(
'form' => $myForm->createView(), // << Add "->createView()"
'vendor' => $vendor,
'productId' => $productId
));
Related
I have a User Entity and I collect all the data in a form called Register. But once after registering, I want to edit some of the fields of a user by a form. But some fields are avoided being updated (email) and Password field is optional. I tried rendering the same register form but $form->handleRequest($request) throws an error saying:
Expected argument of type "string", "NULL" given at property path
"password".
I want to know if there is a way to do this?
I tried $form->submit($request->request->get($form->getName()),false);
instead of $form->handleRequest($request); but it didn't work.
// This is my controller.
/**
* #Route("/update/{id}", name="update")
* #param $id
* #param Request $request
* #param UserPasswordEncoderInterface $passwordEncoder
* #param UserRepository $userRepository
* #return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function updateUser($id,Request $request, UserPasswordEncoderInterface $passwordEncoder, UserRepository $userRepository){
$user = $userRepository->find($id);
$form = $this->createFormBuilder($user)
->add('email',EmailType::class,[
'label'=>'Email',
'required' => false,
'attr'=>['placeholder'=>"Email"]
])
->add('password',RepeatedType::class,[
'type' => PasswordType::class,
'invalid_message' => 'The password fields must match.',
'required' => false,
'options' => ['attr' => ['class' => 'password-field']],
'first_options' => ['label' => 'Password','attr'=>['placeholder'=>"Password"]],
'second_options' => ['label' => 'Confirm Password','attr'=>['placeholder'=>"Confirm Password"]],
])
->add('firstName',TextType::class,['label'=>'First Name', 'attr'=>['placeholder'=>"First Name"]])
->add('lastName',TextType::class,['label'=>'Last Name','attr'=>['placeholder'=>"Last Name"]])
->add('address',TextareaType::class,['required' => false,'label'=>'Address','attr'=>['placeholder'=>"Address"]])
->add('idNumber',TextType::class,['label'=>'NIC Number','attr'=>['placeholder'=>"NIC Number"]])
->add('phoneNumber',TelType::class,['label'=>'Phone Number','attr'=>['placeholder'=>"Phone Number"]])
->add('image',FileType::class,['label'=>'Photo','required'=>false,'attr'=>['hidden'=>"hidden", 'accept'=>"image/jpeg, image/png"]])
->add('save',SubmitType::class,[
'label'=>'Register',
'attr' => [
'class'=>"btn btn-outline-success float-right"
]
])
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
if($user->getPassword() !=""){
$user->setPassword($passwordEncoder->encodePassword($user,$user->getPassword()));
}
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirectToRoute('home');
}
return $this->render('register/update.html.twig', [
'form'=>$form->createView(),
]);
}
// And this is my TWIG file.
{% extends 'base.html.twig' %}
{% block title %}Edit Info{% endblock %}
{% block refHome %}{{ path('main') }}{% endblock %}
{% block body %}
<div class="container" style=" margin-left: auto;margin-right: auto;margin-top: 1cm;" >
<div class="row">
{# <div class="col-lg-2"></div>#}
<div class="col-lg-10">
<div class="card" style="width: 50rem;text-align: center;">
<div class="card-body">
<h3 class="card-title float-left">Edit Info</h3>
<div style="margin-top: 2cm;">
<p class="card-text">
<form name="register" id="register_form" method="post" enctype="multipart/form-data">
<div id="register">
<div class="container">
<div class="row">
<div class="col-sm-5">
{{ form_errors(form.firstName) }}
{{ form_widget(form.firstName) }}
</div>
<div class="col-sm-2"></div>
<div class="col-sm-5">
{{ form_errors(form.lastName) }}
{{ form_widget(form.lastName) }}
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="row">
<div class="col-sm-6">
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
</div>
<div class="col-sm-1"></div>
<div class="col-sm-5">
{{ form_errors(form.idNumber) }}
<input type="text" id="register_idNumber" placeholder="NIC Number" onkeypress="isInputNumber(event)" pattern="^[0-9]{9}[a-zA-Z]$" value="{{ form.idNumber.vars.value }}" name="register[idNumber]" required="required" class="form-control" " /> <div class="input-group-append">
</div>
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="row">
<div class="col-sm-5">
<div style="margin-top: 0.5cm;"></div>
{{ form_errors(form.phoneNumber) }}
<input type="tel" id="register_phoneNumber" value="{{ form.phoneNumber.vars.value }}" name="register[phoneNumber]" onkeypress="isPhone(event)" required="required" placeholder="Phone Number" class="form-control" pattern="^[0-9]{10}$" />
</div>
<div class="col-sm-2"></div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="row" >
<div class="col-sm-7">
{{ form_errors(form.address) }}
{{ form_widget(form.address) }}
</div>
<div class="col-sm-1" hidden="hidden">
{{ form_widget(form.image) }}
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="form-group row">
<div class="col-sm-7">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="defaultUnchecked" style="float: left">
<label class="custom-control-label" for="defaultUnchecked">Change Password</label>
</div>
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div id="register_password" >
<div class="form-group row">
{% for passwordField in form.password %}
<div class="col-sm-5">
{{ form_errors(passwordField) }}
{{ form_widget(passwordField) }}
</div>
<div class="col-sm-2"></div>
{% endfor %}
</div>
<div style="margin-top: 0.5cm;"></div>
</div>
</div>
{{ form_widget(form.save,{'label':"Update"}) }}
{{ form_widget(form._token) }}
{{ form_errors(form) }}
</form>
</p>
</div>
</div>
</div>
</div>
<div class="col-lg-1"></div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script>
const checkBoxPwd = document.getElementById('defaultUnchecked');
const pwdDiv = document.getElementById('register_password');
pwdDiv.style.visibility = "hidden";
checkBoxPwd.addEventListener("change",function () {
if (checkBoxPwd.checked == true){
pwdDiv.style.visibility = "visible";
}else{
pwdDiv.style.visibility = "hidden";
}
});
</script>
<script>
function isInputNumber(eve) {
var ch = String.fromCharCode(eve.which);
if (document.getElementById('register_idNumber').value.length < 9){
if (!(/[0-9]/.test(ch))){
eve.preventDefault();
}
}else {
if (!(/[a-zA-Z]/.test(ch))){
eve.preventDefault();
}
}
if (document.getElementById('register_idNumber').value.length > 9){
eve.preventDefault();
}
}
function isPhone(eve) {
var ch = String.fromCharCode(eve.which);
if (!(/[0-9]/.test(ch))){
eve.preventDefault();
}
if (document.getElementById('register_phoneNumber').value.length > 9){
eve.preventDefault();
}
}
</script>
{% endblock %}
I am having a weird problem. I added an upload image field to a form. Every thing work fine when i use this code in the twig file
{{ form_start(form) }}
<button>d</button>
{{ form_end(form) }}
But when ever i want to use form_widget to costumize the look of the form this error appear when ever i try to sumit the form
FatalErrorException
Error: Call to a member function getFileName() on string.
This is the View that cause the problem
{% extends 'BridgeTravelBundle:Admin:layout.html.twig' %}
{% block body %}
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<form method="POST" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Nom</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
{{ form_widget(form.name, { 'attr': {'class': 'form-control', 'placeholder': 'Prenom' } }) }}
{{ form_errors(form.name) }}
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Icon</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
{{ form_widget(form.icon, { 'attr': {'class': 'form-control', 'placeholder': 'Prenom' } }) }}
{{ form_errors(form.icon) }}
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Icon</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
{{ form_widget(form.file) }}
{{ form_errors(form.file) }}
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button class="btn btn-success">Valider</button>
</div>
</div>
</form>
</div>
</div>
{% endblock body %}
This is the Action that control the form.
public function addCategoryAction(Request $request){
$category=new Category();
$form=$this->createForm(CategoryType::class,$category);
$form->handleRequest($request);
if ($form->isSubmitted())
{
$image= $form['file']->getData();
$em=$this->getDoctrine()->getManager();
$req = $request->request->get('Bridge_TravelBundle_Category');
$category->setName($req['name']);
$category->setIcon($req['icon']);
$name = $req['name'];
try {
if(!is_dir("CategoriesPictures")){
mkdir("CategoriesPictures");
}
move_uploaded_file($image,"CategoriesPictures/".$image->getFileName());
rename("CategoriesPictures/".$image->getFileName() , "CategoriesPictures/".$name.".jpg");
}
catch (IOExceptionInterface $e) {
echo "Erreur Profil existant ou erreur upload image ".$e->getPath();
}
$em->persist($category);
$em->flush();
return $this->redirectToRoute('admin_categories');
}
return $this->render("BridgeTravelBundle:Admin:addcategory.html.twig",array('form' => $form->createView(),));
}
This the CategoryType.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CategoryType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name')
->add('icon')
->add('file',FileType::class, array(
'multiple' => false,
'attr' => array(
'accept' => 'image/*',
)
)
) ;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Bridge\TravelBundle\Entity\Category'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'Bridge_TravelBundle_Category';
}
}
This attribute enctype='multipart/form-data' is missing from your form tag.
You should just you form_start & form_end instead writing form tag by yourself.
You can also put custom attributes with form_start function like this
{{ form_start(form, { 'attr': {'class': 'foo', 'id': 'bar' } }) }}
Also, form_end is very important because it also print out form_rest and any missing field.
<form method="POST" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">
will become
{{ form_start(form, { 'attr': { 'id': 'demo-form2', 'data-parsley-validate': null, 'class': 'form-horizontal form-label-left' } }) }}
and
</form>
will become
{{ form_end(form) }}
I'm trying to update the fields in the database, but I couldn't
here is my routes :
Route::get('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
here the controller:
public function postOrder()
{
$this->orderForm->validate(Input::all());
$order = $this->orders->getNew([
'link' => Input::post('link'),
'size' => Input::post('size'),
'color' => Input::post('color')
]);
$this->orders->save($order);
return Redirect::back()->withMessage('Order has been updated');
}
here is the blade:
{{ Form::open() }}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
so each time I try to update the order I got error "MethodNotAllowedHttpException ", I tried a lot of methods but I'm lost. I'm still beginner in php and this problem drive me crazy, so I'll be glad if you can help guys.
thanks
*** I've updated the code
So you're posting to the route, /orders. Therefor you need a HTTP POST request. You're now assigning a GET request to the /orders route.
You need to change your code to:
Route::post('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
Also you need to add a CSRF Token, this can be done through adding {!! csrf_field() !!} in your blade (inside your Form open and close).
{{ Form::open() }}
{!! csrf_field() !!}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Hope this works!
You must specify the method in the Form::open method.
{{ Form::open(array('method' => 'post')) }}
Just added this in the repository:
public function updateOrder($id, array $data)
{
$orders = $this->getById($id);
if (!empty($data['title'])) {
$orders->title = $data['title'];
}
if (!empty($data['link'])) {
$orders->link = $data['link'];
}
(AND SO ON)
$orders->save();
and in the controller:
public function postOrder($id)
{
$this->orders->updateOrder($id, Input::all());
return Redirect::back()->withMessage('Updated');
}
and that's it
I'm trying to add a new match into the database but if I purposely don't enter anything into the form, no errors are displayed. I have based this on my registration form which works perfectly. If I correctly fill out the form all data goes into the database correctly.
MatchController.php
public function postCreate()
{
$validator = Validator::make(Input::all(), Match::$rules);
if ($validator->passes())
{
$datetime = Input::get('year')."-".Input::get('month')."-".Input::get('day')." ".Input::get('time').":00";
$match = new Match;
$match->type = Input::get('match_type');
$match->associate = Input::get('associate');
$match->date = $datetime;
$match->location = Input::get('location');
$match->save();
$matchp1 = new Matchplayer;
$matchp1->user_id = Input::get('id');
$matchp1->match_id = $match->id;
$matchp1->team = 1;
$matchp1->save();
$matchp2 = new Matchplayer;
$matchp2->user_id = Input::get('opponent');
$matchp2->match_id = $match->id;
$matchp2->team = 2;
$matchp2->save();
return Redirect::to('members/matches/create')->with('message', 'Match created!');
}else{
return Redirect::to('members/matches/create')->withErrors($validator)->withInput();
}
}
Match Model (Match.php)
class Match extends Eloquent{
protected $table = 'matches';
public static $rules = array(
'match_type'=>'required',
'associate'=>'alpha|min:2'
'location'=>'required|alpha|min:2',
'team_mate2'=>'required_if:match_type,doubles',
'opponent2'=>'required_with:team_mate2'
);
}
Match Creation View (matchCreate.php)
<div class="row clearfix">
<!-- Error Message Box -->
#if(Session::has('message'))
<div class="col-md-8 col-md-offset-2">
<div class="alert alert-info">{{ Session::get('message') }}</div>
</div>
#endif
<div class="col-md-9">
<!-- Top Menu BEGIN -->
<ul class="nav nav-pills nav-justified">
<li>{{ HTML::link('members/logout', 'Profiles') }}</li>
<li>{{ HTML::link('members/logout', 'Leagues') }}</li>
<li class="active">{{ HTML::link('members/logout', 'Add Match') }}</li>
<li>{{ HTML::link('members/logout', 'Results & Fixtures') }}</li>
<li>{{ HTML::link('members/logout', 'Matchboard') }}</li>
</ul>
<!-- Top Menu END -->
<div class="page-header">
<h3>Create a Match</h3>
</div>
<div class="col-sm-6">
{{ Form::open(array('url'=>'members/matches/create', 'action' => 'post', 'class'=>'form-horizontal')) }}
{{ form::hidden('id', Auth::user()->id) }}
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Match Type</label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('match_type', '<span class="help-block">:message</span>') }}
{{ Form::select('match_type', array('singles' => 'Singles', 'doubles' => 'Doubles'), 'singles', array('id' => 'match_type', 'class' => 'form-control input')) }}
</div>
</div>
<div class="form-group">
<label for="gender" class="col-sm-5 control-label col-sm-pad">League Associate</label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('associate', '<span class="help-block">:message</span>') }}
{{ Form::text('associate', Input::old('associate'), array('class' => 'form-control input')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Team Mate #1<br><small></small></label>
<div class="col-sm-7 col-sm-pad">
<p class="form-control-static">{{ Auth::user()->first_name." ".Auth::user()->last_name }}</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Opponent #1</label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('opponent', '<span class="help-block">:message</span>') }}
{{ Form::select('opponent', $users, 'key', array('class' => 'form-control input')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Team Mate #2<br><small>(Doubles Only)</small></label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('team_mate2', '<span class="help-block">:message</span>') }}
{{ Form::select('team_mate2', $users, 'key', array('class' => 'form-control input')); }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Opponent #2<br><small>(Doubles Only)</small></label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('opponent2', '<span class="help-block">:message</span>') }}
{{ Form::select('opponent2', $users, 'key', array('class' => 'form-control input')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Date</label>
{{ $errors->first('day', '<span class="help-block">:message</span>') }}
<div class="col-lg-2 col-sm-pad">
{{ Form::selectRange('day', 1, 31, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
</div>
<div class="col-lg-2 col-sm-pad">
{{ Form::selectRange('month', 1, 12, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
</div>
<div class="col-lg-3 col-sm-pad">
{{ Form::selectRange('year', 2014, 2015, 'a', array('class' => 'form-control input-sm input dob-form-control')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Time</label>
<div class="col-lg-3 col-sm-pad">
{{ $errors->first('time', '<span class="help-block">:message</span>') }}
{{ Form::select('time', $times, 'key', array('class' => 'form-control input input-sm dob-form-control')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Location</label>
<div class="col-sm-7 col-sm-pad">
{{ Form::text('location', Input::old('location'), array('class' => 'form-control input')) }}
</div>
</div>
{{ Form::submit('Create', array('class'=>'btn btn-success btn-block')) }}
{{ Form::token() . Form::close() }}
</div>
<div class="col-sm-6">
</div>
</div>
#include('includes.sidemenu')
</div>
routes.php
Route::post('members/matches/create', 'MatchController#postCreate');
You're creating your Validator object incorrectly. The make() method has four parameters:
/**
* Create a new Validator instance.
*
* #param array $data
* #param array $rules
* #param array $messages
* #return \Illuminate\Validation\Validator
*/
public function make(array $data, array $rules, array $messages = array(), array $customAttributes = array()) { ... }
You're passing each of your validation rules as a separate array, rather than together as one array. Subsequently, you aren't passing any of your input data, either, so it has no actual data to validate against.
My required_if validation does not work. No error is output when "doubles" is selected but "team_mate2 and opponent2" are not selected. I need the form to require a second team mate and opponent when doubles is selected as the match type.
Match.php (Model)
<?php
class Match extends Eloquent{
protected $table = 'matches';
public static $rules = array(
'match_type'=>'required',
'associate'=>'alpha|min:2',
'location'=>'required|alpha|min:2',
'opponent'=>'numeric',
'team_mate2'=>'required_if:match_type,doubles',
'opponent2'=>'required_with:team_mate2'
);
public static $messages = array(
'opponent.numeric'=>'You must select a player.'
);
}
MatchController.php
public function postCreate()
{
$validator = Validator::make(Input::all(), Match::$rules, Match::$messages);
if ($validator->passes())
{
$datetime = Input::get('year')."-".Input::get('month')."-".Input::get('day')." ".Input::get('time').":00";
$match = new Match;
$match->type = Input::get('match_type');
$match->associate = Input::get('associate');
$match->date = $datetime;
$match->location = Input::get('location');
$match->save();
$matchp1 = new Matchplayer;
$matchp1->user_id = Input::get('id');
$matchp1->match_id = $match->id;
$matchp1->team = 1;
$matchp1->save();
$matchp2 = new Matchplayer;
$matchp2->user_id = Input::get('opponent');
$matchp2->match_id = $match->id;
$matchp2->team = 2;
$matchp2->save();
return Redirect::to('members/matches/create')->with('message', 'Match created!');
}else{
return Redirect::to('members/matches/create')->withErrors($validator)->withInput();
}
}
Match Create View
{{ Form::open(array('url'=>'members/matches/create', 'action' => 'post', 'class'=>'form-horizontal')) }}
{{ form::hidden('id', Auth::user()->id) }}
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Match Type</label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('match_type', '<span class="help-block">:message</span>') }}
{{ Form::select('match_type', array('singles' => 'Singles', 'doubles' => 'Doubles'), 'singles', array('id' => 'match_type', 'class' => 'form-control input')) }}
</div>
</div>
<div class="form-group">
<label for="gender" class="col-sm-5 control-label col-sm-pad">League Associate</label>
<div class="col-sm-7 col-sm-pad">
{{ $errors->first('associate', '<span class="help-block">:message</span>') }}
{{ Form::text('associate', Input::old('associate'), array('class' => 'form-control input')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Team Mate #1<br><small></small></label>
<div class="col-sm-7 col-sm-pad">
<p class="form-control-static">{{ Auth::user()->first_name." ".Auth::user()->last_name }}</p>
</div>
</div>
<div class="form-group{{ $errors->first('opponent', ' has-error') }}">
<label class="col-sm-5 control-label col-sm-pad">Opponent #1</label>
<div class="col-sm-7 col-sm-pad">
{{ Form::select('opponent', $users, 'key', array('class' => 'form-control input')) }}
{{ $errors->first('opponent', '<span class="help-block">:message</span>') }}
</div>
</div>
<div class="form-group{{ $errors->first('team_mate2', ' has-error') }}">
<label class="col-sm-5 control-label col-sm-pad">Team Mate #2<br><small>(Doubles Only)</small></label>
<div class="col-sm-7 col-sm-pad">
{{ Form::select('team_mate2', $users, 'key', array('class' => 'form-control input')); }}
{{ $errors->first('team_mate2', '<span class="help-block">:message</span>') }}
</div>
</div>
<div class="form-group{{ $errors->first('opponent2', ' has-error') }}">
<label class="col-sm-5 control-label col-sm-pad">Opponent #2<br><small>(Doubles Only)</small></label>
<div class="col-sm-7 col-sm-pad">
{{ Form::select('opponent2', $users, 'key', array('class' => 'form-control input')) }}
{{ $errors->first('opponent2', '<span class="help-block">:message</span>') }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Date</label>
{{ $errors->first('day', '<span class="help-block">:message</span>') }}
<div class="col-lg-2 col-sm-pad">
{{ Form::selectRange('day', 1, 31, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
</div>
<div class="col-lg-2 col-sm-pad">
{{ Form::selectRange('month', 1, 12, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
</div>
<div class="col-lg-3 col-sm-pad">
{{ Form::selectRange('year', 2014, 2015, 'a', array('class' => 'form-control input-sm input dob-form-control')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label col-sm-pad">Time</label>
<div class="col-lg-3 col-sm-pad">
{{ Form::select('time', $times, 'key', array('class' => 'form-control input input-sm dob-form-control')) }}
</div>
</div>
<div class="form-group{{ $errors->first('location', ' has-error') }}">
<label class="col-sm-5 control-label col-sm-pad">Location</label>
<div class="col-sm-7 col-sm-pad">
{{ Form::text('location', Input::old('location'), array('class' => 'form-control input')) }}
{{ $errors->first('location', '<span class="help-block has-error">:message</span>') }}
</div>
</div>
{{ Form::submit('Create', array('class'=>'btn btn-success btn-block')) }}
{{ Form::token() . Form::close() }}
When I don't use a model like so:
$validator = Validator::make(
array('match_type' => Input::get('match_type')),
array('opponent' => 'required_if:match_type,doubles')
);
This seems to work. I still am unsure on why my method does not work, does the match_type value not pass over to required_if when in a Model?