Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, March 23, 2015

Laravel validation trouble shootings.

While working on a project I stumbled upon this huge pile of problem of validating a form. Even though the fancy coding styles did make some things easy, it made some other things hard, especially when not having a complete documentation. So, here's a note for future.
\ 1. Custom validation messages: Custom messages, if not in a language file must be written for each field and each of the rules of that field. So you cannot escape with a single message written for email field if you want it to be shown on every rule. You have to write that same message for each rule, individually
2. Field name renaming: When printing validation messages Laravel, if not instructed otherwise, will use a slightly polished version of the 'name' attributes of you fields. So, for example name='user_name' will be made into user name. So if you instead want it to look like "Username", you have to use the method called
setAttributeNames
Like this:
$attributeNames = array(
   'name' => 'Name',
   'cat' => 'Category',     
);
$validator = Validator::make ( Input::all (), $rules );
$validator->setAttributeNames($attributeNames);
// Credits for: http://stackoverflow.com/a/25189223/1928610
3. Check uniqueness in a table having a different name than field name: Now what if you want to ensure that the user always enters an email that is not yet present in the 'users' table when signing up. Laravel has a great validation rule for it called 'unique:table_name'. So you use it like
'email' => 'unique:users'
But if you are like me, you may at first run into the problem where your field 'name' attribute does not match with the uniqueness field in your database. Like in my case, email field's name attribute contained 'email_address' but in my 'users' table, the column name was 'email'. So to fix this, Laravel validaton just takes another attribute with the validator.
'email_address' => 'unique:users,email'
Neat right?
Scouring through the documentation will help you more as it is doing for me. I'll keep posting further troubles I shoot in Laravel. InshaAllah.

Saturday, October 26, 2013

Running your first PHP application in Google App Engine (Fix for the --php_executable_path flag () issue)

As I may (or may not) have mentioned before, I mostly blog here for the purpose of note-keeping for myself. In most of the cases I don't really care about how good the tutorials (not so much) are coming out. So, please do your homework if you are following any of my tutorials at all. This is gonna be a disclaimer somewhere, but see? I can't give a damn here. Thanks.
I was trying my hands on the Google App Engine. My system is Linux so I downloaded, followed the linux part of things from their official how-to-first-time page here [ https://developers.google.com/appengine/docs/php/gettingstarted/installing ]
As you can see from that page (yeah, that's a dependency) you
1. Download the PHP SDK
2. Extract it some nice place
3. Write a "hello world!" PHP script
4. Execute the python script (dev_appserver.py) in the the "google_appengine" (if you haven't done it otherwise) folder.
BUT ! I got stuck here for sometime, and looked up for the fix.
Source of the problem:
_PHPBinaryError: The path specified with the --php_executable_path flag () does not exist.
Which naturally implied 2 things
1. The PHP installation was NOT as the app engine wants it to be
2. God doen't want me to code anymore
I naturally believe God loves it when I'm busy with my favourite thing so number 2 was obsolete. I looked up on Google for some solutions and a sudden SO page suggested that executing the appserver executable like
./dev_appserver.py --php_executable_path="...path..to..the..php-cgi..executable..." helloworld
will solve the problem.
Following this trail I came to notice that my ignorance toward the tutorial's PHP installation paid of here. The problem was actually my PHP installtion was a CLI version. While the app engine essentially wants a CGI version. For doing so, the tutorial has very good instructions, and yes, php-cgi is NOT available in Ubuntu repos. So I did
sudo apt-get install gcc libmysqlclient-dev libxml2-dev
  wget --trust-server-names http://us2.php.net/get/php-5.4.15.tar.bz2/from/us1.php.net/mirror
  tar xjf php-5.4.15.tar.bz2
  cd php-5.4.15
  ./configure --prefix=$PWD/installdir --enable-bcmath --with-mysql
  make install
  cd -
And afterwards executed
./dev_appserver.py --php_executable_path="...path..to..the..php-cgi..executable..." helloworld
And it went smooth. :) By the way, the above method will NOT harm you normal PHP installation.

Sunday, May 26, 2013

[PHP] Turn multiple input fields / selections into a single array

While trying to design a form so that it takes multiple inputs from text fields and multiselect selection menus, I was facing problem because even though I could handle a fixed number of fields in my script internally, what for multiple selections in a selection list?? The rescuer [] :)
Yes, just use it after the name of you input field, this way.
<select multiple class="input-medium search-query" id="autolist-filter-select" name="filter_str[]">
<input id="autolist-filter-text1" type="text" name="filter_str[]"/> <input id="autolist-filter-text2" type="text" name="filter_str[]"/>

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...